One of the simplest Urlrewrite filters implemented via Java WEB filter

Source: Internet
Author: User

Tools: Java EE-enabled Eclipse IDE

Web filters can be added directly through a dynamic Web project, or directly by creating a Java project by adding classes, see below filter code:

Package Me.ereach.urlfilter;import Java.io.ioexception;import Javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.requestdispatcher;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;import Javax.servlet.annotation.webfilter;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.HttpServletResponse; @WebFilter (filtername = "Filter01", Urlpatterns = {"/*"}) public class Filter01 Implements Filter {private filterconfig fc;public void init (Filterconfig fconfig) throws servletexception {THIS.FC = fconf IG;} public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {HttpServletRequest HttpRequest = (httpservletrequest) request; HttpServletResponse HttpResponse = (httpservletresponse) response; String ContextPath = Httprequest.getcontextpath (); String Servletpath = Httprequest.getservletpath (); if (SERVLEtpath.startswith ("/hallo/")) {int L = servletpath.length (); String next = ContextPath + servletpath.substring (4); RequestDispatcher dispatcher = Fc.getservletcontext (). Getrequestdispatcher (Next);d Ispatcher.forward (Request, response); return;} else if (Servletpath.startswith ("/welcom/hi/")) {int L = servletpath.length (); String next = ContextPath + servletpath.substring (9); RequestDispatcher dispatcher = Fc.getservletcontext (). Getrequestdispatcher (Next);d Ispatcher.forward (Request, response); return;} Chain.dofilter (request, response);} @Overridepublic void Destroy () {//TODO auto-generated method stub}}

This filter is very simple, all the forwarded URLs are written in the code, so as long as the registration of the filter can be used directly. The following steps are required to use this filter:

    1. Export this filter as a jar file.
    2. Copy this jar to the Web app's Web-inf/lib
    3. Register this jar in Web-inf/web-xml
    4. Set Context-root to "/"
    5. Restart the Web app for the settings to take effect

One of the simplest Urlrewrite filters implemented via Java WEB filter

Related Article

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.