• 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

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't quite understand this statement in HFSJ

Depending on the class whose method creates a RequestDispatcher, the path to the resource to be forwarded to will change



Can someone explain this please...?

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not 100% sure your book refers to this, but you have basically two ways to get a RequestDispatcher :
  • From javax.servlet.ServletRequest
  • From javax.servlet.ServletContext

  • The path used for each has a different meaning. Check both API, especially ServletRequest#getRequestDispatcher.
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh thanks! I wasn't aware of a getRequestDispatcher(path) in ServletContext.

    I find that the getRequestDispatcher(path) in ServletContext cannot have relative paths(makes sense) and that it can be used with getContext() to forward to a resource in a different web-app in the server! This is interesting stuff!
     
    Ranch Hand
    Posts: 56
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think we can also give relative path in requestDispatcher(path). Please correct me if I am wrong.
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi namita,

    There are two ways to get a RequestDispatcher...

    One is using a ServletRequest and the other using a ServletContext.
    When you use a ServletRequest yes, you can specify a relative path in request.getRequestDispatcher(path)

    Whereas when you use the servletContext to get a RequestDispatcher the path should start with a root...
    servletContext.getRequestDispatcher("/resource2.htm"); We can't use relative paths here.
     
    namita sasa
    Ranch Hand
    Posts: 56
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank for clarifying the doubt.
     
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is one more way to get the request dispatcher- using getNamedDispatcher.
    Can we use the relative path in this requestDispatcher?
    Can anyone tell - what are the appropriate places for all these dispatchers.
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    getNamedDispatcher(String servletName)

    can help when you need to forward to servlets that are not directly available to the users. i.e to the servlets that do not have a <url-mapping>.

     
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Vishwanath Murthi wrote:getNamedDispatcher(String servletName)

    can help when you need to forward to servlets that are not directly available to the users. i.e to the servlets that do not have a <url-mapping>.




    Hi, every servlet created has a <url-mapping> tag in the web.xml.
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    bhavuk soni wrote:
    Hi, every servlet created has a <url-mapping> tag in the web.xml.



    I don't think its mandatory. esp, when you don't want that servlet to be accessed directly by the user...

    Please google "getNamedDispatcher() examples" and check p.118 and p.119 in first result "the Sams Teach yourself JSP..." Books Preview..
    Sorry, adding the link did not work.

     
    Ranch Hand
    Posts: 664
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How come links aren't working???
     
    Narendhiran Nagarajan
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for all your replies.....
     
    knowledge is the difference between drudgery and strategic action -- tiny ad
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic