aspose file tools*
The moose likes Servlets and the fly likes forwarding to another servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "forwarding to another servlet" Watch "forwarding to another servlet" New topic
Author

forwarding to another servlet

rak shar
Greenhorn

Joined: Dec 06, 2002
Posts: 9
Hi
Suppose i have a servlet in one Context (1 jvm) and i want to forward the request to another servlet in another context(different JVM), what to choose from
sendRedirect or RequestDispatcher.forward
Rgrds
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
RequestDispatcher.forward works for same context. You cant forward to different context.
you should use sendRedirect().
It first sends request to browser that document is moved to diffrent location.


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Ravish,
I think you can get around this issue by getting a reference to the other target ServletContext with this.getServletContext.getContext(uripath). Then using this new target ServletContext you can get a RequestDispatcher and you should then be set.
Bob Kerfoot
SCJP 1.2
SCWCD
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

One caveat though
A container may reject this.getServletContext.getContext(uripath)for security reasons.
In a security conscious environment, the servlet container may return null
for a given URL.


Groovy
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Pradeep,
You are correct. In the server.xml file you would need to ensure that the crossContext attribute was set to true in the context element for the application you were trying to connect to. If network traffic to and from your server was an issue it might be better not to use the sendRedirect() as you now have two request, response round trips instead of the server centric forward which handles the routing without going back to the client.
Bob Kerfoot
SCJP 1.2
SCWCD
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Bob,
Which app server are you talking about?
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Tomcat 4.1.24
Bob Kerfoot
SCJP 1.2
SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forwarding to another servlet
 
Similar Threads
Servletcontext
Defaul ServletContext ...??? Question
question about distributable web applications
Setting Context Attribute in Clustered Environment
Accessing servlet