Java Web Filter and javawebfilter
Filters are called filters or interceptors. The basic function is to intercept the process of calling servler. Some special functions are implemented before and after servlet response and processing. In fact, the Filter is a class that implements the javax. servlet. Filter interface. The javax. servlet. Filter interface defines three methods:
- Init (FilterConfig filterConfig): used to initialize the filter. The initialization function similar to the constructor can be completed in init (). If the FilterConfig object is used in the initialization code, the initialization code can only be written in the init () method of the Filter, but not in the constructor.
- DoFilter (ServletRequest request, ServletResponse response, FilterChain chain): request and response are the request and response objects passed by the previous Filter object in the Web server or Filter chain; the chain parameter represents the object of the Filter chain. The doFilter () method of the FilterChain object must be called in the doFilter () method of the Current Filter object, in order to deliver the request to the next Filter or target program of the Filter chain for processing.
- Public void destroy (): The destroy () method is called before the Web server uninstalls the Filter. This method is used to release resources opened by the Filter object, such as closing the database and IO streams.
1. Implement the first Filter program
The Index. java file is as follows:
1 package com.net; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 11 public class Index extends HttpServlet {12 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {13 PrintWriter out = response.getWriter();14 out.println("Index");15 }16 17 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {18 doGet(request, response);19 }20 }
The MyFilter. java file is as follows:
1 package com.net; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException;10 import javax.servlet.ServletRequest;11 import javax.servlet.ServletResponse;12 13 public class MyFilter implements Filter {14 public void init(FilterConfig fConfig) throws ServletException {15 System.out.println("MyFilter.init()");16 }17 18 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {19 PrintWriter out = response.getWriter();20 out.println("Hello MyFilter");21 chain.doFilter(request, response);22 }23 24 public void destroy() {25 System.out.println("MyFilter.destroy()");26 }27 }
Add the following code to the web. xml configuration file:
1 <filter> 2 <filter-name>MyFilter</filter-name> 3 <filter-class>com.net.MyFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>MyFilter</filter-name> 7 <url-pattern>/Index</url-pattern> 8 </filter-mapping> 9 10 11 <servlet>12 <servlet-name>Index</servlet-name>13 <servlet-class>com.net.Index</servlet-class>14 </servlet>15 <servlet-mapping>16 <servlet-name>Index</servlet-name>17 <url-pattern>/Index</url-pattern>18 </servlet-mapping>
The test results are as follows:
2. Filter chain
An example of the Filter chain is shown below:
The test program is as follows. For the Index. java file and MyFilter. java file, see the code in the first Filter program. The MyFilter2.java file and the web. xml configuration file are as follows:
The MyFilter2.java file is as follows:
1 package com.net; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException;10 import javax.servlet.ServletRequest;11 import javax.servlet.ServletResponse;12 13 public class MyFilter2 implements Filter {14 public void init(FilterConfig fConfig) throws ServletException {15 }16 17 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {18 PrintWriter out = response.getWriter();19 out.println("Hello MyFilter2 Start");20 chain.doFilter(request, response);21 out.println("Hello MyFilter2 End");22 }23 24 public void destroy() {25 }26 }
Add the following code to the web. xml configuration file:
<filter> <filter-name>MyFilter</filter-name> <filter-class>com.net.MyFilter</filter-class></filter><filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/Index</url-pattern></filter-mapping><filter> <filter-name>MyFilter2</filter-name> <filter-class>com.net.MyFilter2</filter-class></filter><filter-mapping> <filter-name>MyFilter2</filter-name> <url-pattern>/*</url-pattern></filter-mapping><servlet> <servlet-name>Index</servlet-name> <servlet-class>com.net.Index</servlet-class></servlet><servlet-mapping> <servlet-name>Index</servlet-name> <url-pattern>/Index</url-pattern></servlet-mapping>
The test results are as follows:
Note: What is the call sequence between filters? This is related to the order they declare in web. xml. For example, according to the preceding web. xml configuration, the Filter call sequence is as follows:
Request-> MyFilter-> MyFilter2-> Index (servlet)
Response <-MyFilter <-MyFilter2 <-Index (servlet)
3. lifecycle of a Filter
The web server is responsible for creating and destroying filters. When a web application is started, the web server will create a Filter instance object and call its init method to initialize the object, so as to prepare for interception of subsequent user requests, the filter object is created only once, and the init method is executed only once. Through the parameters of the init method, you can obtain the FilterConfig object that represents the current filter configuration information. The FilterConfig interface encapsulates all the registration information of the Filter program in web. xml, and provides some methods to obtain the configuration information, as shown below:
The MyFilter. java file is as follows:
Package com.net; import java. io. IOException; import java. io. printWriter; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; public class MyFilter implements Filter {public void init (FilterConfig fConfig) throws ServletException {System. out. println ("MyFilter. init () "); // get the name of the filter String filterName = fConfig. getFilterName (); // get in web. string initParam = fConfig. getInitParameter ("like"); System. out. println (filterName); System. out. println (initParam);} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {PrintWriter out = response. getWriter (); out. println ("Hello MyFilter"); chain. doFilter (request, response);} public void destroy () {System. out. println ("MyFilter. destroy ()");}}
The web. xml configuration items for MyFilter. java are as follows:
<filter> <filter-name>MyFilter</filter-name> <filter-class>com.net.MyFilter</filter-class> <init-param> <param-name>like</param-name> <param-value>java</param-value> </init-param></filter><filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/Index</url-pattern></filter-mapping>