The concept of the filter:
Filter one JavaWeb three components (the servlet , listener , filter) , it Servlet is very similar! However, filters are used to intercept the request, instead of processing the request.
When a user requests a time Servlet, it will first be deployed on the implementation of this request Filter , if Filter "release", it will inherit the Servlet execution requested by the user ; if Filter does not "release", then it will not execute the user's request Servlet .
In fact, it can be understood that when a user requests a time Servlet, Tomcat will go to perform the registration on this request Filter , and then whether or not "release" by the Filter to decide. Can be understood, the Filter to decide whether to call the Servlet ! When the execution is completed Servlet the code also performs Filter code behind.
The user sends a request, Tomcat server after get request, perform the corresponding the servlet , the response content to the user. With the filter, the user sends a request, the server tomcat after receiving the request, first performs a filter, and then to execute servlet
The Filter : Before performing the resource requested by the user, will first perform a filter, if the filter is released, it will execute the resource requested by the user, if the filters do not release the user requested resource can not be executed .
scenes to be used:
1 , the user is not logged in to intercept
2 , centralized code cut (focus on Chinese garbled)
3 , the efficiency of the statistical program code
Filter effect :
1 : can intercept requests (request)
2 : may be intercepted response (response)
3 : release , agreed to by connector
4 : Statistics
How to create a filter :
1 : implement an interface filter
2 : In the web.xml way @WebFliter registration or use annotations in the Filter ( "/ *"), the following example uses annotations way
在web.xml文件中进行配置
<filter>
<display-name>FilterDemo1</display-name>
<filter-name>FilterDemo1</filter-name>
<filter-class>com.aaa.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Whether the user is logged intercept
com.aaa.Fliter Package;
Import the javax.servlet *;.
Import the javax.servlet.Filter;
Import javax.servlet.annotation.WebFilter;
Import java.io.IOException;
// use annotations arranged: to intercept all requests
@WebFilter ( "/ *")
public class Filter2 the implements the filter {
public void the destroy () {
System.out.println ( "destruction ......... ........... filter");
}
public void the doFilter (the ServletRequest req, the ServletResponse RESP, the FilterChain catena alberghiera) throws ServletException, IOException {
// HttpServlet type first converted req and RESP
the HttpServletRequest Request = (the HttpServletRequest) req;
Response = HttpServlerResponse (the HttpServletResponse) RESP;
// Get the session object (user) determines whether null
Object Request.getSession = User () the getAttribute ( "User");.
IF (! User = null) {
// User is not Description already logged empty, release
chain.doFilter (request, the Response);
} the else {
// the User not logged empty explanation, jump to the login screen, but not all requests must be intercepted, but also to remove some here , like login screen requests, invokes the login function
String STRs [] = { "the login.jsp", "the LS"};
// get the user request path, and then determines whether the user requests a path comprising a path STRs [] is, if it contains on the release
String requestURI = request.getRequestURI (); // get the requested path
//requestURI.contains("login.jsp ");
// contanins: comprising determining, it is determined whether the path of the include path requestURI brackets, If there returns true, if not return to false
Boolean = Tag to false;// use the tag value determines whether this request intercepting
// use enhanced for loop of STRs [] traversal
for (String STR: STRs) {
IF (request.contains (STR)) {
Tag = to true;
BREAK; // Tag when true, the out of the loop, since it has been matching the resources
}
}
// determined by knockdown of the tag to see if the request
iF (tag) {
the chain.doFilter (request, Response);
} {the else
Response.sendRedirect ( "/ lizzhen626 / views / the login.jsp (item registration screen path) ");
}
}
}
public void the init (the FilterConfig config) throws ServletException {
System.out.println (" filter open ............ ..... ...... ");
}
}