Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forwarding to another servlet

 
rak shar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Kerfoot
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Kerfoot
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,
Which app server are you talking about?
 
Bob Kerfoot
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat 4.1.24
Bob Kerfoot
SCJP 1.2
SCWCD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic