我需要轉發我的請求(對於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