• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

response.SendRedirect and session

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to CodeRanch, Pavankumar Shetty!
 
Greenhorn
Posts: 14
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic