Friday, March 14, 2008

Servlet fordwarding within and out of a Web Application

To redirect in a servlet, you get the servlet context object, get the requestDispatcher, and then run the forward method on it. This is seamless to the web browser. All data gets forwarded to the servlet. Like this:


RequestDispatcher rd = request.getRequestDispatcher("/testfiles/test.jsp");
rd.forward(request, response);

The trouble is that this doesn't work if you want to forward outside of the application. For that, you have to get the context of the other web application first.


 RequestDispatcher rd = getServletContext().getContext("/otherapp").getRequestDispatcher("/testfiles/test.jsp");

I don't know how to forward to another server entirely.

1 comment:

Anonymous said...

Thanks for writing this.

Labels

Blog Archive

Contributors