aspose file tools*
The moose likes Servlets and the fly likes Servlets sendredirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets sendredirect" Watch "Servlets sendredirect" New topic
Author

Servlets sendredirect

srikanth darbha
Greenhorn

Joined: May 19, 2013
Posts: 28
Hi ,

i have few doubt regarding sendRedirect(String location) in httpresponse.
1)can the location value be an url mapping i.e, like response.sendRedirect("/addAmount") else should it be only sendRedirect("loginhome.html") or sendRedirect("loginhome.jsp")

2)What is meant by relative path ("/") what happens if i dont give relative path ?

please explain me clearly since i am new to servlets technology.


Thanks in advance



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

The URL should be a server-relative URL that starts with the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mohamed Iqzas
Ranch Hand

Joined: Jan 02, 2012
Posts: 53

actually - "/" takes you to the root.

when you start the url with "/" then append the url to "www.ur-domain-name.com".... this is how the final url will look like.

when you use without "/" - you should append the url to the directory your current page is in.

for ex: if your current url is in "www.ur-domain-name.com/MyApp/html/index.html", and inside index.html:

-> if you use a link with url-"login.html" not starting with slash , then it'll take you to the url - "www.ur-domain-name.com/MyApp/html/login.html"...

-> or if your url is "jsp/login.html", then it'll take you to the url - "www.ur-domain-name.com/MyApp/html/jsp/login.html"...

I have not given you all you need. You can search in google to get clear idea about it. Probably it might be in stackoverflow.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

The confusion can be cut by just following the best practice of using server-relative URLs (those that start with the context path) for redirects to resources within the same web app. For external resources, use the full absolute URL. Easy.
Mohamed Iqzas
Ranch Hand

Joined: Jan 02, 2012
Posts: 53

how is the root path configured in j2ee? when i start a url with "/" it once referred to the root of the server. www.myserver.com

but in another instance it referred to the root of my application directory. www.myserver.com/myApp

is it server specific? any xml file where we need to mention this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

It depends upon the servlet container.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets sendredirect
 
Similar Threads
Difference between sendRedirect() & forward() method
how do i include servlet in jsp
sendRedirect() - Servlet path
images display problem with dispatcher.forward()
loading image in panel