Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a form auto-populate a Bean?

 
Darren Wheatley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JSP form that I post to a controller servlet. I would like the form to automatically populate a java bean so that the controller servlet can then extract the data from the bean.
I read somewhere that the following will do that:
<jsp:useBean id="fBean" class="com.djwheatley.FormBean" scope="session">
<jsp:setProperty name="fBean" property="*"/>
</jsp:useBean>

<form action="Controller" method="post">
Username: <input type="text" name="username"/> <br/>
Password: <input type="text" name="password"/> <br/>
<input type="hidden" name="action" value="login"/>
<input type="submit" name="submit" value="OK"/>
</form>
My "FormBean" class is as follows:
public class FormBean
{
private StringsUsername;
private StringsPassword;
private StringsAction;
/*
* Default constructor
*/
public FormBean()
{
sUsername = "";
sPassword = "";
sAction = "";
}
public void setUsername(String user)
{
sUsername = user;
}
public String getUsername()
{
return sUsername;
}
public void setPassword(String p)
{
sPassword = p;
}
public String getPassword()
{
return sPassword;
}
public void setAction(String a)
{
sAction = a;
}
public String getAction()
{
return sAction;
}
}
Then in my Controller class I have:
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
FormBean fBean = (FormBean)request.getAttribute("fBean");

BUT fBean is always NULL.
Any ideas? Is this actually possible, or am I confused?
Thanks
Darren.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The names of your form variables have to match those in the controller for introspection to work.
 
Darren Wheatley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm still confused I'm afraid.
I thought that the idea was if you have an "<input name="bob"> in a form, then you have to have the following two methods in your Form Bean:
setBob
getBob
Is this right?
I have followed this pattern in matching up my HTML Form to my Form Bean.
Any ideas?
Thanks
Darren.
 
What a show! What atmosphere! What fun! What a tiny ad!
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!