As title metioned, How to invoke another servlet in a servlet and get the invoked servlet response?
4 Answers
Use RequestDispatcher
instance which is available through the HttpServletRequest
instance.
However, if you're looking at getting hold of the single instance held by the servlet container [like using the getServlet
method in the ServletContext
instance], it's an entirely different story. The servlet specs have purposefully deprecated the operation which might allow such an option. But, if you really want to invoke one servlet while executing the other one, use the include
method of the RequestDispatcher
instead of the forward
method.

- 4,797
- 4
- 32
- 42
getServletContext().getNamedRequestDispatcher("servletName")
.forward(request, response);
However I'd assume there are better options. For example move the code you need to a helper class / utility method, and invoke it.
As I come to think of it, you may want another thing: call a servlet separately. For that you need:
InputStream is = new URL(urlOfTheServlet).openStream();
IOUtils.copy(is, response.getOutputStream());
(this is using apache commons-io to copy the input stream to the output stream of the current request)

- 588,226
- 146
- 1,060
- 1,140
Use ServletContext or current request to get RequestDispatcher, and then use RequestDispatcher forward() or include().
Can use Spring MockHttpServletRequest and MockHttpServletResponse to create new request and response instead of use current request.
Example:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
servletRequest.setServerName(request.getServerName() );
servletRequest.setServerPort(request.getServerPort() );
servletRequest.setSession(request.getSession() );
servletRequest.setMethod(HttpMethod.GET.name() );
servletRequest.setRequestURI(url);
servletRequest.setParameters(parameters);
MockHttpServletResponse servletResponse = new MockHttpServletResponse();
servletResponse.setCharacterEncoding("UTF-8");
// Use include() instead of forward(). Similar as client HttpClient GET
dispatcher.include(servletRequest, servletResponse);
String content = servletResponse.getContentAsString();

- 1,101
- 1
- 9
- 7
String destinationBlockAccount ="./BlockAccount";
response.sendRedirect(response.encodeRedirectURL(destinationBlockAccount));
Furthermore you can sending a parameter like directly from JSP:
response.sendRedirect(response.encodeRedirectURL("./GetAccount?accountID="+accountID));

- 8,146
- 10
- 35
- 40
-
Please add an explanation to your code. – Jason Roman Feb 27 '16 at 18:49
-
It means that you have a servlet named BlockAccount with URL pattern /BlockAccount. Then we can redirect to this servlet. see http://stackoverflow.com/questions/20371220/what-is-the-difference-between-response-sendredirect-and-request-getrequestdis – Feb 27 '16 at 20:27
-
and https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html – Feb 27 '16 at 20:28