• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher - best to get it from request or from context?

 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that ServletContext.getRequestDispatcher() takes a path relative to the application root, and ServletRequest.getRequestDispatcher() takes a path relative to the current requesting object. Other than that, they work exactly the same. Why do we need both?

Is this just something that changed as Java has matured and both methods were left in for backward compatibility or are there circumstances where one choice is better than the other?

Most of the code I see uses the request method, so I'm guessing this is probably the older/original way of doing things and the context method was added later for some reason?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just a mater of preference. I always use the method on the request.
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jk Robbins wrote:Most of the code I see uses the request method, so I'm guessing this is probably the older/original way of doing things and the context method was added later for some reason?



I suspect the biggest reason you see the ServletRequest version used is that you already have a reference to the ServletRequest object in your do* method, so it's just easier to do request.getRequestDispatcher(...) vs getServletContext().getRequestDispatcher(...).

I've personally never called the ServletContext method, nor have I ever used a relative path.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only time the context version is required is for making cross-context dispatching, which is not supported by many containers.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you. That shines some light on things and satisfies my curiosity.
 
reply
    Bookmark Topic Watch Topic
  • New Topic