user4768611
user4768611

Reputation:

After redirect code still executing from previos page

How System.out.println("") is executed after response has been redirected to other resource.

In my code:

res.sendRedirect("/more_value_in_param/display.jsp");
System.out.println("to avoid penalty and disconnection");

Why in console to avoid penalty and disconnection is being displayed since response has been already redirected to display.jsp?

Upvotes: 1

Views: 2733

Answers (3)

ewhoch
ewhoch

Reputation: 361

res.sendRedirect() does not break control flow within that method, but does have effects on the use of the servlet response.

A response is sent to the client with the new URL, and within the program flow of res.sendRedirect(), the response should be considered to be committed.

Good information is found in the HttpServletResponse javadoc as well as "Myth2" listed in Control Flow Myths busted in Java.

I would also research the difference between sendRedirect and forward to get some insight on the client/container relationship at play.

Upvotes: 0

vivekpansara
vivekpansara

Reputation: 899

The sendRedirect() method does not halt the execution of your method.
You should either branch your code in such a way that the call to sendRedirect() is the last statement in your method or explicitly call return; after calling sendRedirect().

Upvotes: 4

sinclair
sinclair

Reputation: 2861

Sending a redirect does not break the bean-lifecycle, so the following code is executed.

Upvotes: 0

Related Questions