This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes forward() in servlets. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "forward() in servlets." Watch "forward() in servlets." New topic
Author

forward() in servlets.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Hi All,

I have a question suppose I am hitting a URL as given below:


http://localhost:8081/myapp/servlet/login.do

Now in this Login.do servlet I am using forward method to do that following:


I want to ask that the request dispatcher will forward on which URL?

http://localhost:8081/myapp/servlet/login.do

or
http://localhost:8081/myapp/servlet/servlet/login.do




Pawan Chopra
SCJP - DuMmIeS mInD
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
To answer correctly to your question it's important to know what is for you

/servlet/StartCISPage and how it is related to login.do

in few words is it possible to have a look at your deployment descriptor? (web.xml file)

Thank you.

Bye,
Nicola
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what you found, when you tried that?
Phani Raju
Greenhorn

Joined: Aug 03, 2007
Posts: 19
If the url starts with a "/" then it is relative to the host otherwise, it is relative to the page loaded. So the request dispatcher will forward to http://localhost:8081/servlet/StartCISPage

If the request dispatcher is created without a leading slash like "servlet/StartCISPage", then it would going to http://localhost:8081/myapp/servlet/servlet/StartCISPage
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Phani Raju wrote:
If the request dispatcher is created without a leading slash like "servlet/StartCISPage", then it would going to http://localhost:8081/myapp/servlet/servlet/StartCISPage


Yes,woith out "/", *it is realtive to the current path* . so previously it was http://localhost:8081/myapp/servlet/login.do so, when you forward without slash("/") , then it search from current path so *it is http://localhost:8081/myapp/servlet/---- your url which is not start with "/" -----*

when you forward with "/" , *it is related to the context* . so server search from context i.e, http://localhost:8081/myapp/---- your url which is start with "/" ----

hth
Premila Devi Dayalan
Ranch Hand

Joined: Jul 19, 2009
Posts: 40
# RequestDispatcher d = httpservletrequest.getRequestDispatcher("/servlet/CAM);
# requestDispatcher.forward(httpservletrequest,response);

If the url starts with a "/" then it is relative to the host otherwise, it is relative to the page loaded. So the request dispatcher will forward to http://localhost:8081/servlet/CAM

If the request dispatcher is created without a leading slash like "servlet/CAM", then it would going to http://localhost:8081/servlet/servlet/CAM

Am I right ? Please guide
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Premila Devi wrote:Please guide


read my post . and why cant you try it out yourself ? practical gives you much confident.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward() in servlets.
 
Similar Threads
Issue in web.xml mapping
modules and url
Servlet Mapping
Problem accessing jsp Page
Tomcat 404 Error - Still not working