This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes sendRedirect(...) to a page inside a .war file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "sendRedirect(...) to a page inside a .war file" Watch "sendRedirect(...) to a page inside a .war file" New topic
Author

sendRedirect(...) to a page inside a .war file

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

This question is from Niko's mock exam.

There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
response.sendRedirect("/help/index.html");

a.The content of index.html
b.HTTP Status 404


The answer given is b - status 404. But why should it fail?

As per my understanding, the war file would unfold to be the deployed application (directories and all). So wouldn't the container be able to find application_root/help/index.html?


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Think about it for a minute. What issues the redirect? The server? The browser? Other?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Bear Bibeault wrote:Think about it for a minute. What issues the redirect? The server? The browser? Other?


Ummm.. a redirect is issued by the server to the browser? (redirecting to xyz Url)

According to the API, the server would convert the relative URL to absolute URL before sending the redirect back to the browser. So "/test/index.html" would become "http://www.example.com/myapp/test/index.html" . So still not sure why the browser would not find it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Nidhi Sar wrote:Ummm.. a redirect is issued by the server to the browser? (redirecting to xyz Url)

Nope.

So "/test/index.html" would become "http://www.example.com/myapp/test/index.html"

Again, no.

Hint: the redirect is asked for by the server, but is not issued by the server.

How does this change things?
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Does it have something to do with the 'servlet container root' instead of 'application root'?

I mean, won't the redirect request be changed to: http://www.example.com/help/index.html instead of http://www.example.com/myApp/help/index.html ?


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

OK, lets deconstruct.

Step 1. Client (browser) sends a request to resource X
Step 2. Resource X starts processing the request in a servlet and comes across response.sendDirect("resource Y").
Step 3. Resource X sends back a response to the client with a status code + some info, saying "go to resource Y"
Step 4. Client sends a new request to resource Y

/test/index.html would get converted to http://www.example.com/test/index.html (not with the app-directory in between as I wrote previously)

So the net result would be a new request from the client to http://www.example.com/test/index.html

Please correct me if I am wrong.
Shobhan Ganta
Ranch Hand

Joined: Nov 11, 2010
Posts: 37
Hi Nidhi,

Does it have something to do with the 'servlet container root' instead of 'application root'?


I think whatever Pedro said is correct.
the response.sendRedirect() method expects its path parameter to be relative to the server root, not the webapp root. Thus, to send a redirect to the above URL from a servlet running , you must say

response.sendRedirect(request.getContextPath() +"/help/index.html");


please ignore my earlier post.
Thanks
Shobhan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

During a redirect the server sends headers to the browsers, which in turn, issues the new request. All requests from the browser need to include the context path, otherwise the container would have no idea what application to contact.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Got it!

Thanks Bear, Shobhan, Pedro... it all makes sense now.
 
Don't get me started about those stupid light bulbs.
 
subject: sendRedirect(...) to a page inside a .war file
 
Similar Threads
Tomcat unable to find servlet in Shared linux java hosting Server
problem in deploying WAR files in server
Deployment of applets in WL6.0
Niko's exam doubt
Struts 2 -Eclipse