Java Address bar Rewrite very detailed

Source: Internet
Author: User

The egg hurts these days. Look at how someone else's URL rewriting is.
1. Explain what's going on URL rewriting, and its pros and cons:

URL rewriting, in fact, is to take a lot of parameters of the URL, to become a well-behaved URL.
Example:/viewthread.jsp?id=1234
After rewriting, you can use/viewthread/1234.html

What are the benefits of using URL rewriting to your site?
First: Facilitate the search engine crawl, because now most of the search engine on the dynamic page crawl is still relatively weak, they prefer to crawl some static pages. Most of the data on our current page is dynamically displayed. This requires us to change the dynamic page into a static page, to facilitate the search engine crawl.
Second: Make users easier to understand, very few users to care about your site's page address, but for the general medium and large sites to enhance readability is still necessary. This will make your site more perfect.
Third: The implementation of hidden technology, we can use URL rewriting can realize the hidden technology. Do not expose the technology you are using and offer convenience to enthusiasts who want to attack your site.
Four: can be very convenient reuse, improve the portability of the site. If we change the method in the background, we can ensure that the page part of the foreground is not changed. This improves the portability of the site.
Although it has so many advantages, but also a little disadvantage, because it is through the filter principle to achieve, it is thought that there is a more access, will affect the speed of the point of access, this can be negligible
Let's first understand how it works, but it's a simple filter that transforms the URL of the request into the URL we want and then requests it.
There are several ways to override URL rewriting:
Example of a custom filter:

1. Configuration filters are generally in Web. xml
<filter>
<filter-name>urlFilter</filter-name>
<filter-class>com.url.filter.URLFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>urlFilter</filter-name>
<url-pattern>*.shtml</url-pattern>
</filter-mapping>
2. Create a new parsing rule configuration file Urlrewrite.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<urlrewrite>
<rule>
<from>^/(\w+) _ (\w+) _ (\d+) \.shtml$</from> match expression-Regular expression
<to type= "forward" >/$1.do?$2=$3</to> post parse URL
</rule>
</urlrewrite>
3. Filter Handling Class Urlfilter.java
public class Urlfilter implements Filter {
public void DoFilter (ServletRequest servletrequest, Servletresponse
Servletresponse, Filterchain Filterchain) throws
Ioexception,servletexception
{
HttpServletRequest request = (httpservletrequest) servletrequest;
HttpServletResponse response = (httpservletresponse) servletresponse;
String Realpath
=request.getsession (). Getservletcontext (). Getrealpath ("/");

String fileName = Realpath + "Web-inf\\urlrewrite.xml";
String uri = Request.getservletpath ();
String Rewriteurl = Getrewriteurl (URI, fileName);
if (null! = Rewriteurl) {
Request.getrequestdispatcher (Rewriteurl). Forward (request, response);
Return
}
Filterchain.dofilter (ServletRequest, servletresponse);

}

private string Getrewriteurl (string url, string fileName) {
Documentbuilderfactory f = documentbuilderfactory.newinstance ();
try {
Documentbuilder builder = F.newdocumentbuilder ();
Document document = Builder.parse (FileName);
NodeList list = Document.getelementsbytagname_r ("rule");
for (int i = 0; i < list.getlength (); i++) {
Element elemnt = (Element) list.item (i);
NodeList List2 = Elemnt.getelementsbytagname_r ("from");
element element = (Element) list2.item (0);
String Formvalue = Element.getfirstchild (). Getnodevalue ();
NodeList list3 = Elemnt.getelementsbytagname_r ("to");
Element element2 = (element) list3.item (0);
String type = Element2.getattribute ("type");
String Tovalue = Element2.getfirstchild (). Getnodevalue ();
String Rewriteurl = Url.replaceall (Formvalue, Tovalue);
if (URL! = null &&! "). Equals (Url.trim ()) &&
!url.equals (Rewriteurl)) {

return rewriteurl;

}

}

} catch (Exception ex) {

Ex.printstacktrace ();

}

return null;

}

}

Second, Tomcat's filter components
1: Download urlrewrite-2.6.0. The latest version is now 3.1.
Http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip
Http://urlrewritefilter.googlecode.com/files/urlrewritefilter-3.1.0.zip
Cuff the Urlrewrite-2.6.0.jar to the classpath.
2: Build a Urlrewrite.xml file in the Web-inf directory.
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE urlrewrite Public "-//tuckey.org//dtd urlrewrite 2.6//en"
"Http://tuckey.org/res/dtds/urlrewrite2.6.dtd" >
<urlrewrite>
<rule>
<from>^/(\w+) _ (\w+) _ (\d+) \.shtml$</from>
<to type= "Forward" >/$1.do?$2=$3&amp;pageSize=15</to>
</rule>
</urlrewrite>
Explain:
<from></from> Write your own defined access address
<to type= "Forward></to> is the actual access address.
For example, our actual access address is:
/urlrewriterdemo/urltestaction.do?pagenumber=123
And we want to rewrite it:
/urlrewriterdemo/urltestaction_pagenumber_123.shtml
This looks more lot better than we actually are. That's what we should write.
<rule>
<from>^/(\w+) _ (\w+) _ (\d+) \.shtml$</from>
<to type= "Forward" >/$1.do?$2=$3&amp;pageSize=15</to>
</rule>
Note: Common & to use &amp; To express. The $1,$2 represents the parameters that correspond to the regular expression >/(\w+)/(\w+) that you configure. <to type= "forward" > Default is Type= "forward".

3: Initialize in Web. Xml. Add the following code:
<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>*.shtml</url-pattern>
</filter-mapping>
Third, the use of the system 404 error
When the request must be unable to find the relevant page, then jump to error.jsp for the corresponding processing
1. Add error jump configuration Web. xml
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
2. Write error page error.jsp
<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "pageencoding=" GBK "%>
<%
Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);
String key = (string)
Request.getattribute ("Javax.servlet.forward.servlet_path");
String reg = "^/(\\w+) _ (\\w+) _ (\\d+). shtml$";
String Rewriteurl = Key.replaceall (Reg, "/$1.do?$2=$3&amp;pagesize=15");
if (key! = NULL | |!key.equals (REWRITEURL)) {
Request.getrequestdispatcher (Rewriteurl). Forward (Request,response);
}
else{
Out.print ("Sorry, the page you requested was not found!");
}
%>

Java Address bar rewrite is verbose

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.