情深忆往缠绵 2022-07-01 14:38 采纳率: 44.6%
浏览 183
已结题

Filter过滤器无法获取到值

问题遇到的现象和发生背景

我看书上和网上使用filterConfig.getInitParameter()都能获取到值,为什么我获取不到

问题相关代码,请勿粘贴截图

Filter:


package test1;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/B2")
public class B2 implements Filter{
    
    @Override
    public void destroy() {
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        //打印任意内容测试
        System.out.println("使用doFilter()方法");
        //将请求传回过滤链
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //获取初始化参数
        String value1 = filterConfig.getInitParameter("value");
        //输出初始化参数
        System.out.println("获取值为: " + value1);
    }

}


servlet:(或许用不到,都放这把)

package test1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/B1")
/**
 * Servlet implementation class B1
 */
public class B1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public B1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String)e.nextElement();
            out.print(name + "<br/>");
            String value = request.getHeader(name);
            out.print(value + "<br/>");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}


xml:

    <servlet>
        <servlet-name>B1</servlet-name>
        <servlet-class>test1.B1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>B1</servlet-name>
        <url-pattern>/test1/B1</url-pattern>
    </servlet-mapping>
    
    <filter>
        <filter-name>B2</filter-name>
        <filter-class>test1.B2</filter-class>
        <init-param>
            <param-name>value</param-name>
            <param-value>value</param-value>
        </init-param>
</filter>
    <filter-mapping>
        <filter-name>B2</filter-name>
        <url-pattern>/test1/B2</url-pattern>
    </filter-mapping>
运行结果及报错内容

img

我的解答思路和尝试过的方法

其它没什么问题,就是获取不到值,不知道咋回事

  • 写回答

1条回答 默认 最新

  • 皮蛋不吃粥 2022-07-01 14:44
    关注

    @WebFilter 注解里面加一个 initParams = { @WebInitParam(name = "value", value = "value")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月10日
  • 已采纳回答 7月2日
  • 创建了问题 7月1日

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方