• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 3412
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic