This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Retaining values between JSP pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Retaining values between JSP pages" Watch "Retaining values between JSP pages" New topic
Author

Retaining values between JSP pages

Jolly Khanna
Greenhorn

Joined: Sep 24, 2001
Posts: 12
Hi,
Can someone please help?
Here's the problem I'm having:

I have a user who enters a product number on the JSP page (let's call it page1.jsp). The user hits the submit button and the product number goes to a Java Action class that retrieves the product from a database. The results are stored in a Table. The table is stored in a session in the Java action class like so:
session.setAttribute("tablegrid", table);
The table consists of rows and columns. Rows represent the product's COLORS, and columns represent the SIZES available. The user is then forwarded to page2.jsp, which displays the results in table format. In page2.jsp, the user can enter values in each cell. The values represent quantity. At the bottom of page2.jsp, there's a text box where the user can enter another product number. When the user enters another product number and hits the "next" button, the values for the current product are stored/saved, the Java action class is called again, and the next table is built based on the number you entered and stored in the session. page2.jsp is refreshed with the new table. If the user decides to hit the browser's 'BACK' button while they are in the middle of entering values for this new table, the page should show the previous values the user entered. The user can enter many product numbers and fill in the table values. This can go on until the user hits the "finished" button on page2.jsp, at which time they are directed to summary.jsp. summary.jsp will show a summary of all the tables and values they entered for each cell.

How can I store the values for each product the user enters? What is the best approach? And how can a recall these values if the user hits the back button? I thought about storing the values the user enters in a Hashtable but how do I associate the hashtable with the product number?

EX:

page1.jsp

<form name="test" action="getproducts.do">
<input type="text" name="products">
<input type="button" value="submit">
</form>

page2.jsp


<% ResultData tab = (ResultData)session.getAttribute("tablegrid"); %>

<form name="products" action="showproducts.do">
<table>
<%
for(int i = 0; i < 10; i++)
{ %>
<tr>
<%
for(int i = 0; i < tab.getNumRows(); i++) { %>
<td><input type="text" name="<%= tab.getString(i) %>" value=""></td>
<% } %>
</tr>
<%
} %>

</table>
<input type="text" name="nextItem">
<input type="button" value="NEXT">
<input type="button" value="FINISHED">
</form>

JavaAction.java

//Some logic
....

Hashtable products = new Hashtable();
//This is how I am currently saving the values the user entered
while (enum.hasMoreElements())
{
String name = (String) enum.nextElement();
if(request.getParameter(name) != null && request.getParameter(name).length() > 0)
{
products.put(name, request.getParameter(name));
}

}
...
//some more logic
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retaining values between JSP pages