Hi, Just curious, but I needed help in this topic, Can this requirement be achieved... The Servlet specification allows inclusion of another servlet/html page via the RequestDispatcher, but the spec clearly states that the new resource has to be within the scope of the ServletContext (page 54 of Servlet spec 2.3). Even the JSP spec mentions the same thing (Page 71 of JSP spec 1.2). Now how can we include/access resources outside the current ServletContext, e.g I have a page, that has to include contents of another site/URL in a frame, within the current page. (This can be probably achieved via browser-pull --specifying the browser to retrieve the page), but how do we do it at the server, so that the browser effectively sees it as one response from my servlet/JSP? Would really appreciate if somebody gives me some help on this...... Thanks, Ramesh gutty
I can think of a few ways of accomplishing this. The easiest is just to use frames in the browser window and get the remote page to load in one of the windows. Not my favourite. You could write a special jsp (better as a servlet) that accepts the remote location of the page to include, opens it then streams it to the client. Nasty but it'd work and be an interesting exersize to get working. The reason this is not realy possible for remote pages is that servlets (and hence JSPs) execute within a servlet container. The container is supposed to decide what the servlet can do and what it can see etc. When you call RequestDispatcher you are in effect creating a version of the container around the new code and getting ready to fire it off. You aren't allowed to control the execution of code on remote machines therefore you can't create a RequestDispatcher there. I hope I haven't been too verbose here, but basically it'll work in frames fine, it can be done via a streaming servlet with some work and possibly some parsing, not sure how to do it otherwise. Dave.
javax.servlet.ServletContext has a getContext(String uripath) method that allows servlets (quoting the javadoc here) "... to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher objects from the context. The given path must be absolute (beginning with '/') and is interpreted based on the server's document root."
Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Ramesh, do not forget that a frameset consists of multiple requests and responses in the first place, and as far as I understand browsers tend to open multiple connections for to retrieve the frames, so browser-pull should not really add any significant overhead. You still don't want to do that, yet the page you need to include runs on a different server so you can't use Phil Hanna's technique? Then you could create a servlet which uses a java.net.URL/URLConnection to retrieve the included page like any other HTTP client would, and send it off as a response. It won't be any faster (unless the servlet implements a caching mechanism). But the client would receive all its pages from your server. - Peter
Joined: Apr 24, 2001
Thanks Dave, Peter, Phil Thanks very much for your support. Dave, it really seems like opening a URL Connection is the only way for a servlet to access resources from some other web server. But as Peter has pointed out, this does need some caching mechanism to make it faster. This does answer my question. Thanks a lot guys, for your help.. Cheers to JavaRanch! Ramesh gutty