TGM
TGM

Reputation: 1679

Servlet redirect

resp.sendRedirect("/myurl");
req.getSession().setAttribute("foo", "bar");

In this case, do I have access to the foo attribute after the redirect? On generally speaking, a servlet is completely executed before the redirect is made or it stops it's execution after the redirect line?

Thanks

Upvotes: 7

Views: 15653

Answers (3)

Dirk
Dirk

Reputation: 1

I found out a more generic approach that works for jsp files as well as for servlets.

String url = "http://google.com";

response.reset();
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location",url);
response.getWriter().close();
response.getWriter().flush();

Upvotes: 0

chakri
chakri

Reputation: 629

after redirecting to that particular page, the control goes to that page and come back to old page and execute the req.getSession().setAttribute("foo", "bar"); also. this is the sendRedirect() bahaviour

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160170

It continues execution.

It's not a return, it just adds information to the response.

Upvotes: 8

Related Questions