wood burning stoves*
The moose likes JSP and the fly likes Page Bean not setting text property Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Page Bean not setting text property" Watch "Page Bean not setting text property" New topic
Author

Page Bean not setting text property

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Hi, I am trying to use a session bean to set the values from two textboxs. The first jsp has the following 2 lines of code at the beginning:
<jsp:useBean id="user" class="com.beans.UserData" scope="session" />
<jsp:setProperty name="user" property="*" />

For some reason the second jsp will not pick up the value of the textbox.
The second jsp has the following two lines of code at the beginning:
<jsp:useBean id="user" class="com.beans.UserData" scope="session" />
<%= user.getUserid() %>

You would think it could be a syntax or other error, but if I am to explicitly set the property value in the first jsp (ex. value="123"), the bean property is successfully populated and the value 123 is displayed in the second jsp. This also rules out the possibility of a configuration issue with Tomcat. Any ideas?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

You mention "the textbox", but give no clue as to where it fits into the picture. Is it in the first JSP? The unmentioned JSP that submitted to the first JSP?

If it's in the first JSP there's no way that its value will be set into a bean that you create on the same page. The JSP runs to create the page to send to the client, long before any input can be put into the form elements. If you want to pick up the values of submitted form elements in a bean, the bean must be declared in the JSP that you are submitting the form to.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Thanks for the super fast response! My text box was in the first jsp. There wasn't an unmentioned jsp. I made some modifications, which also are not working. The first jsp (login.jsp) contains two text boxes (loginid and loginpass) and uses a POST to submit to the second jsp (validate.jsp).
Validate.jsp looks like the following:

------
<jsp:useBean id="user" class="com.beans.UserData" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<jsp:getProperty name="user" property="userid"/>

<html>
<HEAD>
</HEAD>

<BODY>
<FORM name="validateform">
This is coming from the userid textbox on login.jsp:
<br>
<%= user.getUserid() %>
</body>
</html>
------
And here is my UserData bean:

------
package com.beans;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: Aug 31, 2004
* Time: 12:18:36 AM
* To change this template use File | Settings | File Templates.
*/
public class UserData {
String userid = null;
String userpass = null;

public void setUserid(String value)
{
System.out.println("Andys value is: " + value);
userid = value;
}

public void setUserpass(String value)
{
userpass = value;
}

public String getUserid() { return userid; }

public String getUserpass() { return userpass; }
}
------

The second jsp (validate.jsp) is still displaying null.
Any ideas?

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

I don't see where you expect loginid and loginpass to be set into the bean. It has no such properties.
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
That was a typo. These are the values I have in the first jsp:
<input type="text" name="userid" size="13"></font></input> <br>
<input type="password" name="userpass" size="13"></input></p>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Are the setters getting called?
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
No, there is nothing printing in the log window. I wonder if the submit is not operating properly.. I added something like this on my second jsp:

------
<%
String name = request.getParameter( "userid" );
session.setAttribute( "theName", name );
%>
Hello, <%= session.getAttribute( "theName" ) %>
------

And it is producing null. I don't think there is a Tomcat config for allowing a session scope? I am out of ideas - it appears to be set up textbook.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

What happens if you cut out the middle man and just see if the parameter is getting to the second page? Why all the hulabaloo with the session?

userid is <%= request.getParameter( "userid" ) %>

Is it as expected?

If not, what does your form look like?
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Ok figured it out. This is kind of embarrassing.
I have a js file for validation and it was set to this (see the error?):

------
function validateForm() {
if (form1.userid.value == "" && form1.userpass.value == "") {
alert("Enter a Username and Password");
form1.userid.focus();
return false;
}
else if (form1.userid.value == "") {
alert("Enter a Username");
form1.userid.focus();
return false;
} else if (form1.userpass.value == "") {
alert("Enter a Password");
form1.userpass.focus();
return false;
} else {

form1.userid.value = "";
form1.userpass.value = "";
return true;
}
}
------

Every time I submitted the form, I was clearing out the form values.
I am a newbie to JSP - I guess I learned a valuable lesson here.
Thanks for helping me out.
Regards.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

No prob... that's what JavaRanch s all about!
 
Don't get me started about those stupid light bulbs.
 
subject: Page Bean not setting text property
 
Similar Threads
Multiple submit buttons with JavaBeans
Servlet not being called
problem with bean using Apache
About jsp:useBean !!!!
Java Bean Scope Attribute