Re: open an external page from a servlet
steve.chernyak@gmail.com wrote:
Try:
response.sendRedirect("http://www.abcde.com/foo.html");
instead of using the dispatcher
The REDIRECT is serving its design purpose if you send the browser to another
server. It makes no sense to forward() the request and response objects
outside the current app server.
You can still use the dispatcher if the other app is on the same server. It
runs server-side, not client-side, so you can keep request attributes that
would otherwise need to be session-level.
In the Javadocs for ServletRequest.getRequestDispatcher()
<http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)>
"The difference between this method and
ServletContext.getRequestDispatcher(java.lang.String) is that this method can
take a relative path."
So we look up ServletContext.getRequestDispatcher(java.lang.String)
<http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRequestDispatcher(java.lang.String)>
"The pathname must begin with a "/" and is interpreted as relative to the
current context root. Use getContext to obtain a RequestDispatcher for
resources in foreign contexts."
Ok, look up getContext(), also a method of ServletContext
<http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getContext(java.lang.String)>
"Returns a ServletContext object that corresponds to a specified URL on the
[same] server."
So, assuming no null returns or Exceptions, you can use
getServletContext().getContext( "/anotherAppOnSameServer" )
.getRequestDispatcher( "/path/within/the/app" )
.forward( request, response );
- Lew