[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

losing variables in session and request

Posted on 2009-05-05
11
Medium Priority
?
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
11 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 24302908
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
ID: 24302931
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
ID: 24302964
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:pronane
ID: 24302997
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
ID: 24303003
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
 
LVL 1

Author Comment

by:pronane
ID: 24303018
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
ID: 24303163
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
ID: 24303582
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
ID: 24312787
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 28

Accepted Solution

by:
rrz earned 200 total points
ID: 24322053
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
A solution for Fortify Path Manipulation.
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.
Suggested Courses

650 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