Servlet Filter not working for Filter on RequestDispatcher

Discussions

Web tier: servlets, JSP, Web frameworks: Servlet Filter not working for Filter on RequestDispatcher

  1. Hi All,
    I have a Filer that was mapped to Servlet1 whcih has got URL/Servlet mapping too, In Servlet2 I am using RequestDispatcher to forward to Servlet1. In this case the Filter that was mapped to Servlet1 is not getting intercepted. Where as if I access Servlet1 from my browser the Filter is getting intercepted.

    Is this a Contraint with Filter or is there anything I will have to do?
    Here is my Code

    Servlet1

    public class VJServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html> <body>");
            out.println("<h1>Testing Filters</h1>");
            out.println("</body></html>");
        }
    }

    Servlet2

    public class VJServlet1 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html><body>");
            out.println("<h4>In Servlet2</h4>");
            out.println("</body></html>");
            RequestDispatcher dis = request.getRequestDispatcher("/servlet/MyTest");
            dis.forward(request,response);
        }
    }

    doFilter of My Filter Class FilterTest

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws java.io.IOException, ServletException {
            System.out.println("Entered VJFilterTest ......." );
           long before = System.currentTimeMillis();
           chain.doFilter( req, res );
           long after = System.currentTimeMillis();
            System.out.println("Elapsed Time: " + ( after - before ) + "ms" );
    System.out.println("Exiting VJFilterTest ......." );
        }
    }

    WEB.XML
    <filter>
      <filter-name>VJFilterTest</filter-name>
      <filter-class>FilterTest</filter-class>
    </filter>

    <filter-mapping>
      <filter-name>VJFilterTest</filter-name>
      <!--<servlet-name>Servlet1</servlet-name>-->
      <url-pattern>/servlet/MyTest</url-pattern>
    </filter-mapping>


        <servlet>
            <servlet-name>Servlet1</servlet-name>
            <servlet-class>Servlet1</servlet-class>
        </servlet>
        
        <servlet>
            <servlet-name>Servlet2</servlet-name>
            <servlet-class>Servlet2</servlet-class>
        </servlet>


        <servlet-mapping>
            <servlet-name>Servlet1</servlet-name>
            <url-pattern>/servlet/MyTest</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>Servlet2</servlet-name>
            <url-pattern>/servlet/MyFilterTest</url-pattern>
        </servlet-mapping>


    Thanks in Advance,
    Gautham.

    Threaded Messages (2)

  2. I got the ANS - Thanks[ Go to top ]

    I had to add
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>

    to My Filter mapping, which now looks like

    <filter-mapping>
      <filter-name>VJFilterTest</filter-name>
      <!--<servlet-name>VJServlet</servlet-name>-->
      <url-pattern>/servlet/MyTest</url-pattern>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
  3. Re: I got the ANS - Thanks[ Go to top ]

    Thnx! had the same problem