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("clie ntid",id);
}
%>
---------------------
page2.jsp
<%
out.println("SESSION CLIENT"+ session.getAttribute("clie ntid")+ "ID:"+ session.getId());
%>
The above is only printing the SESSION ID and is printing "null" for clientid.
Whats wrong with the above?
thnx,
sg
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("clie
}
%>
---------------------
page2.jsp
<%
out.println("SESSION CLIENT"+ session.getAttribute("clie
%>
The above is only printing the SESSION ID and is printing "null" for clientid.
Whats wrong with the above?
thnx,
sg
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.
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.
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
Don't think that'll ever return true in a jsp page.
Instead of:
if (session.isNew())
try:
if (session.getAttribute("cli entid") == null)
if (session.isNew())
try:
if (session.getAttribute("cli