Solved

Sessions & JSP

Posted on 2003-12-10
9
550 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:sgaucho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9913527
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
 
LVL 9

Expert Comment

by:vzilka
ID: 9913637
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
 

Author Comment

by:sgaucho
ID: 9913824
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
Industry Leaders: 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!

 

Accepted Solution

by:
AlexNYC earned 80 total points
ID: 9913867
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
 

Author Comment

by:sgaucho
ID: 9913904
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
 
LVL 9

Expert Comment

by:vzilka
ID: 9913983
Are you sure your browser support cookies?
0
 

Expert Comment

by:AlexNYC
ID: 9914003
try putting <%=session.isNew()%> at the bottom of the page and see what that value is
0
 
LVL 92

Expert Comment

by:objects
ID: 9915217
> session.isNew()

Don't think that'll ever return true in a jsp page.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9917180
Instead of:

if (session.isNew())

try:

if (session.getAttribute("clientid") == null)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 105
jsp error 6 72
learn programming 8 94
Java List 4 76
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 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