Java development: Share a personalized visit using Urlrewrite to achieve URLs

Source: Internet
Author: User

Many web sites have some more personalized access to the domain name, such as: http://www.xxx.com/u/pp point Open can see PP's personal homepage, this address is easier to remember, that today to talk about the implementation of this technology.

1. Using Java as the development language to implement this kind of address rewriting requires you to introduce a jar package: Urlrewrite-3.1.0.jar

2. Then it is to do a personalized address configuration, here to create the urlrewrite.xml, I put it in the Web-inf directory, the code is as follows:

  

<?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>^/u/([a-za-z0-9]*) $</ from>        < totype= "Forward">/blog/tohome.do?xxx=$1</ to>    </Rule>        <Rule>//can also be configured a lot, with parameters or not with all can</Rule></Urlrewrite>

3. Next is to load our Urlrewrite.xml file in Web. XML, the code is as follows:

<!--URL Rewrite filter -    <Filter>        <Filter-name>Urlrewritefilter</Filter-name>        <Filter-class>Org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</Filter-class>        <Init-param>            <Param-name>Confpath</Param-name>            <Param-value>/web-inf/urlrewrite.xml</Param-value>        </Init-param>    </Filter>    <filter-mapping>        <Filter-name>Urlrewritefilter</Filter-name>        <Url-pattern>/*</Url-pattern>    </filter-mapping>

4. If it is without parameters, enter the alias access directly on the browser, and the background will automatically jump to the specified page.

4. If it is a parameter, the name of the domain address of the personality, here also gives a method, backstage received the request after processing, the code is as follows:

        //Address bar Input alias processing@RequestMapping (value = {"/tohome"}, Method ={requestmethod.get}) PublicString Tohome (Member model, HttpServletRequest req, Modelmap modelmap) {//Query by aliasMap<string, object> maps =NewHashmap<string, object>(); Maps.put ("XXX", Model.getxxx ()); List<Object> list =Memberservice.selectall (maps); if(List! =NULL&& list.size () > 0) {                //load data, jump            return"Home/page"; } Else {            return"404"; }    }

5. Finally, we can enter on the browser: HTTP://WWW.XXX.COM/U/PP to see the effect.

Java development: Share a personalized visit using Urlrewrite to achieve URLs

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.