Java 转发来自过滤器的请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3415240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:07:29  来源:igfitidea点击:

Forward request from a filter

javajakarta-eefilter

提问by Spiderman

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.

我需要转发我的要求(到JSP,但我不认为它的物质)从http.Filter如果原请求的URI通过一些验证,我的过滤器运行。

I found this page that faced similar task

我发现这个页面面临类似的任务

Still I need to figure the following:

我仍然需要弄清楚以下几点:

  1. How can I get ServletContext in doFilter()method (in order to call forward API) getServletContext()is not recignized

  2. 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)?

  1. 如何在doFilter()方法中获取 ServletContext (为了调用转发 API) getServletContext()不被认可

  2. 我必须call chain.doFilter()在转发之前,转发之后还是根本不需要?此外,chain.doFilter()如果我的验证通过或仅在验证失败时我是否必须调用(因为在这种情况下我不会继续转发我的页面)?

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);

    }

采纳答案by Péter T?r?k

How can I get ServletContext in doFilter() method?

如何在 doFilter() 方法中获取 ServletContext?

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)?

我是否必须在转发之前,转发之后或根本不调用 chain.doFilter() ?此外,如果我的验证通过或仅在验证失败时(因为在这种情况下我不会继续转发我的页面),我是否必须调用 chain.doFilter() ?

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.

我想说的是,如果您转发了请求,则不应调用chain.doFilter()- 转发的请求将根据其自己的过滤器配置进行过滤。如果您的验证失败,则取决于您的 Web 应用程序的语义是什么 - 如果原始页面是某种常规错误/登录/欢迎屏幕,则您可能希望在验证失败时继续该页面。在不了解更多上下文的情况下很难说。

回答by jridley

To get the ServletContext, you've got 2 options:

要获得 ServletContext,您有 2 个选择:

  • Store off the FilterConfigduring the initialization and call FilterConfig.getServletContext()

  • call HttpServletRequest.getSession().getServletContext()

  • FilterConfig在初始化和调用期间存储关闭FilterConfig.getServletContext()

  • 称呼 HttpServletRequest.getSession().getServletContext()

I don't think you necessarily need the ServletContextto get the RequestDispatcheras you could just call HttpServletRequest.getRequestDispatcher().

我认为您不一定需要ServletContext获取 ,RequestDispatcher因为您可以调用HttpServletRequest.getRequestDispatcher().

In relation to FilterChain.doFilter()call, if you're forwarding, I would think you wouldn't make the call, as once you forward, I assume you don't want any of the standard behavior to take place. If you don't forward (you don't fall into your if block), then I'd call the FilterChain.doFilter()method, however that assumes there is a target on the other end to be invoked.

关于FilterChain.doFilter()呼叫,如果您要转接,我认为您不会拨打电话,因为一旦您转接,我假设您不希望发生任何标准行为。如果你不转发(你不会陷入你的 if 块),那么我会调用该FilterChain.doFilter()方法,但是假设另一端有一个目标被调用。