问题遇到的现象和发生背景
我看书上和网上使用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>
运行结果及报错内容
我的解答思路和尝试过的方法
其它没什么问题,就是获取不到值,不知道咋回事