Avatar of Damn
Damn asked on

Redirect to the same application arriving before data in session saved

I save some data to http session, then send a redirect to another page in same application. In the next page (Servlet) I try to read the data from the session, but this data is empty.
Code is similar to this:

     session.setAttribute("myAttribute", "<data >SomeData</data>");
    response.sendRedirect(redirectUrl);

Now when next servlet is executed, session.GetAttribute("myAttribute") does not return data I have just saved.
In order for the next page to display correctly, I need the data from the session.
How to avoid redirect coming to server before the original code ends writing data to session?
Java EE

Avatar of undefined
Last Comment
Kuldeepchaturvedi

8/22/2022 - Mon
Kuldeepchaturvedi

if it is in the same application then use
.forward rather than redirect.
ASKER
Damn

Can't do forward. I need the browser to be set to the new URL, so if user reloads the page, he reloads new URL, not original one.
Kuldeepchaturvedi

If your urls are in the same application, then they should be able to share the session... Try printing the session ids at both points..

i.e. at first servlet before redirect and then at second servlet after redirect.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
Damn

They do share the same session. I think that the problem is that under some circumstances browser redirect arrives at server before session.setAttribute was executed.
ASKER CERTIFIED SOLUTION
Kuldeepchaturvedi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question