Sessions & JSP


I am facing a problem with utilizing sessions in my JSP pages and would appreciate some help.. heres the code:


String clientid="12";

session = request.getSession(true);
if (session.isNew( )) {
out.println("SESSION CLIENT"+ session.getAttribute("clientid")+ "ID:"+ session.getId());

The above is only printing the SESSION ID and is printing "null" for clientid.

Whats wrong with the above?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Http is a stateless protocol. There is no way of guaranteeing that a session start in page 1 will be the same as one in page 2. If you need to obtain an object with certainty, you need a persistence layer.
I think your problem is that in JSP there is already a session default variable.
Try removing the session = request.getSession(true) line, and see if it works.
THe HTTPSession object should be visible to your client. But - make sure you have a cookies enabled in your browser.
sgauchoAuthor Commented:
I have worked with session long time bak using the same syntax and it had worked..dunno whats causing the problem now..

vzilka, I commented the session = request.getSession(true) line but still unable to retrieve the clientid value using the getAttribute..

any more suggestions??

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

If it is not a new session then the clientid will not be set.  Try removing the if statement to see if that is the problem

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sgauchoAuthor Commented:
Hi guys,

I have solved the problem by commenting the //if (session.isNew())  line and its working now but that still doesnt explain why session.isNew() was not returning true.

Are you sure your browser support cookies?
try putting <%=session.isNew()%> at the bottom of the page and see what that value is
Mick BarryJava DeveloperCommented:
> session.isNew()

Don't think that'll ever return true in a jsp page.
Instead of:

if (session.isNew())


if (session.getAttribute("clientid") == null)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.