你的位置:首页 > Java教程

[Java教程]java使用URLRewrite实现网页伪静态(servlet过滤器)


步骤1:引入包urlrewrite 3.2.0.jar:(点我下载)
步骤2:web. 
 < filter>
     <filter-name >UrlRewriteFilter </filter-name >
       <filter-class> org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class >
  </ filter>
 
  < filter-mapping>
     <filter-name >UrlRewriteFilter </filter-name >
     <url-pattern >/* </url-pattern >
  </ filter-mapping>

步骤3: WEB-INF 下新建urlrewrite.
<? version ="1.0" encoding= "UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
             "http://tuckey.org/res/dtds/urlrewrite3.2.dtd" >
            <urlrewrite >
            <rule >
                 <from >/userinf-(\w*).html </from >
                 <to type= "forward">/userinf.jsp?username=$1 </to >
            </rule >
            </urlrewrite >
 

注:只要访问路径为..../userinf-(\w*).html,都会被解析成userinf.jsp?username=$1
 
下面是一个测试的jsp页面,
 
<%@ page language ="java" import= "java.util.*" pageEncoding= "utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://" +request.getServerName()+":" +request.getServerPort()+path+"/";
String user = request.getParameter("username");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
  < head>
    <base href= "<%= basePath%> ">
    <title ><%= user %> 的个人信息</title >
  </ head>
  < body>
 <%
     out.println("现在时间是:" + new java.util.Date() +"</br>");
     out.println("用户名:"+user);
  %>
  </ body>
</html>
 

运行示例:
 

 
 

==============================END==================================