Using the Request Dispatcher


Web tier: servlets, JSP, Web frameworks: Using the Request Dispatcher

  1. Using the Request Dispatcher (4 messages)

    When I use the Request Dispatcher from within a JSP file, i cam not able to see any of the response written by the called servlet. Should I use any methds to extract the data, or will it automatically be included in the output? Thanks

    Threaded Messages (4)

  2. Using the Request Dispatcher[ Go to top ]

    Why are you using this interface within a JSP ?
    It is usually implemented within a servlet to perform either a "forward" or "include" of another URL.
    Maybe what you need is to use a getHeader method of request:

    String referrer = (String)request.getHeader("referer");

    this provides the URI of the caller of the current JSP/servlet.
  3. Using the Request Dispatcher[ Go to top ]

    Usha, you should post the text of your call to the Request Dispatcher so that we can see what you're trying to do. In any case, if what you literally want to do is dynamically include the content generated by a servlet in your JSP, try using the <jsp:include .../> action tag.


  4. Using the Request Dispatcher[ Go to top ]

    Thanks guys. I tried jsp:include, but that doesnt work either
    the code is like this:
          RequestDispatcher req=application.getRequestDispatcher("/classes/ProcessServlet");
          request.setAttribute("query",new String("select * from tab"));
          request.setAttribute("query_type",new String("query"));
          if (req!=null)
                 req.include(request, response);
                  out.println("Error: Request Dispatcher returned a null");

    It doesnt return a null, but doesnt print anything from my servlet ProcessServlet either
  5. Using the Request Dispatcher[ Go to top ]

    Usha, I wrote a small example for myself to see if the request dispatcher include would work, and it seems to work fine. I also tested <jsp:include> and that worked too. I had one difference from you, however: my argument to the request dispatcher specifies a "/servlet/..." prefix, for instance:

    RequestDispatcher rq = application.getRequestDispatcher("/servlet/com.whatever.SomeServlet");

    and the jsp:include looks like:

    <jsp:include page="/servlet/com.whatever.SomeServlet" flush="true"/>

    Beyond that, test your servlet to make sure it's actually generating output.