我需要转发我的请求(对于jsp,但我认为这不重要)从一个http.Filter 如果原始请求的URI通过一些验证,我的过滤器运行。转发请求从一个过滤器
我发现这个page that faced similar task
我仍然需要计算如下:
我怎样才能获得的ServletContext在
doFilter()
方法(以呼叫转移API)getServletContext()
不recignized在转发之前还是转发之前,我还必须
call chain.doFilter()
? 另外,如果我的验证通过了,或者只有在验证失败的情况下(因为在这种情况下我不会继续转发我的页面),我还必须拨打chain.doFilter()
吗?
这个问题实际上是继续this thread, 更加明显,代码可能是这样的:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = ((HttpServletRequest)request);
String requestURI = httpServletRequest.getRequestURI();
String contextPath = httpServletRequest.getContextPath();
if (<this is my implementation of the validation of this filter>){
getServletContext().getRequestDispatcher(
"MySpecific.jsp").forward(request,response);
}
}
chain.doFilter(request,response);
}
这两个答案都很好。另一个是第一个。 – Spiderman 2010-08-09 07:01:32
在初始化过程中关闭FilterConfig并调用FilterConfig.getServletContext()这是我首选的方法,因为没有明确的会话: – Gurnard 2012-11-07 13:36:10