Javaurlrewrite record of website URL rewriting process

Now most websites and shopping malls will use URL rewriting. It is also because of the E-commerce mall they are doing. URL rewriting is to use another rule to display the original URL, which makes it convenient for users to access and shield some information.

Here's its benefits. In the development process, we often encounter some URLs with a lot of parameters. On the one hand, it seems annoying. On the other hand, some information is directly displayed on the URL, which will cause some security problems. Using URL rewriting, URLs with parameters can be reflected in a regular way, such as:

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

It also hides the parameters that should be displayed on the URL, hiding the technical implementation and sensitive information. In addition, URL rewriting is also conducive to the access of search engines.

The URL rewriting contacted by the project recently adopts UrlRewrite, which mainly uses filter technology to process the accessed URL when the user requests to realize the role of rewriting.

The following is an example of the use of UrlRewrite (personally, the official document of UrlRewrite is relatively comprehensive and easy to understand)

Import of UrlRewrite:

The import of UrlRewrite is very simple. First, add urlrewrite-3.2.0 in the Lib folder of the project 0. Jar package, and then, on the web Declare filter in XML

<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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

After declaring the filter, you need to create a UrlRewrite. Net file in the WEB-INF directory XML file

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

This file is the rule making file of UrlRewrite. It is mainly configured to rewrite the URL in the future.

At this point, the import of UrlRewrite is complete

After the UrlRewrite is successfully imported, the next step is mainly through UrlRewrite By adding rules to XML, you can rewrite the URL. Here are some common rules.

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

Rule is the child node of UrlRewrite. It is the main rule node of UrlRewrite. It contains two child nodes: from and to. From represents the requested URL and to represents the real URL to go to. For from, UrlRewrite has two matching modes: regular expression matching and wildcard matching. The above is regular expression matching. When matching, the regularity of the matching part can be extracted and passed as parameters

According to the above rule settings, when the URL accessed by the client is http://127.0.0.1:8080/Struts/demo/hello.html Because the matching part is hello, it jumps to http://127.0.0.1:8080/Struts/hello Come on. When there are multiple rules in the URL rule, the matching parameters will also increase. For example:

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

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

 <rule match-type="wildcard">
         <from>/demo2/*/*</from>
         <to type="redirect">/Struts/$1.action?age=$2</to>
 </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 repeated here.

In addition to supporting the jump of specified rules, UrlRewrite also supports the execution of a function of an object when matching rules

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

As set above, to implement the matching rule is to execute a function, you need to add one more run node and add the corresponding class attribute and method attribute on the node. At the same time, the corresponding class must inherit the RewriteRule class, and the executed method must pass in two parameters: HttpServletRequest and httpservletresponse

public class Demo extends RewriteRule{

    public void log(HttpServletRequest request,HttpServletResponse response){
        System.out.println("haha1");
    }
    
    public void log2(HttpServletRequest request,HttpServletResponse response){
        System.out.println("haha2");
    }
}

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

If you want to execute a function every time a rule is matched, you can add a class rule child node in UrlRewrite. After this node is set, the specified function will be executed once every time a rule is matched.

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

In addition to processing the requested URL, UrlRewrite also provides the function of rewriting the address in the return page. Rule is used to process the URL entered by the user, but in the development process, it is often necessary to add some URL requests to the page. UrlRewrite can rewrite the URL in the page through rules. For example:

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

After adding the rule in UrlRewrite, our original address in the page is

However, it is displayed on the page as follows:

In this way, many developing technologies can be hidden and safer.

These are some common uses of UrlRewrite. About UrlRewrite, some people on the Internet say that it will affect the performance, because each request needs to be filtered again, but there are different opinions. After all, using URL Rewrite is still good for the website.

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>