Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from servlet to another servlet using RequestDispatcher

I am trying to pass data from one servlet to another using the RequestDispatcher. This is my code for the Dispatcher.

String address;

address = "/Java Resources/src/coreservlets/MapOut.java";

RequestDispatcher dispatcher =
  request.getRequestDispatcher(address);
dispatcher.forward(request, response);

When I try to run it, it gives me an error saying the path is unavailable. Do I have to include something for the dispatcher to send to another servlet?

like image 868
stacktraceyo Avatar asked Jul 12 '12 17:07

stacktraceyo


2 Answers

You just need to pass servlet-mapping 's url-pattern in the getRequestDispatcher.

Let say your servlet mapping is "myMap" for the "MapOut" Servlet in the web.xml.Then it should be

RequestDispatcher dispatcher = request.getRequestDispatcher("/myMap");
dispatcher.forward(request,response);

doGet() of forwarded Servlet will be called.

Example: web.xml

      <servlet>
        <description></description>
        <servlet-name>MapOut</servlet-name>
        <servlet-class>coreservlets.MapOut</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>MapOut</servlet-name>
        <url-pattern>/myMap</url-pattern> <!-- You can change this-->
      </servlet-mapping>
like image 75
Hardik Mishra Avatar answered Sep 28 '22 23:09

Hardik Mishra


You can directly write your name of the servlet in request.getRequestDispatcher("your servlet name"); it will fetch the path according to the web.xml configuration.

RequestDispatcher rd= request.getRequestDispatcher("MyServletName");
rd.forward(request,response);
like image 22
Mitul Maheshwari Avatar answered Sep 29 '22 00:09

Mitul Maheshwari