Solved

losing variables in session and request

Posted on 2009-05-05
11
551 Views
Last Modified: 2013-11-24
I am simply trying to set a request variable and then get the variable on reloading of the page but the value is null on reloading of the page:

top of the jsp:

 <% out.println(request.getAttribute("AttributeName"));%>

.......

request.setAttribute("AttributeName","This is the Attribute value.");
System.out.println("got here at  the setting of the request " + request.getAttribute("AttributeName"));
out.println(request.getAttribute("AttributeName"));


When the page is initially loaded the first line above prints out null, and the last line prints out "This is the Attribute value."

What im actually trying to do is set the selected value of a dropdown to what was previouslly selected before the page is reloaded(validation and navigation off to another page), but it isn't working becuase i cant seem to set anything in the session:


<select name="effectiveYear" id="effectiveYear" tabindex="13" value="<%= session.getAttribute("effectiveYear") %>">

<c:forEach var="year" begin="0" items="${indicationAddForm.dropDownYears}">

	<c:if test="${sessionScope.effectiveYear eq year}">

                          <option selected="selected" value="<c:out value="${year}"/>" <%= session.getAttribute("effectiveYear") %>><c:out value="${year}"/></option>
 

									</c:if>

									<c:if test="${sessionScope.effectiveYear != year}">

										<option value="<c:out value="${year}"/>" <%= session.getAttribute("effectiveYear") %>><c:out value="${year}"/></option>

									</c:if>							

    	

</c:forEach>

Open in new window

0
Comment
Question by:pronane
  • 5
  • 4
11 Comments
 
LVL 10

Expert Comment

by:mahome
Comment Utility
You can set in the session and you have to

 <% out.println(request.getSession().getAttribute("AttributeName"));%>
 

.......
 

request.getSession().setAttribute("AttributeName","This is the Attribute value.");

System.out.println("got here at  the setting of the request " + request.getSession().getAttribute("AttributeName"));

out.println(request.getSession().getAttribute("AttributeName"));

Open in new window

0
 
LVL 10

Expert Comment

by:mahome
Comment Utility
I would recommend to use ${effectiveYear} instead of <%= session.getAttribute("effectiveYear") %>, so it is not important if it's in the request-, page- or sessionScope.
0
 
LVL 1

Author Comment

by:pronane
Comment Utility
sorry i should have been a bit clearer in my question i think.  It doesn't have to be in the session, i would prefer it to be in teh request to be honest.

so why doesn't this work then?

request.setAttribute("AttributeName","This is the Attribute value.");
System.out.println("got here at  the setting of the request " + request.getAttribute("AttributeName"));
out.println(request.getAttribute("AttributeName"));


and then when the page is reloaded the attributeName value should be set and print out the string "this is the attribute value":
 <% out.println(request.getAttribute("AttributeName"));%>


0
 
LVL 1

Author Comment

by:pronane
Comment Utility
The request.getSession() worked.  Ok so why does the normal request.setAttribute(); not work above?



"I would recommend to use ${effectiveYear} instead of <%= session.getAttribute("effectiveYear") %>, so it is not important if it's in the request-, page- or sessionScope."

it doesn't work using ${effectiveYear} above as it thinks its null
0
 
LVL 10

Expert Comment

by:mahome
Comment Utility
because, when the page is reloaded, this is a new request and there will be a new request object. Session is your only possibility to use a variable in more than one request.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:pronane
Comment Utility
so what do i need to change in the code snippet to set effective year so that its available on reloading of the page?!
0
 
LVL 10

Expert Comment

by:mahome
Comment Utility
depends on how the reload is triggered. Your aim is to put the value in the session.
If it is Browser-reload (F5), you have to use JavaScript with an onChange-Listener on the select and set the value via Ajax (quite complicate). Otherwise you can put the selected value in the session in the servlet which is called:
request.getSession().setAttribute("effectiveYear", request.getParameter("effectiveYear"));

The JSP snippet is as follow. Just little corrections (for example: there is no value attribute for select).

<select name="effectiveYear" id="effectiveYear" tabindex="13">

<c:forEach var="year" begin="0" items="${indicationAddForm.dropDownYears}">

        <c:if test="${sessionScope.effectiveYear eq year}">

         	<option selected="selected" value="<c:out value="${year}"/>"><c:out value="${year}"/></option>

        </c:if>

        <c:if test="${sessionScope.effectiveYear != year}">

			<option value="<c:out value="${year}"/>"><c:out value="${year}"/></option>

		</c:if>        

</c:forEach>

</select>

Open in new window

0
 
LVL 1

Author Comment

by:pronane
Comment Utility
where should i put this;

request.getSession().setAttribute("effectiveYear", request.getParameter("effectiveYear"));


because when it reloads it seems to be setting it to the value that was previous to the one i selected before i refreshed?!
0
 
LVL 1

Author Comment

by:pronane
Comment Utility
that still doesn't work, it still loses the variable when i return to the page from another page using the code snippet you have given.  It appears is not setting the effectiveYear correctly and then it is not set on the session when i reselect a value....
0
 
LVL 27

Accepted Solution

by:
rrz earned 50 total points
Comment Utility
Here is some sample code that might help you. Just copy and paste this JSP.
<%@ page import="java.util.*" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<%

   List<Integer> dropDownYears = Arrays.asList(2000,2001,2003,2004,2005,2006,2007,2008,2009);

   session.setAttribute("dropDownYears",dropDownYears);

%>

<c:if test="${not empty param.effectiveYear}" >

          <c:set var="effectiveYear" value="${param.effectiveYear}" scope="session" />

</c:if>

<c:if test="${empty effectiveYear}" >

    <c:set var="effectiveYear" value="2000" scope="session" />

</c:if>

<html>

<body>

<form>

<select name="effectiveYear" id="effectiveYear" tabindex="13" >

<c:forEach var="year" items="${dropDownYears}">

	<c:if test="${effectiveYear eq year}">

                <option selected="selected" value="${year}" >${year}</option>

	</c:if>

	<c:if test="${effectiveYear != year}">

		<option value="${year}" >${year}</option>

	</c:if>							

    	

</c:forEach>

</select>

<input type="submit" />

</form>

</body>

</html>

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now