How do I redirect from a servlet in one context to a servlet in another? Specifically I'd like to know how to share objects between contexts.

Michael Dean

If you're trying to do a redirection (i.e. response.sendRedirect(String)), it requires an absolute URI (including the context path). If you don't know the context path, you're out of luck. :(

If you're trying to forward, you need to get a ServletContext reference pointing to the other context, then get a RequestDispatcher from that other context. Once again, you'll need to know the context path.

To get a reference pointing to the other context, just call getContext(String uripath) on the ServletContext reference for your context. The uripath should be a String specifying the context path of another web application in the container.

Note, however, that in many cases, you will receive null references--even if you specify a valid context path. In most servlet containers, a context (web application) must explicitly allow other contexts access to its ServletContext. For example, in Tomcat, the crossContext attribute of the Context element of server.xml must be set to true to allow other contexts to access this context. The Servlet specification recommends this behavior for "security conscious" environments.

See also: