Java UrlRewrite implements live recording of the website URL rewriting process

Source: Internet
Author: User

Java UrlRewrite implements live recording of the website URL rewriting process

Currently, most websites and malls use URL rewriting. This is also because of the ongoing e-commerce Mall. URL rewriting uses another rule to display the original URL so that users can easily access it and shield some information.

Here, we will talk about its advantages. during the development process, we often encounter some URLs with a lot of parameters. This way, on the one hand, Seems messy, and on the other hand, some information is directly displayed on the URL, there will be some security issues. Using URL rewriting allows a URL with parameters to be reflected in a more rule, such:

 
 
  1. /demoAction?id=1            ==>            /demo1.html 

It also hides the passing parameters that are supposed to be displayed on the URL, hiding the technical implementation and sensitive information. In addition, URL rewriting is also conducive to search engine access.

Recently, the project used UrlRewrite to rewrite the URL. It mainly used the Filter technology to process the accessed URL during user requests to rewrite the URL.

The following are examples of use of UrlRewrite. I personally think that the official documentation of UrlRewrite is comprehensive and easy to understand)

Import of UrlRewrite:

UrlRewrite import is very simple, first you need to add the urlrewrite-3.2.0.jar package in the lib folder of the project, and then declare the Filter in web. xml

 
 
  1. <filter> 
  2.         <filter-name>UrlRewriteFilter</filter-name> 
  3.         <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> 
  4.     </filter> 
  5.     <filter-mapping> 
  6.         <filter-name>UrlRewriteFilter</filter-name> 
  7.         <url-pattern>/*</url-pattern> 
  8.         <dispatcher>REQUEST</dispatcher> 
  9.         <dispatcher>FORWARD</dispatcher> 
  10.     </filter-mapping> 

After the filter is declared, you need to create a new urlrewrite. xml file under the WEB-INF directory

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE urlrewrite 
  3. PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" 
  4. "http://tuckey.org/res/dtds/urlrewrite3.0.dtd"> 
  5. <urlrewrite> 
  6.  
  7. </urlrewrite> 

This file is the rule-making file for UrlRewrite. Later, you will configure it to rewrite the URL.

Now, the import of UrlRewrite is complete.

After UrlRewrite is successfully imported, you can rewrite the URL by adding rules in urlrewrite. xml. List some common rules here.

 
 
  1. <rule> 
  2.          <from>^/demo/(\w+).html$</from> 
  3.          <to type="redirect">/Struts/$1</to> 
  4. </rule> 

Rule is a subnode under urlrewrite and the primary rule node of urlrewrite. It contains two subnodes: from and to. from indicates the request URL, to indicates the actual URL to be converted. UrlRewrite has two matching modes: Regular Expression matching and wildcard matching. When matching, the matching part of the regular expression can be extracted as a parameter for transmission.

When the URL accessed by the client is http: // 127.0.0.1: 8080/Struts/demo/hello.html, the matching part is hello, so it jumps to http: // 127.0.0.1: 8080/Struts/hello. When there are multiple regular expressions in the URL rule, the matching parameters will also increase. For example:

 
 
  1. <rule> 
  2.          <from>^/demo1/(\w+)/(\w+).html$</from> 
  3.          <to type="redirect" >/Struts/$1.action?age=$2</to> 
  4. </rule> 

The default matching method of rule is a regular expression, but sometimes it can be matched as a wildcard. When writing rules, you only need to add a match-type = "wildcard" attribute in rule.

 
 
  1. <rule match-type="wildcard"> 
  2.          <from>/demo2/*/*</from> 
  3.          <to type="redirect">/Struts/$1.action?age=$2</to> 
  4. </rule> 

For the to node, UrlRewrite provides a variety of URL jump methods, such as forward and redirect. These two methods are the same as the functions provided by most MVC frameworks and will not be described here.

In addition to redirecting from a specified rule, UrlRewrite also supports executing a function of an object when matching the rule.

 
 
  1. <rule> 
  2.          <from>^/demo3/(\w+)/(\w+).html$</from> 
  3.          <run class="com.sean.action.Demo" method="log" /> 
  4.          <to type="redirect" >/Struts/$1.action?age=$2</to> 
  5. </rule> 

For example, to implement a matching rule to execute a function, you need to add one more run node and add the corresponding class and method attributes on the node. At the same time, the corresponding class must inherit the RewriteRule class, and the method to be executed must be passed in two parameters: HttpServletRequest and HttpServletResponse

 
 
  1. public class Demo extends RewriteRule{ 
  2.  
  3.     public void log(HttpServletRequest request,HttpServletResponse response){ 
  4.         System.out.println("haha1"); 
  5.     } 
  6.  
  7.     public void log2(HttpServletRequest request,HttpServletResponse response){ 
  8.         System.out.println("haha2"); 
  9.     } 

In this way, when the URL entered by the client matches the specified rule for the first time, UrlRewrite executes the corresponding function, which is executed only when the first matching is successful.

If you want to execute a function for each matching rule, you can add a class-rule subnode in urlrewrite. After this node is set, the specified function will be executed once after each matching rule.

 
 
  1. <class-rule class="com.sean.action.Demo" method="log2"/> 

In addition to processing the requested URL, UrlRewrite also provides the ability to rewrite the address on the returned page. Using rule is to process the url entered by the user. However, during development, you often need to add some url requests to the page. You can use UrlRewrite rules to rewrite the url on the page. For example:

 
 
  1. <outbound-rule match-type="regex"> 
  2.          <from>/(\w+).action\?id=(\w+)$</from> 
  3.          <to>/$1.html</to> 
  4. </outbound-rule> 

After adding this rule to UrlRewrite, the original address on the page is

However, the page is displayed as follows:

In this way, many development technologies can be hidden, Which is safer.

The above are some common usage of UrlRewrite. About UrlRewrite, some people on the Internet say this will affect the performance, because each request needs to undergo another filter, but this is still wise, after all, using URL rewriting, it is good for websites.

UrlRewrite help documentation: http://pan.baidu.com/s/1c0fg0uc

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.