In servlet we can use requestdispatcher and it will forward from one servlet to another servlet with same session in same project.but if i am using different project getRequestDispatcher is not working.its giving 404 error.because its appending servlet name before the url. how can i achieve getRequestDispatcher in different project in same server?
RequestDispatcher rd = request.getRequestDispatcher("/v1/status1/toreply1");
rd.forward(request, response);
In same project getRequestDispatcher working fine. but in different project its not working ?can anyone explain why its happening ..?
You can achieve getRequestDispatcher in different project on same server.
Check out https://stackoverflow.com/a/19273223/1428052
It works on same server for different project by using getServletContext().getContext() method.
You can follow below steps for detail implementation.
First you need to make changes in below file
(Windows) C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\context.xml Set value of crossContext to true.
context.xml
Please note that crossContext="true".
Suppose you have two web applications with name InterServletComm1 and InterServletComm2 having servlets Servlet1 and Servlet1 in each web application respectively. Then the code in each servlets goes as follows:
Servlet1.java
Servlet2.java
Above code sends attribute name from InterServletComm1 and it is received in InterServletComm2. Please let me know if this answer is not clear.
You have to use the
ServletContext
of the other web application to get theRequestDispatcher
. The other context can be looked up byServletContext#getContext(String uri)
: