J2ee Filter instance (page Jump), j2eefilter
The javax. servlet. Filter class contains three methods.
Public void destroy (); // destroy object public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain ); // write the code for executing the Filter response in this method public void init (FilterConfig fConfig); // initialize the object
First, create a web project and create two JSP pages. The program in this article uses the doFilter () method to jump from index1.jsp to index2.jsp.
Create the index1.jsp page and index2.jsp.
Next, configure WEB. xml to configure the Filter in WEB. xml, which is the same as configuring Servlet, Class Name and class package, and then ing, which is very simple.
<Filter> <filter-name> filter </filter-name> <filter-class> com. filter </filter-class> </filter> <filter-mapping> <filter-name> filter </filter-name> // it must be consistent with the filter-name above. <url -pattern> *. action </url-pattern> // any. all page requests at the end of the action can be returned to the filter </filter-mapping>
Then there is the index1.jsp page, you only need to write one
<A href = "forward. jsp"> click here to jump to index2.jsp </a>
Test whether the jump is successful. The index2.jsp content is arbitrary (this is my page !).
Next, configure the doFilter () method in the Filter class in the com package. The Code is as follows:
HttpServletRequest req = (HttpServletRequest) request; String path = req. getServletPath (); // This method is only available in the HttpServletRequest class and obtains the page response path System. out. println (path); if ("/forward. action ". equals (path) {// if it is consistent with the address in href in index1.jsp, the request is redirected to index2.jsp. getRequestDispatcher ("index2.jsp "). forward (request, response);} else {// if not consistent, jump to the index3.jsp page request. getRequestDispatcher ("index3.jsp "). forward (request, response);} chain. doFilter (request, response );
Above.