The mechanism of Javaweb filter filter and the difference between using request redirection and Request forwarding filter, filter dispatcher request and forward difference __java

Source: Internet
Author: User

The first thing to be clear is that the URL is filtered

So will all page jump URLs change? No, like request forwarding, all backwards and forwards are a URL
First, we should make clear the difference between request redirection and request forwarding;
For example, there are two Web pages index.jsp and then jump to main.jsp request forwarding in two different ways

server-side behavior, can be understood as "internal processing" content change, url unchanged
Equivalent to Index.jsp said to me, and then secretly outsourced to main.jsp to deal with

The most common, is the continuation of the user's business process, the equivalent of a user request a page, and then the page to another page processing, url unchanged, the user is not aware of

Forward from index.jsp request to main.jsp

- URL unchanged, content is main.jsp content, url or index.jsp;
-The user requests and submits the data to retain, may continue processing;
-User not aware of request redirection

client behavior, can be understood as a restart, a pure jump URL to change

The equivalent of index.jsp said I don't care, you find someone else

Then the user submits the request to the MAIN.JSP, two separate requests, the URL certainly changes, jumps to a completely unrelated new visit;

Compare two times can know, two kinds of jump essence completely different, if establish a simplefilter filter content is jump;
Filter/index.jsp and/main.jsp two URLs, code as follows;

Simplefilter.java

Import Com.sun.net.httpserver.Filter;

Import javax.servlet.*;
Import java.io.IOException;

/**
 * Created by xuh-h on 2017/7/20.
 *
/public class Simplefilter implements javax.servlet.filter{
    @Override public
    void Init (filterconfig Filterconfig) throws Servletexception {
        System.out.println ("Filter initialization ************");

    @Override public
    void Dofilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain Filterchain) throws IOException, servletexception {
        System.out.println ("Start of Filter" ")";
        /*filterchain.dofilter (ServletRequest, servletresponse); * *
        System.out.println ("First monitor Start");
        Servletrequest.getrequestdispatcher ("main.jsp"). Forward (ServletRequest, servletresponse);
        System.out.println ("Filter End") "" "" "";

    }

    @Override public
    Void Destroy () {
        System.out.println ("Terminate ...). ");
    }
}

Web.xml

    <filter>
        <filter-name>SimpleFilter</filter-name>
        <filter-class>simplefilter </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>simplefilter</ filter-name>
        <url-pattern>/index.jsp</url-pattern>
    </filter-mapping>
        < filter-mapping>
        <filter-name>SimpleFilter</filter-name>
        <url-pattern>/main.jsp </url-pattern>
    </filter-mapping>

1 for request forwarding; forward jump to main.jsp url unchanged, so the filter only filters once/index.jsp access;
Of course this is for the dispatcher type is the default request, if the dispatcher type is changed to forward, then even if the URL does not change, you can filter forward jump behavior, and then will die circular access main.jsp;

2 for request redirection, the URL has been changed, and then has been filtered, dead loop

3 If you want to monitor the forward behavior of main.jsp, it is best to write another filter simplefilter2,dispatcher type for forward, then whenever index.jsp secretly jump to the main.jsp can have the corresponding operation;

Simplefilter2.java

Import javax.servlet.*;
Import java.io.IOException;

/**
 * Created by xuh-h on 2017/7/20.
 *
/public class SimpleFilter2 implements javax.servlet.filter{
    @Override public
    void Init (filterconfig Filterconfig) throws Servletexception {
        System.out.println ("Filter2 initialize ************");

    @Override public
    void Dofilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain Filterchain) throws IOException, servletexception {
        System.out.println ("Filter2 start") "");
        /*filterchain.dofilter (ServletRequest, servletresponse); * *
        System.out.println ("Forward jump monitor Start");
        Filterchain.dofilter (ServletRequest, servletresponse);

        System.out.println ("The End of Filte2") "" "" ";

    }

    @Override public
    Void Destroy () {
        System.out.println ("2 terminated ...). ");
    }
}

Run results

[2017-07-20 01:52:49,242] Artifact Filterdemo:war Exploded:artifact is deployed successfully
[2017-07-20 01:52:49,242] Artifact FilterDemo: War Exploded:deploy took 615 milliseconds Filter start "" "" ""
first monitoring start of the
Filter2 start
"" "" "" "" Forward jump monitor Start main interface main "" "" "
" Filte2 End "" "" "", "" The "" Filter
"" "" "" "," "" "" "" "" First listens to start Filter2 start "" "", "" ""
forward jump Monitor start
main interface main
"" "" "", "" "" "" "" "", "" "" "" "" " Filter End "", "" "

I hope that through my explanation can let you have a certain understanding of the jump filter

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.