RequestDispatcher and usage of forward slash

H

harryos

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
 
D

Daniel Pitts

harryos said:
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 :)
 
L

Lew

Daniel said:
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!):
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top