Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher

 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • 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...?

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
    • 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!
     
    namita sasa
    Ranch Hand
    Posts: 56
    • Mark post as helpful
    • send pies
    • 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
    • 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
    • Quote
    • Report post to moderator
    Thank for clarifying the doubt.
     
    Narendhiran Nagarajan
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • 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
    • 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>.

     
    bhavuk soni
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • 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
    • 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.

     
    Nabila Mohammad
    Ranch Hand
    Posts: 664
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How come links aren't working???
     
    Narendhiran Nagarajan
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for all your replies.....
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic