wood burning stoves 2.0*
The moose likes Servlets and the fly likes sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect()" Watch "sendRedirect()" New topic
Author

sendRedirect()

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
what is the difference between the

response.sendRedirect("/foo/doStuff.html")(with forward slash)

and

response.sendRedirect("foo/doStuff.html")(without forward slash)

please explain
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Relative path comes into 2 ways.

with"/" and without "/"


1. without using "/": container will look the jsp from current directory.

2. using "/" :container will look the jsp from ROOT directory.

i prefer 2nd one.

Hope This helps
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

First one specifies the absolute path (context specific path) while the second one specifies the relative path.


[Servlet tutorial] [Servlet 3.0 Cook Book]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by sudhir nim:
First one specifies the absolute path (context specific path) while the second one specifies the relative path.



No. it is also one kind of relative path.

Note:

absolute path means you have to give whole url starting from http://...
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by seetharaman venkatasamy
using "/" :container will look the jsp from ROOT directory.


Instead of ROOT, context root is the more appropriate term.
And BTW the original poster has mentioned html files and not jsp.

Although that wont change the answer, it might confuse a bit to beginners


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

And sandeep Talari please UseAMeaningfulSubjectLine.
So that you get more useful responses.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Amit
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Thanks Amit

You are welcome
Rohith Kumar Singh
Greenhorn

Joined: Aug 27, 2008
Posts: 6
Both the paths mentioned are relative only. The one for which '/' precedes the path will be relative to the underlying context root and for the other one where '/' is not preceding the path it'll be relative to the current request being serviced.

Cheers,
Rohit
[ August 27, 2008: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It's important to remember that merely adding a forward slash to the beginning of a relative url will not cause the browser to look in the root of your servlet context. Browsers know nothing about servlet contexts.

In order to build a proper context relative link you need to explicitly add the contextPath yourself. We have two articles that explain how to do this in our JSP FAQ:
http://faq.javaranch.com/java/RelativeLinks
and
http://faq.javaranch.com/java/ResourceUrlProblems

The both cover the same thing but are written in different ways.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect()