Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

forwarding to another servlet

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomcat 4.1.24
Bob Kerfoot
SCJP 1.2
SCWCD
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic