|
Javax.servlet.Filter class has three main methods.
public void destroy (); // destroy the object
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain); Filter response code // write In this method
public void init (FilterConfig fConfig); // initialize objects
First create a web project, the establishment of two JSP pages, and the procedure main article on the use of the death doFilter () method, to jump from index1.jsp index2.jsp.
Establish good index1.jsp page and index2.jsp.
Click WEB.xml following configuration, configuration WEB.xml the Servlet Filter and configuration as the class name and class package, then the map is very 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 .action end of the page request can be returned to the filter
< / Filter-mapping>
Then index1.jsp page, just write a
Click here to jump caused index2.jsp
Test whether you can successfully jump, index2.jsp content easily (this is my page!).
Next is the configuration package com Filter class doFilter () method. Specific code as follows:
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getServletPath (); // This method only HttpServletRequest class has to obtain a path to the page response
System.out.println (path);
if ( "/ forward.action" .equals (path)) {// If they are consistent with the jump in href index1.jsp the address index2.jsp
request.getRequestDispatcher ( "index2.jsp") forward (request, response).;
} Else {// if not jump index3.jsp page
request.getRequestDispatcher ( "index3.jsp") forward (request, response).;
}
chain.doFilter (request, response);
the above. |
|
|
|