我有一个这样的servlet:
@WebServlet("/a/path")
@WebInitParam(name="name", value="name_value")
public class MyServlet extends HttpServlet {
//...
在这个 servlet 上,我放置了一个过滤器:
@WebFilter(dispatcherTypes = { DispatcherType.REQUEST }, urlPatterns = { "/a/*" })
public class MyFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//...
HttpServletRequest req = (HttpServletRequest)request;
//problem comes here
System.out.println(req.getServletContext().getInitParameter("name"));
//...
}
问题是,即使我在 MyServlet 中设置了 @WebInitParameter ,程序也会打印出一个空字符串(请参阅注释行//problem come here in MyFilter)。我验证并看到servlet 的init()方法在doFilter()之前执行。那么任何人都可以在这个问题上照亮我吗?为什么initParameter “name” 为空,如果它设置为一个值?
谢谢!