Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mistery dissapearance of a session

Status
Not open for further replies.

navrsalemile

Programmer
Feb 6, 2005
62
CA
I have a class which sets session parameters using
session.putValue( "param1", "value1");
session.putValue("param2", "value2");

and then redirects to a JSP page:

response.sendRedirect( "
somepage.jsp retrieves session params using this scriptlet:

<%
String param1 = "";
Object o = pageContext.getAttribute("param1", PageContext.SESSION_SCOPE );
if ( o != null ) {
param1 = o.toString();
out.println("param1 = " + param1);
}
String param2 = "";
o = pageContext.getAttribute("param2", PageContext.SESSION_SCOPE );
if ( o != null ) {
param2 = o.toString();
out.println("param1 = " + param2);
}
%>
...
<%@ include file="header.jsp" %>
...
<%
if (param2.compareTo("") != 0 ) {
out.flush();
pageContext.include( "welcome_" + param2 + ".jsp );
}
else {
out.println( "page not included")
%>
...
<%
if (param1.compareTo("") != 0) {
out.flush();
pageContext.include( param1 + ".jsp" );
else {
%>
...
<%@ include file="footer.jsp" %>

But the attributes retrieved from session here are nulls as I get message "page not included".
Isn't sendRedirect() supposed to preserve the session and only create a new http request!?

thanks,
mile
 
Hi,

HttpSession.putValue is been Deprecated. Try with setAttribute(String, Object) insted of putValue(String,Object);

Cheers
Venu
 
Tried all sort of things including that but no success. putValue() is equivalent to setAttribute().
Just before sendRedirect() I check parameters in session and they are there. After redirecting to JSP I check session using pageContext session scope and HttpSession but params are null!? I have no clue what is wrong
 
Hi,

Try this Test pages. Run resredirect.jsp which set's the attributes in session and redirect's to the redirect.jsp which will print the values in the session object.

resredirect.jsp
Code:
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Session Put Values</title>
  </head>
  <body>
   <%
     // put dummy values into session and redirect to a differernt page
     session.setAttribute("user","hello");
     session.setAttribute("role","admin");
     response.sendRedirect("redirect.jsp");
   %>
  </body>
</html>

redirect.jsp
Code:
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Redirect JSP page</title>
  </head>
  <body>
  <%
    //print values from session
    String user  = pageContext.getAttribute("user",PageContext.SESSION_SCOPE).toString();
    String role  = pageContext.getAttribute("role",PageContext.SESSION_SCOPE).toString();
    out.println("For User " + user + " >> Role is " + role);
  %>
  </body>
</html>

Output what I got is
Code:
For User hello >> Role is admin

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top