Java URLRewrite重定向

urlrewrite的主要作用:

    1)隐藏真实URL,提高安全性

    2)更加友好的URL,好记(看博客园就行知道啦)

    3)便于搜素引擎收录

首先,准备工作,下载urlrewrite:http://code.google.com/p/urlrewritefilter/downloads/list

可以随意选择自己喜好的版本下载。copy jar包到lib目录中,然后copy urlrewriter.xml到WEB-INF目录下,提供urlrewriter.xml文件内容:







    
        
            The rule means that requests to /test/status/ will be redirected to /rewrite-status
            the url will be rewritten.
        
        /test/status/
        %{context-path}/rewrite-status
    


    
        
            The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
            the url /rewrite-status will be rewritten to /test/status/.

            The above rule and this outbound-rule means that end users should never see the
            url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
            in your pages.
        
        /rewrite-status
        /test/status/
    


    


添加 filter到web.xml:

     UrlRewriteFilter
     org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
     
          logLevel
          WARN
      


      UrlRewriteFilter
      /* 

PS:如果使用的是struts2框架, urlrewrite的filter需要在struts2的filter之前

urlrewrite中含有两个标签


        
            The rule means that requests to /test/status/ will be redirected to /rewrite-status
            the url will be rewritten.
        
        /test/status/
        %{context-path}/rewrite-status

会把你的请求链接根据你写好的规则转换成对应的请求,也就是的子标签和代表来源链接(伪链接),代表转换后的连接(真实链接)。比如上面的请求,会把/test/status/转换成/rewrite-status请求服务器中内容。


        
            The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
            the url /rewrite-status will be rewritten to /test/status/.

            The above rule and this outbound-rule means that end users should never see the
            url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
            in your pages.
        
        /rewrite-status
        /test/status/

就是把你页面上的URL根据规则转换对应的的美化后的URL, 需要重写URL,可以使用response.encodeURL  或者 JSTL标签,标签中的是真实链接,中的是伪链接。这个标签会把你包含/rewrite-status的链接转换成/test/status/。

中标签都是根据正则来进行匹配


	    ^/company/([0-9]+).html$
	    /goods/company.jsp?companyId=$1
参数需要用()包围起来。[0-9]+代表一个或多个0-9的数字。

标签中有两个小字符"^"和"$"。

“^”这个小字符,根据我的测试,代表你的项目URL,比如我的是http://localhost:8080/urlrewriteDemo/,如果你的请求是http://localhost:8080/urlrewriteDemo/company/1.html,则会被转换成http://localhost:8080/urlrewriteDemo/goods/company.jsp?companyId=1。

如果你的请求http://localhost:8080/urlrewriteDemo/test/company/1.html则不会被转换,因为匹配不上,如果去掉"^",则可以匹配上,请求会被转换成http://localhost:8080/urlrewriteDemo/test/goods/company.jsp?companyId=1。

"$"代表任意字符串,也就是说,你的请求字符串http://localhost:8080/urlrewriteDemo/company/1.html,无论在后面加什么字符串,比如说http://localhost:8080/urlrewriteDemo/company/1.html?id=1111,也只会转换成http://localhost:8080/urlrewriteDemo/goods/company.jsp?companyId=1,如果没有"$",则会被转换成http://localhost:8080/urlrewriteDemo/goods/company.jsp?companyId=1?id=1111

你可能感兴趣的:(JavaEE)