Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward request from a filter

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:

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

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

    }
like image 762
Spiderman Avatar asked Aug 05 '10 13:08

Spiderman


People also ask

What is difference between forward and redirect?

Redirection is a type of response sent back to the browser to instruct it to fetch another page. The URL in the browser address bar will change here. Forwarding happens server-side, and the result of the forward action is returned to the browser.

What is filter request?

A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource.

What does Chain doFilter request response do?

doFilter. Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked. Parameters: request - the request to pass along the chain.


2 Answers

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.

like image 198
Péter Török Avatar answered Oct 20 '22 05:10

Péter Török


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

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

  • call HttpServletRequest.getSession().getServletContext()

I don't think you necessarily need the ServletContext to get the RequestDispatcher as you could just call 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.

like image 11
jridley Avatar answered Oct 20 '22 03:10

jridley