aspose file tools*
The moose likes Struts and the fly likes Problem with session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with session" Watch "Problem with session" New topic
Author

Problem with session

Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi everybody,
I have a problem in my application.
I have the page Page1.jsp where I have a form and I get only a value from a select tag.When I click the button I go to another page(Page2.jsp) where I have another form with input texts and select tags.In Page2.jsp I use

<%=request.getParameter("value_of_page1")%>

to get the value selected in my Page1.jsp.
In my Action class for the Page2.jsp I must use this value for a query and I try to get the value in this way

String field1= request.getParameter("value_of_page1");

After that I put field1 as parameter of my query.Well,when I run the application I don't get any result from the query,because(and this is my opinion)when I call 'request.getParameter("value_of_page1")' in my Action class the request object is referred to Page2.jsp.
I have also thought the solution can be the use of a session object.
I am quite sure of this because when I run the first form I get its value in Page2.jsp,but when I run the form on this page and I get no results,when I get back to Page2.jsp this value is null.
But how to solve this problem?
What should I write in my Action class?
I need a fast help.
Thanks in advance,
Mattia
Brendt Lucas
Greenhorn

Joined: Jan 04, 2007
Posts: 6
I don't know if I'm right, because I haven't been using JSP's for too long, but in Page2.jsp use the <jsp:useBean> tag to get the form from your last page, then call the form's getter() method for the value you want.



Again I may be wrong.
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
I am sorry but your solution does not work for my project.
I think it is a problem in my Action class for Page2.jsp because I don't know how to call the value of the form in Page1.jsp.
Is there a way to handle the session variable in Struts?
How can I use it in my case?
Mattia
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Input is page1.jsp, which sends to data to a form called MyForm. It then goes into MyAction, which does some business logic. MyAction forwards to page2.jsp. Correct?

Create a bean with the data you want to hold. Call it Bean.java

Define it in your action and populate the values to the bean from your form.

Then make the bean visible to page2.jsp


Then in page2.jsp you can make references to ${bean}

For example:

${bean.name} would be the name value in your bean.

Hope this helps.
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi Chris,
I made a part of what you said.Because I already had a bean where I was trying to hold the values of my first and my second form.I don't know if this is the error,but I have not changed anything in this bean and I made these steps.
In the method execute of the class Page1Action I wrote:

request.setAttribute("test",request.getParameter("value_of_page1"));

In page2.jsp I wrote:
<td>Previous value</td>
<logic resent name="test">
<bean:write name="test" />
</logic resent>
In this way I got no errors for the page.
In my class Page2Action I wrote this to get the value of Page1.jsp:

String value_of_page1 = req.getParameter("test");

Then when I execute the second form,I still get no results from my query and when I go back in Page2.jsp the value of "Previous value" is null.
What was it wrong?
Thanks,
Mattia.
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
You don't have to include this in either jsp page:

String value_of_page1 = req.getParameter("test");

You should be able to just type ${test.x} where x is the field in the bean of test.
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi,
I've written this in page2.jsp:
${test.value_of_page1}
and I get this error:

javax.servlet.ServletException: Unable to find a value for "value_of_page1" in object of class "java.lang.String" using operator "."

What does it mean?
Thanks,
Mattia
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Please post your bean code here.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I could not quite follow along with the processing here. I just wanted to add that I almost never get or set individual values on the request. I find that it is easier and cleaner to use action forms and let Struts populate the properties of the form.

- Brent
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
IMHO a form should be just that, a form. The form values should be used to prepopulate a form or to accept input. Now in this case a form is being used to send data to the action. The action is then forwarding to a result page. You wouldn't want to create another Form just to pass data to the result page.

You could.

We use JSTL here a lot also, so I don't like to tie myself to the Struts tags.
[ January 04, 2007: Message edited by: Chris Boldon ]
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi Chris,
This is part of my code in my pages and my Action classes:
Page1.jsp:
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<jsp:useBean id="bean" scope="session" class="com.myapp.struts.Bean" />
<html:form action="cluster">
<table border="3" cellspacing="5">
<thead>... </thead>
<tbody>
<tr><td><html:select name="bean" property="value_of_page1" styleClass="dropdown" size="1" >
<html ptions name="bean" property="list" />
</html:select></td></tr></tbody></table>
<html:submit value="Next step" />

Page1Action:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("test",request.getParameter("value_of_page1"));
return mapping.findForward(SUCCESS); }

Page2.jsp:
<jsp:useBean id="bean" scope="session" class="com.myapp.struts.Bean" />
<th><bean:message key="prompt.value" /></th>
<th> <logic resent name="test">${bean.value_of_page1}
</logic resent></th>
.....

I was wrong to write my code before.Now I get a null value for 'value_of_page1' in Page2.jsp.
I hope this can help to find the error.
Thanks,
Mattia
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Looking at your code, I don't know why you would expect there to be a value in bean.value_of_page1. You are not setting it in your action and you are not using action forms so Struts is not going to set it for you. Like I said before, I like to use action forms and let Struts do the work for me. If you don't want to go that approach then it seems like you need to update your action code to retrieve the "bean" object from the session and update the value_of_page1 property based on the value from the request.

What you are doing seems pretty simple. I am not quite sure why you are dealing with objects on the session. If your action mapping does not redirect, then the value_of_page1 already exists on the request when Page2.jsp is generated so you could just use that value.

IMO, 90% of the knowledge required to implement an application based on Struts has nothing to do with Struts. It deals with the workings of HTTP, HTML (especially HTML form) and a little with Servlets. Spending some time to learn more about this would make you a better Struts programmer.

- Brent
[ January 05, 2007: Message edited by: Brent Sterling ]
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
I see in Page1Action you define a form but you never use it. What type of data are you taking in, and are you using a form to bring it in? I'd use a form to bring data in, then I'd define the form in the action, then set the value in your bean with the getter from the form, then pass the bean out to your jsp.

ValueBean.java
 
jQuery in Action, 2nd edition
 
subject: Problem with session