Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 19th, 2002, 12:46 PM
andrewl41 andrewl41 is offline
Coder Extraordinaire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: London
Posts: 34 andrewl41 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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,

Reply With Quote
  #2  
Old March 19th, 2002, 05:25 PM
Marky_Mark Marky_Mark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: On a screen near you
Posts: 498 Marky_Mark User rank is Private First Class (20 - 50 Reputation Level)Marky_Mark User rank is Private First Class (20 - 50 Reputation Level) 
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

Reply With Quote
  #3  
Old March 19th, 2002, 06:36 PM
andrewl41 andrewl41 is offline
Coder Extraordinaire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: London
Posts: 34 andrewl41 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old March 19th, 2002, 07:15 PM
andrewl41 andrewl41 is offline
Coder Extraordinaire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: London
Posts: 34 andrewl41 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old April 1st, 2002, 10:06 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 39 m 5 sec
Reputation Power: 116
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

Reply With Quote
  #6  
Old April 2nd, 2002, 11:14 AM
andrewl41 andrewl41 is offline
Coder Extraordinaire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: London
Posts: 34 andrewl41 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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,

Reply With Quote
  #7  
Old April 2nd, 2002, 10:32 PM
aabha aabha is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: India
Posts: 7 aabha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to aabha
try using

session.putValue("name", request.getParameter("name"));

for putting values into the session and

session.getValue to retreive values from the session...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Sessions not working

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap