Problem With RequestDispatcher !!!

Discussions

Web tier: servlets, JSP, Web frameworks: Problem With RequestDispatcher !!!

  1. Problem With RequestDispatcher !!! (2 messages)

    Hi,

    I am using

    ctx = getServletContext();
    RequestDispatcher rd = ctx.getRequestDispatcher("/Hello.jsp") ;
    rd.forward(req,res);

    in my application.But the problem is - for the first time it is displaying the Hello.jsp properly but again if I do some action on Hello.jsp Screen which needs the same screen to be displayed once again its giving ServletException.

    Most probably it holds the context when I redirect it for the first time and on the next try it tries to do it above that.So it is giving "Not Found Exception"

    Can any one help me with this ?

    Sabyasachi
  2. Try forwarding from the request rather than the servlet context:

    RequestDispatcher rd = request.getRequestDispatcher("/Hello.jsp");

    I've found that to be more reliable.
  3. Hi,

    A big difference exists between the getRequestDispatcher method of the ServletContext interface and that belonging to the ServletRequest interface. The one you use depends on the location of the resource to be included or forwarded to. If you use the getRequestDispatcher method of the javax.servlet.ServletContext interface, you pass a path that is relative to the root of the ServletContext. If you use the getRequestDispatcher method of the javax.servlet.ServletRequest interface, you pass a path that is relative to the current HTTP request.
    so you can use either
    RequestDispatcher rd = request.getRequestDispatcher("/Hello.jsp");
    or
    RequestDispatcher rd =
        getServletContext().getRequestDispatcher("/yourWebContext/Hello.jsp");

    Nabil BENMIRA