Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store a value so that any JSP page can read it?

Nick R Wilson
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’m writing a web application using Struts 1.1. One of the initial web pages displays a grid of rows, loaded from a database table. The primary key of this table is not shown as part of the grid; instead it is displayed as a drop-down box so that only the rows for the selected key will be displayed.

What I want to do is to remember which value has been selected by the user, when they select a different drop-down value, and then choose to go to another page. This primary key is shared by several other tables, and I want it to default to display the corresponding rows with the same selected key, when the user moves to another JSP page.

On the web pages, this drop down box is stored within a form and is changed when the user clicks on the ‘Change’ button. What I need to know is where to store the value, so that it can later be retrieved by a different JSP page. Note that the web page that fails to find the String is not the next page in succession; the selected JSP page is one of several and is chosen via some JavaScript that choose which action to take, e.g.:
<li><a href="#"
onmouseout="mclosetime()">Control Types</a>
<div id="m4"
<html:link forward="AddCTypeNew">Add Control Type</html:link>
<html:link forward="ListCType">List Control Types</html:link>

The code I am using to display the drop down box in the JSP pages is as follows:

String controllerID = (String) System.getProperty("controllerID");
String valSelect = null;
<form action="/GameControls/">
<select name="controllerID">
<c:forEach items="${cTypes}" var="cType">
<c:when test='${cType.controllerID == controllerID}'>
<c:set var="valSelect" scope="page" value="selected=true" />
<c:set var="valSelect" scope="page" value="" />
<option <c:out value='${valSelect}'></c:out> value='<c:out value='${cType.controllerID}'></c:out>'>
<c:out value="${cType.controllerDescription}"></c:out></option>
</select> <input type="submit" value="Change"></form>

My problem is in storing the String ‘controllerID’. I have tried storing is as a parameter, an attribute, and, in this example, as a System.getProperty. The above code successfully reads from a List of cType objects. But the check as to which value needs the “selected=true” attribute always fails because the controllerID String is always null.

In my struts-config.xml file, here is an example of an action that is called, that tries to retrieve the controllerID String:

<action path="/addBCodeNew" type="filemaint.AddButtonCodePreAction"
name="cTypeForm" attribute="cType" validate="false" scope="session">
<forward name="success" path="/WEB-INF/pages/tiles/buttonCodeAddTiles.jsp" />

I have tried both ‘session’ and ‘request’ as values for the scope attribute, but neither has worked.

I’m not sure what else to illustrate in regards to my problem. If I’ve missed any important information about my configuration, I’ll happily supply it.
My bottom line questions are:
Why is my controllerID String always null? What is the correct way to store a value submitted to a form, so that any other JSP page can successfully retrieve it?
Bear Bibeault
Author and ninkuma
Posts: 65335
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.
David Newton
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to save the value in the action, not on the JSP page.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic