Forums Register Login

Passing JavaBean from jsp to servlet

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
it's a teeny, tiny, wafer thin ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 8376 times.
Similar Threads
JavaBean between Servlet and JSP
What is the equivalent to scriptlets for invalidating the session in jsp
Jstl not reflecting values back to servlet
Help with this code!
Infinate loop between JSP and Servlet - getParameter scope appears to be indefinate
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:22:31.