session objects are not saving - please advise

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!
      
neojin80Asked:
Who is Participating?
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.

objectsCommented:
browser may not have cookies enabled so a new session is created for each request.
0
petmagdyCommented:
first make sure that
request.getUserPrincipal() or request.getUserPrincipal().getName() dosn't return null
0
suprapto45Commented:
Well,

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

I hope that helps.

Regards
Dave
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

neojin80Author Commented:
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?
0
petmagdyCommented:
please post ur platform information, App Server, etc
0
neojin80Author Commented:
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"
      xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Billing</web-resource-name>
      <description> accessible by authenticated users of the PI and ACP Admin role</description>
      <url-pattern>*.jsp</url-pattern>
      <http-method>GET</http-method>
      <http-method>POST</http-method>
      <http-method>PUT</http-method>
      <http-method>DELETE</http-method>
    </web-resource-collection>
    <auth-constraint>
      <role-name>116</role-name>
      <role-name>117</role-name>
      <role-name>118</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
  <login-config>
    <auth-method>FORM</auth-method>
    <realm-name>Login</realm-name>
    <form-login-config>
      <form-login-page>/Login/login.html</form-login-page>
      <form-error-page>/Login/error.html</form-error-page>
    </form-login-config>
  </login-config>
  <security-role>
    <role-name>116</role-name>
    <role-name>117</role-name>
    <role-name>118</role-name>
  </security-role>
</web-app>
0
raj3060Commented:
<%
     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,
session.setAttribute("userid",userid);
Once it is set you can retreive it anywhere within that session.
-Raj
0

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
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
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.