动态的URL变成静态的URL可以UrlRewriteFilter来处理
UrlRewriteFilter的介绍:
UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如 Resin,Orion,Tomcat等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。
为什么要使动态的URL变成伪静态的URL:
1:为了对搜索的友好,因为有些搜索不能抓取动态页面或是对动态抓取的页面没有静态页面高.
2:屏蔽内部的url结构.
3:美化url.
UrlRewriteFilter使用:
1.下载http://tuckey.org/urlrewrite/#download目前稳定的版本是2.6,最新版3.1,推荐使用2.6版.解压缩后将文件考到相应的web-inf/lib和web-inf下.
2、配置web.xml
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class> org.tuckey.web.filters.urlrewrite.UrlRewriteFilter </filter-class> <init-param> <param-name>logLevel</param-name> <param-value>WARN</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN" "http://tuckey.org/res/dtds/urlrewrite2.6.dtd"> <urlrewrite> <rule> <from>^/([a-z]+)/([a-z]+)/([0-9]+).htm$</from> <to>/$1/$2/index.jsp?currPage=$3</to> </rule> <rule> <from>^/([a-z]+)/([a-z]+)/([a-z]+)/([0-9]+).htm$</from> <to>/$1/$2/$3/index.jsp?currPage=$4</to> </rule> <rule> <from>^/register.htm</from> <to>/register.jsp</to> </rule> <rule> <from>^/signin.htm</from> <to>/signin.jsp</to> </rule> <rule> <from>^/search/searchExpo.html</from> <to>/search/searchExpo.action</to> </rule> </urlrewrite>
说明:
http://localhost:8080/test/news/news/2.htm 会直接映射到 http://localhost:8080/test/news/news/index.jsp?currPage=2 这个链接
http://localhost:8080/test/news/news/aa/2.htm 会直接映射到 http://localhost:8080/test/news/news/aa/index.jsp?currPage=2 这个链接
jsp代码:
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; String url = request.getRequestURL().toString(); request.setAttribute("url", url); System.out.println(request.getParameter("currPage")+"=============================>>>>>>>>>>>>>>>>>>>>>>>>"+url); %>
在浏览器中输入:http://localhost:8080/test/news/news/2.htm 发现url: http://localhost:8080/test/news/news/index.jsp 且currPage=2,然后在jsp中根据相应的参数做下一步处理;
一般是在jsp页面自定义标签,然后在自定义标签中根据参数处理:
如,可以在自定义中标签中;
public class TestTag extends TagSupport { private static final long serialVersionUID = 1L; protected int currPage = 1;// 当前页数,默认为1, currPage 接收到的值就为url重写后的值 private int pageSize;// 每页数据数量 @Override public int doStartTag() throws JspException { ..... } }
一般可以用这种方法实现网页的伪静态;
.