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

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日

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。