session objects are not saving - please advise

Posted on 2004-11-18
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

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

    Expert Comment

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

    Expert Comment


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

    I hope that helps.


    Author Comment

    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

    please post ur platform information, App Server, etc

    Author Comment

    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

         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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Synchronize a new Active Directory domain with an existing Office 365 tenant
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now