转发来自过滤器的请求 [英] Forward request from a filter
问题描述
如果原始请求的URI通过一些验证,我需要从http.Filter
转发请求(到一个jsp,但我认为这不重要)过滤器运行。
我发现这个面临类似任务的页面
我仍然需要计算以下内容:如何获得 doFilter()
方法中的ServletContext(为了call forward API) getServletContext()
不被重新分配
我是否需要在转发之前调用chain.doFilter()
,转发之前还是根本没有?
另外,如果我的验证通过了,或者只有失败了,我才需要调用 chain.doFilter()
(因为在这种情况下我不会继续前进我的网页)?
这个问题实际上是继续 this thread ,
更明显,代码可能类似于:
preublic $ doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException {$ b $ if(request instanceof HttpServletRequest){
HttpServletRequest httpServletRequest =((HttpServletRequest)请求);
String requestURI = httpServletRequest.getRequestURI();
String contextPath = httpServletRequest.getContextPath();
if(<这是我实现这个过滤器的验证>){
getServletContext()。getRequestDispatcher(
MySpecific.jsp)。forward(request,response);
$ b chain.doFilter(request,response);
$ b $ / code $ / pre
如何在doFilter()方法中获得ServletContext?
httpServletRequest。 。的getSession()的getServletContext();
我必须在转发之前调用chain.doFilter()在转发之前还是根本不?另外,如果我的验证通过了,或者只有失败了(因为在这种情况下我不会继续转发我的页面),我还需要调用chain.doFilter()吗?
我会说,如果你转发请求,你不应该调用 chain.doFilter()
- 转发的请求将被过滤到它自己的过滤器配置。如果你的验证失败,这取决于你的Web应用程序的语义是什么 - 如果原始页面是某种一般的错误/登录/欢迎屏幕,你可能想要继续,当验证失败。如果不了解更多情况,很难说。
I need to forward my request (to a jsp but I don't think it's matter) from an http.Filter if the URI of the original request pass some validation that my filter runs.
I found this page that faced similar task
Still I need to figure the following:
How can I get ServletContext in
doFilter()
method (in order to call forward API)getServletContext()
is not recignizedDo I have to
call chain.doFilter()
before the forward, after the forward or not at all? In addition do I have to callchain.doFilter()
if my validation passed or only if it fails (because in this case I won't continue to forward my page)?
This question actually continue this thread, to be more obvious, the code could be something like:
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);
}
How can I get ServletContext in doFilter() method?
httpServletRequest.getSession().getServletContext();
Do I have to call chain.doFilter() before the forward, after the forward or not at all? In addition do I have to call chain.doFilter() if my validation passed or only if it fails (because in this case I won't continue to forward my page)?
I would say that if you forwarded the request, you should not call chain.doFilter()
- the forwarded request will get filtered according to its own filter configuration. If your validation failed though, it depends on what the semantics of your web app are - if the original page is some sort of general error/login/welcome screen, you may want to continue to that when the validation failed. It is hard to say without knowing more of the context.
这篇关于转发来自过滤器的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!