Servlet中FilterConfig的使用

737 阅读2分钟

原文链接:www.yiidian.com/servlet/fil…

Web容器创建FilterConfig的对象。该对象可用于从web.xml文件获取Filter的配置信息。

1 FilterConfig接口的方法

FilterConfig接口中有以下4个方法:

  • public void init(FilterConfig config):仅在用于初始化过滤器时才调用init()方法。
  • public String getInitParameter(String parameterName):返回指定参数名称的参数值。
  • public java.util.Enumeration getInitParameterNames():返回包含所有参数名称的枚举。
  • public ServletContext getServletContext():返回ServletContext对象。

2 FilterConfig的案例

在下面的示例中,流程大致是:MyFilter过滤器会读取param-value的值,如果将param-value是no,则请求将被转发到IndexServlet,如果是yes则提示信息:“该页面正在建设中”。

2.1 编写页面

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>一点教程网-FilterConfig的使用</title>
    <meta http-equiv="content-type" content="text/html" charset="UTF-8">
</head>
<body>
<a href="servlet1">点击这里</a>
</body>
</html>

2.2 编写IndexServlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * 一点教程网 - http://www.yiidian.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>欢迎访问一点教程网<br>");
    }

}

2.3 编写MyFilter

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *一点教程网 - http://www.yiidian.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("该页面正在建设中");
        }
        else{
            chain.doFilter(req, resp);//放行请求
        }

    }

    public void destroy() {

    }
}

2.4 配置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的参数-->
        <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 运行测试

file

如果param-value的值为no,显示如下:

file

如果param-value值为yes,显示如下:

file

欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。 如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com