• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing JavaBean from jsp to servlet

 
Sheetal Tiwari
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 2680
155
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!