Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequestDispatcher forward between Tomcat instances

I have a scenario where I have single entry point Servlet and further Servlets that requests are forwarded to that undertake heavy processing.

I am looking at options to distribute this load and I would like to know if it is possible using Tomcat or another platform to forward requests between Servlets sitting on different servers using a cluster type configuration or similar.

I have found some documentation on clustering Servlets and Tomcat but none indicate if Servlet request forwarding is possible from what I can see.

http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html

http://tomcat.apache.org/tomcat-5.5-doc/cluster-howto.html

like image 655
Tobias M Avatar asked May 12 '10 00:05

Tobias M


People also ask

What is the use of forward () of RequestDispatcher?

forward. Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

What is the difference between RequestDispatcher forward () and include () method?

Junior developers often get confused between the include and the forward methods of the RequestDispatcher. The key difference between the two is the fact that the forward method will close the output stream after it has been invoked, whereas the include method leaves the output stream open.

What are the two methods of RequestDispatcher class?

The RequestDispatcher interface provides the option of dispatching the client's request to another web resource, which could be an HTML page, another servlet, JSP etc. It provides the following two methods: public void forward(ServletRequest request, ServletResponse response)throws ServletException, java. io.


1 Answers

You could distribute it over webapps in a clustered Tomcat environment and add crossContext="true" to the <Context> element of the webapps in question. Here's an extract of Tomcat's Context Configuration Reference:

crossContext

Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null.

This way you can obtain the desired RequestDispatcher as follows:

RequestDispatcher dispatcher = getServletContext().getContext(name).getRequestDispatcher(path);
like image 174
BalusC Avatar answered Oct 09 '22 11:10

BalusC