RequestDispatcher and usage of forward slash

Discussion in 'Java' started by harryos, Dec 16, 2008.

  1. harryos

    harryos Guest

    While trying out a tutorial about RequestDispatcher ,I came across
    some servlet code like this

    doGet(...) {
    ....
    RequestDispatcher dispatcher =request.getRequestDispatcher("/
    viewPostedData.jsp?"+querystr);
    dispatcher.forward(request,response);

    }

    When I tried this with
    request.getRequestDispatcher("viewPostedData.jsp?"+querystr);

    I found that the servlet invokes the viewPostedData.jsp with the given
    query string.So what was the need for a '/' in front of the name of
    the jsp file?

    Can somebody clarify this?
    thanks,
    harry
     
    harryos, Dec 16, 2008
    #1
    1. Advertising

  2. harryos

    Daniel Pitts Guest

    harryos wrote:
    > While trying out a tutorial about RequestDispatcher ,I came across
    > some servlet code like this
    >
    > doGet(...) {
    > ....
    > RequestDispatcher dispatcher =request.getRequestDispatcher("/
    > viewPostedData.jsp?"+querystr);
    > dispatcher.forward(request,response);
    >
    > }
    >
    > When I tried this with
    > request.getRequestDispatcher("viewPostedData.jsp?"+querystr);
    >
    > I found that the servlet invokes the viewPostedData.jsp with the given
    > query string.So what was the need for a '/' in front of the name of
    > the jsp file?
    >
    > Can somebody clarify this?
    > thanks,
    > harry
    >

    A leading "/" specifies that the path isn't relative to the current
    request. I believe if you *do* use /, then it is relative to the servlet
    context, but I could be wrong, I don't often mess with that level,
    leaving it happily abstracted by Spring :)

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Dec 16, 2008
    #2
    1. Advertising

  3. harryos

    Lew Guest

    harryos wrote:
    >> While trying out a tutorial about RequestDispatcher ,I came across
    >> some servlet code like this
    >>
    >> doGet(...) {
    >> ....
    >> RequestDispatcher dispatcher =request.getRequestDispatcher("/
    >> viewPostedData.jsp?"+querystr);
    >> dispatcher.forward(request,response);
    >>
    >> }
    >>
    >> When I tried this with
    >> request.getRequestDispatcher("viewPostedData.jsp?"+querystr);
    >>
    >> I found that the servlet invokes the viewPostedData.jsp with the given
    >> query string.So what was the need for a '/' in front of the name of
    >> the jsp file?
    >>
    >> Can somebody clarify this?


    Daniel Pitts wrote:
    > A leading "/" specifies that the path isn't relative to the current
    > request. I believe if you *do* use /, then it is relative to the servlet
    > context, but I could be wrong, I don't often mess with that level,
    > leaving it happily abstracted by Spring :)


    I decided to do something truly radical and look at the Javadocs for the
    method (gasp!):
    <http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)>
    wherein they tell us:
    > The pathname specified may be relative, although it cannot extend outside the
    > current servlet context. If the path begins with a "/" it is interpreted as
    > relative to the current context root.


    JAYF.

    I remain somewhat surprised that people don't look there first.

    --
    Lew
     
    Lew, Dec 17, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Will Handley
    Replies:
    1
    Views:
    590
    Nigel Wade
    Dec 10, 2003
  2. Harry
    Replies:
    1
    Views:
    788
    Viator
    Dec 22, 2005
  3. Jan van Mansum
    Replies:
    0
    Views:
    476
    Jan van Mansum
    Jul 23, 2006
  4. destroooooy
    Replies:
    4
    Views:
    668
    Arnaud Delobelle
    Apr 29, 2008
  5. Replies:
    0
    Views:
    587
Loading...

Share This Page