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

x
?
Solved

session objects are not saving - please advise

Posted on 2004-11-18
7
Medium Priority
?
175 Views
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!
      
0
Comment
Question by:neojin80
7 Comments
 
LVL 92

Expert Comment

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

Expert Comment

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

Expert Comment

by:suprapto45
ID: 12625638
Well,

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

I hope that helps.

Regards
Dave
0
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

by:neojin80
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?
0
 
LVL 13

Expert Comment

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

Author Comment

by:neojin80
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"
      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
 
LVL 11

Accepted Solution

by:
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,
session.setAttribute("userid",userid);
Once it is set you can retreive it anywhere within that session.
-Raj
0

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