|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
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, |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
|||
|
|||
|
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? |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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, |
|
#7
|
|||
|
|||
|
try using
session.putValue("name", request.getParameter("name")); for putting values into the session and session.getValue to retreive values from the session... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Sessions not working |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|