• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make a form auto-populate a Bean?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic