I am experiencing strange behavior in my Spring MVC 3.2 application, and I noticed that this only happening when the redirect is done in an alternate way; so my questions are:
Is doing 'redirect:/process' any different from 'redirect:process' for redirecting to an internal controller ?
Does the added slash make any difference, such as affecting session handling ?
What are the reasons for a lost session (or lost session attributes) ?
There's a value I read through my application; even thou I do redirects in many cases, when I add a slash before the Controller URI, on production I am sometimes losing this value.
Any clue on how to troubleshoot the lost session value ?
Note: I am using methods httpRequest.getSession().setAttribute
and httpSession.getAttribute
for accessing the session.
Using the redirect prefix in your controller will generate a HTTP response with the 302 status code and the location header pointing to the redirection URL. The browser will then redirect to that URL (model exposed in the first request will be lost, and the browser URL will be the second one). Save this answer.
Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL. Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.
Answer: Front Controller is responsible to handle the entire incoming request of an application. In Spring MVC, dispatcher servlet acts as a front controller and handles the entire incoming requests.
Behind the scenes, RedirectView will trigger an HttpServletResponse. sendRedirect(), which will perform the actual redirect.
with '/' you are declaring a path from root, which is your servlet context path. without '/', usually it's going to a path relative to your current sub path. for example, if you are at '/go/url', your are pointing to '/go/url/next', not '/next'.
I didn't check spring source code but that's how it works in web browser usually.
EDIT:
I'm sorry, in Spring MVC, you must always provide full path, not just relative path. So you should do "redirect:/full/path".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With