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)