Java EE Filter Usage example (page jump)

Source: Internet
Author: User

There are three main methods in the Javax.servlet.Filter class.

 Public void destroy ();  // destroying Objects  Public void // The code that executes the filter response is written in this method  Public void init (filterconfig fconfig);  // Initializing Objects

First set up a Web project, set up two JSP pages, and the program in this article mainly implements the dead use Dofilter () method, from index1.jsp jump to index2.jsp.

Build good index1.jsp pages and index2.jsp.

The following configuration of Web. XML, configuring the filter in Web. XML as the configuration servlet, the class name and class package, and then the mapping, is simple.

<Filter>  <Filter-name>Filter</Filter-name>  <Filter-class>Com. Filter</Filter-class>  </Filter>  <filter-mapping>  <Filter-name>Filter</Filter-name>//should be consistent with the above Filter-name  <Url-pattern>*.action</Url-pattern>//Any end-of-action page request can be returned to the filter  </filter-mapping>

Then the index1.jsp page, just write a

<  = "forward.jsp" > Click this jump to index2.jsp </a  >

Test if the jump is successful, index2.jsp content (this is my page!).

The next step is to configure the Dofilter () method in the filter class in the COM package. The specific code is as follows:

HttpServletRequest req = (httpservletrequest) request;         = Req.getservletpath ();  This method is only available in the HttpServletRequest class, and gets the path of the page response        System.out.println (path);         if ("/forward.action". Equals (path)) {  //If the address in the href in index1.jsp is consistent, jump index2.jsp            Request.getrequestdispatcher ("index2.jsp"). Forward (request,response);        } Else {                               //if inconsistent, jump to index3.jsp page            request.getrequestdispatcher ("index3.jsp"). Forward (Request, Response);        }                Chain.dofilter (request, response);

Above.

Java EE Filter Usage example (page jump)

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.