Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Passing JavaBean from jsp to servlet

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to pass values from a JSP form to an action servlet using a Java Bean.
JSP Code:

<form action="LoginServlet">
<jsp:useBean id="user" class=" model.User"
scope="session">
<jsp:setProperty name="user" property="*" />
</jsp:useBean>
<tr>
<td>Username:</td>
<td><input type="text"name="userId"/>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"/></td>
</tr>


( I have also tried setting the scope to 'request' )
Then in the servlet, I try to pick it up using: -

Servlet code:

HttpSession session = request.getSession(true);
userBean = (User)session.getAttribute("user");


But the userBean always comes up as a null, I never get the Java bean making it through the request or session.

Please, point out what is wrong with the above code.

Thanks.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that's not the way it works.

When do you build your bean? When the user tries to login?

well in your login servlet get the parameters of your form and build a bean with them.
For example:


Now the bean is created and filled. You can set it in your session attributes



In your jsp now you can refere to it with the name user.
Don't use scriptlets in jsp, use jstl
 
Sheetal Tiwari
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I understand that it is not required to build a bean at login and I am aware that this could be done in the way you specified
But this is just an example where I am trying to set bean in the jsp and pass it on to servlet.

Could you explain how to pass bean from jsp to servlet.
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are able to do that, but not with the values of your input fields. So set a specific value of your bean with a value:


In the servlet you can now see that that bean is instantiated and filled a value
Regards,
Frits
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic