Transferring data from a servlet to another servlet using RequestDispatcher

I am trying to transfer data from one servlet to another using 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 start it, it gives me a message stating that the path is not available. Should I include something for the dispatcher to send to another servlet?

+6
source share
2 answers

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

Let's say your servlet mapping is "myMap" for the "MapOut" servlet in web.xml . Then he must be

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

doGet() redirected 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> 
+12
source

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

 RequestDispatcher rd= request.getRequestDispatcher("MyServletName"); rd.forward(request,response); 
+1
source

Source: https://habr.com/ru/post/920350/


All Articles