Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it for a minute. What issues the redirect? The server? The browser? Other?
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!

Thanks Bear, Shobhan, Pedro... it all makes sense now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic