Urlrewrite in Java

Source: Internet
Author: User

Recently the company's previous project needs to upgrade the revision, one of the modules is to use Struts2 do not need to change, but need to be removed from the previous project, see the Crossing network, find all the static address of the link access, thought is freemarker realization, Careful study will find that it is not so, the original is pseudo-static, the program used Urlrewriter. In fact, the use of Urlrewriter is very simple, see below you will understand.

Urlrewriter Introduction: URL rewrite to translate our dynamic Web address into static address, such as HTML, shtml, but also can hide the true path of the page, such as: sometimes need to xxx.com/news/type1/001.jsp Turns the display path to xxx.com/news_type1_001.jsp.

"First step" Download Jar package

In http://tuckey.org/urlrewrite/#download下载最新的jar文件, the latest is the Beat3.2 i downloaded is urlrewritefilter-2.6.zip. Unzip to get Urlrewrite-2.6.0.jar and Urlrewrite.xml

"Step Two" new Web project Testurlrewrite,

Create a new index.jsp under Webroot, and modify its code as follows:

Java code
  1. <%@ page contenttype= "text/html; Charset=utf-8 "%>
  2. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
  3. <title> Test testurlrewrite</title>
  4. <body>
  5. <%
  6. String id = (string) request.getparameter ("id");
  7. Out.println ("
  8. %>
  9. </body>
<%@ page contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

"Fourth Step" release program

Publish the program to Tomcat, and then enter the following address in your browser:

Http://localhost:8080/TestUrlRewrite/index.jsp?username=niejingyu, page display effect 1 shows:

"Fifth Step" add Urlrewrite File

Copy the Urlrewrite-2.6.0.jar to Webroot\web-inf\lib.

Copy the Urlrewrite.xml to Webroot\web-inf, where the Urlrewrite.xml code looks like this:

XML code
    1. <?xml version= "1.0" encoding= "Utf-8"? > 
    2.  
    3. <! DOCTYPE urlrewrite Public "-//tuckey.org//dtd urlrewrite 3.1//en" &NBSP;
    4.  
    5.    & nbsp;    "Http://tuckey.org/res/dtds/urlrewrite3.1.dtd" >&NBSP;
    6.  
    7. < urlrewrite> 
    8.  
    9.        <rule> 
    10.  
    11.               <from>/(. *) . html</from> 
    12.  
    13.                <to>index.jsp?id=$1</to> 
    14.  
    15.         </rule> 
    16.  
    17. </urlrewrite> 
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE urlrewrite Public "-//tuckey.org//dtd urlrewrite 3.1//en"        "Http://tuckey.org/res/dtds/urlrewrite3.1.dtd "><urlrewrite>       <rule>              <from>/(. *) .html</from>              <to>index.jsp?id=$1 </to>       </rule></urlrewrite>

*.html is a string of regular expressions. If you don't understand this, you can learn about the application of the Java Express expression.

$ $ is also a group capture in a regular expression's language-matching regular.

"Sixth step" modifying the Web. xml file

Add the following to Web. xml:

XML code
  1. <filter>
  2. <filter-name>UrlRewriteFilter</filter-name>
  3. <filter-class>
  4. Org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
  5. </filter-class>
  6. <init-param>
  7. <param-name>logLevel</param-name>
  8. <param-value>DEBUG</param-value>
  9. </init-param>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>UrlRewriteFilter</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>
<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>DEBUG</param-value>       </init-param ></filter><filter-mapping>       <filter-name>UrlRewriteFilter</filter-name>       < Url-pattern>/*</url-pattern></filter-mapping>

"Seventh Step" reboot Tomcat

Access http://localhost:8080/TestUrlRewrite/2.html, page effects:

Here is the match/(. *). HTML, which forwards the request to index.jsp?id=$1. You can also go to xxx.action?id=$1 as needed

Jixian Farmhouse Network Chengdu Farm House Network Chongqing Farm Network Beijing Farmhouse Network Chang-zhu-Tan farm stay Net Love Farm network

Urlrewrite in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.