• Post Reply Bookmark Topic Watch Topic
  • New Topic

response.SendRedirect and session

 
pavankumar shetty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have the following set of code in my interceptor pre handle method. Logic behind this is, for some url in application i am suppose to redirect it to some other url. So i added the below code
if(url is true)
{
resp.sendRedirect(<<someurl>>);
LOGGER.debug("After send redirect--->"+hstName+routeOneURL);
return false;
}

will the log statement below redirect will cause any issues related to session ? currently log mode is error in application.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is cause of after Send Redirect your cursor will not go to next statement.

I am not sure can some one please comment on this...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ket bhav wrote:I think that is cause of after Send Redirect your cursor will not go to next statement.

I am not sure can some one please comment on this...

Incorrect.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
ket bhav wrote:I think that is cause of after Send Redirect your cursor will not go to next statement.

I am not sure can some one please comment on this...

Incorrect.


It's Java code, after all. When one statement ends then the next one after it is run. The normal rules of the Java language aren't suspended just because the code happens to be in a servlet.
 
Vijitha Kumara
Bartender
Posts: 3953
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to CodeRanch, Pavankumar Shetty!
 
Ashish Kataria
Greenhorn
Posts: 14
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too believe that code after SendRedirect will never run(although it will compile).
The reason being that every jsp page is compiled every time that page's url is hit.
So, it might seem it will be considered like any other java code, the method SendRedirect will never ever return to the same page.
 
Vijitha Kumara
Bartender
Posts: 3953
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too believe that code after SendRedirect will never run(although it will compile).

As already pointed out it will, see Paul's reply above.
The reason being that every jsp page is compiled every time that page's url is hit.

No, unless every time it was modified before each request. Where did you get this? And this compilation has nothing to do with the question here.
 
Ashish Kataria
Greenhorn
Posts: 14
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:
Where did you get this? And this compilation has nothing to do with the question here.


I am sorry for that weird reply. But i thought sendRedirect was used in a JSP.
And i did not know that JSPs were also compiled only once(after change). Thank you for sorting it out for me..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify so no one is confused:

  • Control does return from a call to sendRedirect().
  • JSPs are translated and compiled only on their first access.

  • Any assertions to the contrary are incorrect.
     
    Sunil Sunny
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No way once the Send redirect is executed control goes to another page. So now the interaction is with the new url to which it has been redirected. It does not points back to that statement after the "response.sendRedirect" code.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65530
    108
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sunil Sunny wrote:No way once the Send redirect is executed control goes to another page. So now the interaction is with the new url to which it has been redirected. It does not points back to that statement after the "response.sendRedirect" code.

    WRONG! Please do not post incorrect information.
     
    Sunil Sunny
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    WRONG! Please do not post incorrect information.


    Can you tell me sir where I am wrong, i do not think so. once the statement response.sendRedirect is execcuted how the cursor will come back to the statment after respose.sendredirect. Though it will compile but will never run.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65530
    108
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sunil Sunny wrote:Can you tell me sir where I am wrong

    You are very wrong in saying that control does not return to the servlet after a call to sendRedirect()

    i do not think so.

    You think incorrectly.

    once the statement response.sendRedirect is execcuted how the cursor will come back to the statment after respose.sendredirect.

    How will in not? Servlets aren't magic -- they follow the same rules as any other Java class. After a called method finishes execution, control is returned back to the caller. To suggest anything else is nonsense.

    Though it will compile but will never run.

    Have you tested this? (Obviously the answer is "no" because you would have found that you are incorrect.)

    All that the sendRedirect method does is to set a response header to be sent to the browser. Nothing more, and no magic.
     
    Sunil Sunny
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried it just when i saw your last response and sir you must try it i am 100% right . I agree to that servlet is just like java class but come to the point of the topic control never returns to the statement after the calling statement For your information i am posting the code.



    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
    /* TODO output your page here
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet NewServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
    out.println("</body>");
    out.println("</html>");
    */

    out.print(request.getParameter("name"));
    response.sendRedirect("http://www.google.com");
    Cookie c = new Cookie("sun","SWATI");

    c.setMaxAge(2000);
    response.addCookie(c);

    } finally {
    out.close();
    }
    }



    This is what i did and after redirection to "GOOGLE.COM" it has not set the cookie . When i retrieved the cookie it was not present as it was never set.


    if you still think you are right and i am wrong . Please make me clear .



     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65530
    108
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What sort of test is setting a cookie? Really? And of course the cookie will not be set because you are redirecting to google. Do you really think you can set a google.com cookie?

    How about performing a real test and put a logging statement there? You will see that the code is executed.

    if you still think you are right and i am wrong . Please make me clear .


    I cannot make it any clearer. You need to test it for yourself to see that you are wrong. (And with a valid test that actually works.)
     
    Sunil Sunny
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Boss you were right and it was pretty clear to me with the logging example when I tried it .
    But you said cookie will not be set because i was redirecting to google . Actually i had also tried on a local redirection, it still did not work . that's why i said control will never return to the statement after sendredirect. But only one doubt remain in my mind that why the cookie was not set as the control has already returned to it .

    Thanks once again for making me clear.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!