#1
  1. No Profile Picture
    Coder Extraordinaire
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    London
    Posts
    34
    Rep Power
    13

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    14
    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
  4. #3
  5. No Profile Picture
    Coder Extraordinaire
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    London
    Posts
    34
    Rep Power
    13
    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?
  6. #4
  7. No Profile Picture
    Coder Extraordinaire
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    London
    Posts
    34
    Rep Power
    13
    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?
  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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
  10. #6
  11. No Profile Picture
    Coder Extraordinaire
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    London
    Posts
    34
    Rep Power
    13
    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,
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    India
    Posts
    7
    Rep 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...

IMN logo majestic logo threadwatch logo seochat tools logo