The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Sessions not working
Discuss Sessions not working in the Java Help forum on Dev Shed. Sessions not working Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 19th, 2002, 12:46 PM
|
|
Coder Extraordinaire
|
|
Join Date: Jan 2002
Location: London
Posts: 34
Time spent in forums: 7 m 47 sec
Reputation Power: 12
|
|
|
Sessions not working
I have a sessions problem. I use the following code to set some session variables:
Code:
<%@ page session="true" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.servlet.*" %>
<%
session = request.getSession(true);
session.setAttribute("name", request.getParameter( "name" ));
<jsp:forward page="nextpage.jsp"/>
%>
and this code to get the data back again:
Code:
<%@ page session="true" %>
<%@ page import="javax.servlet.http.HttpSession" %>
<%
session = request.getSession(true);
String name = session.getAttribute("name");
%>
but all I get back is null.
I'm 100% sure that the getParameter bit is returning a value so I'm left unsure of whats going on.
Any help would be greatly apreciated. Thanks,
|

March 19th, 2002, 05:25 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: On a screen near you
Posts: 498

Time spent in forums: < 1 sec
Reputation Power: 12
|
|
Try reading the parameter into a String before setting
Code:
String name = request.getParameter("name");
session.setAttribute("name", name);
Mark
__________________
100 trillion calculations per nanosecond
|

March 19th, 2002, 06:36 PM
|
|
Coder Extraordinaire
|
|
Join Date: Jan 2002
Location: London
Posts: 34
Time spent in forums: 7 m 47 sec
Reputation Power: 12
|
|
|
Thanks for the suggestion. I gave it a try but it didn't make any difference.
Are there any settings I need to change or could check that tell JSP to use sessions, perhaps controls on the passing of session ID's?
|

March 19th, 2002, 07:15 PM
|
|
Coder Extraordinaire
|
|
Join Date: Jan 2002
Location: London
Posts: 34
Time spent in forums: 7 m 47 sec
Reputation Power: 12
|
|
|
More information:
I have just tried outputing the SessionId in the page that outputs the data and its different every time I refresh the page. This obviously means that a new session is being created instead of the old one biging picked up. I've removed the true paramater from the getSession call but it made no difference. I have got Cokies enabled in my browser so its not that. Any Ideas?
|

April 1st, 2002, 10:06 AM
|
|
Clueless llama
|
|
Join Date: Feb 2001
Location: Lincoln, NE. USA
|
|
Sessions in jsp's are created implicitly, you do not need to set it in a variable. The session variable is available automatically. You are most likely overwriting it with a new session. Also the boolean 'true' passed to the getSession method is the default, so leaving it out still passes true. Lastly, you have some odd syntax in your code.
Try this:
Code:
<%-- this next line is actually optional. It is the default --%>
<%@ page session="true" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.servlet.*" %>
<%
//the session variable is available automatically. No need to set it
//session = request.getSession(true);
session.setAttribute("name", request.getParameter( "name" ));
%>
<%-- this next line was in the scriptlet tag. Should be outside --%>
<jsp:forward page="nextpage.jsp"/>
--------------------------
<%-- again, next two lines are optional
<%@ page session="true" %>
<%@ page import="javax.servlet.http.HttpSession" %> --%>
<%
//session = request.getSession(true); //do not do this
String name = session.getAttribute("name");
%>
that should work
|

April 2nd, 2002, 11:14 AM
|
|
Coder Extraordinaire
|
|
Join Date: Jan 2002
Location: London
Posts: 34
Time spent in forums: 7 m 47 sec
Reputation Power: 12
|
|
|
Thanks for the advice, I'll out it all into practice.
I found today that the actual problem was in the cookie holding the session ID. The path of the cookie was /Project where as the path being requested was /project (note the difference in case). When I changed this it worked fine.
Thanks again,
|

April 2nd, 2002, 10:32 PM
|
|
Member
|
|
Join Date: Mar 2002
Location: India
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
try using
session.putValue("name", request.getParameter("name"));
for putting values into the session and
session.getValue to retreive values from the session...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|