Link to home
Start Free TrialLog in
Avatar of sgaucho
sgaucho

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of vzilka
vzilka

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.
Avatar of sgaucho

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of AlexNYC
AlexNYC

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sgaucho

ASKER

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
Are you sure your browser support cookies?
try putting <%=session.isNew()%> at the bottom of the page and see what that value is
> session.isNew()

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

if (session.isNew())

try:

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