This article is a common usage of urlrewrite. About Urlrewrite, some people on the internet said this will affect performance, because each time you need to change the filter, but this is still to be a matter of opinion, after all, the use of URL rewriting, the Web site is still good.
Now most of the website and the mall will use to rewrite the URL, contact this, but also because of the E-commerce Mall. URL rewrite, is to use the original URL of another rule to display, so that users convenient access at the same time shielding some information.
The advantages of this, in the development process, often encounter some with a lot of parameters of the URL, this way, on the one hand appears upset, on the other hand, some information directly displayed on the URL, there will be some security issues. URL rewriting allows URLs with parameters to be represented in a more regular manner, such as:
/demoaction?id=1 ==>/demo1.html
It also hides the technical implementation and sensitive information by hiding the parameters that should be displayed on the URL. In addition, URL rewriting is also beneficial to search engine access.
Recent project access to the URL rewrite using the urlrewrite, it is mainly using the filter technology in the user request to access the URL to deal with, to achieve the role of rewriting.
The following is an example of the use of Urlrewrite (personally, Urlrewrite's official documents are more fully understandable)
Import of Urlrewrite:
Urlrewrite import is very simple, first you need to add the Urlrewrite-3.2.0.jar package to the project's Lib folder, and then declare the filter in Web.xml
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.we b.filters.urlrewrite.urlrewritefilter</filter-class> </filter> <filter-mapping> <f Ilter-name>urlrewritefilter</filter-name> <url-pattern>/*</url-pattern> <DISPATC Her>request</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
After the filter is declared, you need to create a new Urlrewrite.xml file in the Web-inf directory
<?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>
The file is a Urlrewrite rule-making file, followed primarily by configuring it for URL rewriting.
At this point, Urlrewrite's import is complete
After the Urlrewrite import succeeds, it is possible to override the URL by adding the rule in Urlrewrite.xml. Some common rules are listed here.
<rule> <from>^/demo/(\w+) .html$</from> <to type= "redirect" >/STRUTS/$1</TO&G T </rule>
Rule is a child node under Urlrewrite and is the primary regular node of Urlrewrite, which contains the from and to two child nodes, from the url,to representing the request represents the true URL to be transferred. There are two matching patterns for From,urlrewrite, one is regular expression matching, one is matching wildcard characters, such as the regular expression match. When a match is made, the regular of the matching part can be extracted as a parameter for passing
As on the rule setting, when the client accesses the URL is http://127.0.0.1:8080/Struts/demo/hello.html, because the matching part is Hello, so it jumps to the http://127.0.0.1:8080/ On the Struts/hello. When there are more than one regular in the URL rule, the matching parameters will also increase. Such as:
<rule> <from>^/demo1/(\w+)/(\w+) .html$</from> <to type= "redirect" >/struts/$1. Action?age=$2</to> </rule>
The default match for rule is a regular expression, but sometimes it can be matched in the form of a wildcard character. When writing rules, just add a match-type= "wildcard" attribute to rule.
<rule match-type= "Wildcard" > <from>/demo2/*/*</from> <to type= "redirect" >/stru Ts/$1.action?age=$2</to> </rule>
With respect to the to node, Urlrewrite offers a variety of URL jumps, such as forward and redirect, which, like most of the features provided by the MVC framework, are not discussed here.
In addition to supporting the jump of a specified rule, Urlrewrite also supports executing a function of an object when matching a rule
<rule> <from>^/demo3/(\w+)/(\w+) .html$</from> <run class= "Com.sean.action.Demo" M ethod= "Log"/> <to type= "redirect" >/Struts/$1.action?age=$2</to> </rule>
As mentioned above, to implement a matching rule is to execute a function, you need to add more than one run node, add the corresponding class properties and method properties on the node. At the same time, the corresponding class must inherit the Rewriterule class, and the executing method must pass in two parameters, namely 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"); } }
Thus, when the URL entered by the client is first matched to the specified rule, Urlrewrite performs the corresponding function, which is executed only when the first match succeeds.
If you want to perform a function every time you match a rule, you can add a class-rule child node in Urlrewrite, and after that node is set, the specified function executes once each time the rule is matched.
<class-rule class= "Com.sean.action.Demo"