Sessions & JSP

Hi,

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

page1.jsp

<%
String clientid="12";

session = request.getSession(true);
if (session.isNew( )) {
  session.setAttribute("clientid",id);
}
%>
---------------------
page2.jsp
<%
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?

thnx,
sg
sgauchoAsked:
Who is Participating?
 
AlexNYCCommented:
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
0
 
CEHJCommented:
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.
0
 
vzilkaCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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??
thnx,
sg

0
 
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.

thnx,
sg
0
 
vzilkaCommented:
Are you sure your browser support cookies?
0
 
AlexNYCCommented:
try putting <%=session.isNew()%> at the bottom of the page and see what that value is
0
 
objectsCommented:
> session.isNew()

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

if (session.isNew())

try:

if (session.getAttribute("clientid") == null)
0
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.

All Courses

From novice to tech pro — start learning today.