Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


session objects are not saving - please advise

Posted on 2004-11-18
Medium Priority
Last Modified: 2010-04-01
Hello everyone,

For some reason, my session objects are not saving in my jsps

For example I have this code is a small application:

      if (session.getAttribute("userid") == null) {
            %>should go here once, and thats it<%
            session.setAttribute ("userid", new String(request.getUserPrincipal().getName()));

I include this block in another page, and every single time I reload the page the "should go here once, and thats it" line keeps coming up. Anyone know what I am doing wrong? Please help!
Question by:neojin80
LVL 92

Expert Comment

ID: 12620621
browser may not have cookies enabled so a new session is created for each request.
LVL 13

Expert Comment

ID: 12621924
first make sure that
request.getUserPrincipal() or request.getUserPrincipal().getName() dosn't return null
LVL 16

Expert Comment

ID: 12625638

Are you sure that you have declared session like
HttpSession session = request.getSession();

I hope that helps.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12628416
I have tested it in both IE and Firefox with cookies enabled so I know that's not the problem.

Yes, request.getUserPrincipal().getName() does not return null

This is in a JSP, and I believe session is implicit.

Anyone else?
LVL 13

Expert Comment

ID: 12628492
please post ur platform information, App Server, etc

Author Comment

ID: 12628861
After some more testing, I believe this issue has something to do with single sign on and ssl in tomcat.

I have 3 web applications, with single sign on enabled. The 3 web applications are called ROOT, Billing, and Admin. From the ROOT, when a user clicks a link that takes them to the Billing context, a new session is instantiated, however is behaves like the problem above. When I turn off SSL, then the problem goes away and the session starts to work.

Very strange. I will post my web.xml file below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"

      <description> accessible by authenticated users of the PI and ACP Admin role</description>
LVL 11

Accepted Solution

raj3060 earned 750 total points
ID: 12644942
     if (session.getAttribute("userid") == null) {
          %>should go here once, and thats it<%
          session.setAttribute ("userid", session.getAttribute("userid") ); //you do not need to do that again as you already have this attribute in session (I beleive that's trus if you already did it).

Anyway, you need to set the attribute before you get it, you are retreiving it before you are setting it, that's why you get null all the time. If you are getting userid from some other place(JSP), then get it like
String userid = (String) request.getParameter("userid");
now set the attribute,
Once it is set you can retreive it anywhere within that session.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Choosing the right mix of apps is very much necessary for CPAs for making the most of the latest technology through which they can boost their growth.
"Day by day nothing changes but when u look back, everything is different". That quote precisely describes today’s digital era. For example, you may not have noticed the change, but Voice Search is now all around us.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question