Original link: http://www.yiidian.com/servlet/filter-config.html
The Web container creates an object for FilterConfig. This object can be used to get the configuration information of the Filter from the web.xml file.
1. Method of filterconfig interface
There are four methods in the FilterConfig interface:
- public void init (FilterConfig config): the init() method is called only when used to initialize the filter.
- public String getInitParameter (String parameterName): returns the parameter value of the specified parameter name.
- public java.util.Enumeration getInitParameterNames(): returns an enumeration containing all parameter names.
- public ServletContext getServletContext(): returns the ServletContext object.
2 case of filterconfig
In the following example, the process is roughly as follows: MyFilter will read the value of param value, if param value is no, the request will be forwarded to IndexServlet, if yes, the message will be prompted: "the page is under construction".
2.1 preparation page
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <title>A little tutorial network-FilterConfig Use of</title> <meta http-equiv="content-type" content="text/html" charset="UTF-8"> </head> <body> <a href="servlet1">click here </a> </body> </html>
2.2 writing IndexServlet
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * A little tutorial website - http://www.yidian.com */ public class IndexServlet extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html;charset=utf-8"); PrintWriter out = res.getWriter(); out.print("<br>Welcome to the tutorial website<br>"); } }
2.3 write MyFilter
import javax.servlet.*; import java.io.IOException; import java.io.PrintWriter; /** *A little tutorial website - http://www.yidian.com */ public class MyFilter implements Filter { FilterConfig config; public void init(FilterConfig config) throws ServletException { this.config=config; } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out=resp.getWriter(); String s=config.getInitParameter("construction"); if(s.equals("yes")){ out.print("This page is under construction"); } else{ chain.doFilter(req, resp);//Release request } } public void destroy() { } }
2.4 configure web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>IndexServlet</servlet-name> <servlet-class>IndexServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>IndexServlet</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <filter> <filter-name>MyFilter</filter-name> <filter-class>MyFilter</filter-class> <!--Filter Parameters of--> <init-param> <param-name>construction</param-name> <param-value>no</param-value> </init-param> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/servlet1</url-pattern> </filter-mapping> </web-app>
2.5 operation test
If the value of param value is no, it is displayed as follows:
If the param value value is yes, the display is as follows:
Welcome to my official account: a little tutorial. Get exclusive learning resources and daily dry goods push. If you are interested in my series of tutorials, you can also follow my website: yiidian.com