Hi ,
I'm new to jsp beans,I'm setting the bean values from trial.jsp and trying to retrieve from trial2.jsp. But the values are coming as null. Can you please help me on this.
Code of Trial.jsp
Trial2.jsp
UserInfo.java
Please help me.....
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35252
7
posted
0
Welcome to JavaRanch.
Please note that the CODE tag uses square brackets ( [ ] ), not parentheses ( { } ). I have fixed this for you.
The "jsp:setProperty property="*" name="person" " line needs to be in Trials2.jsp, not Trial.jsp, because that's where the values are being set.
thanks for the reply..
but how do i access the values if the target of the trial.jsp is a servlet?
can i get the java class using request.getAttribute("person")?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35252
7
posted
0
No, servlets don't work that way - they're called "JSP beans" because they're used with JSPs. Servlets would use getParameter to get at individual form parameters. (As a side note, just about all web frameworks, from Struts to Stripes to Wicket, perform the parameter-to-Java field/object binding automatically.)
Note also that once you make a new request, like with a link, form submission, etc. the *current* request's attributes are lost.
ann anie
Greenhorn
Joined: Feb 22, 2010
Posts: 7
posted
0
Thanks for the reply.
But one more doubt. I read some where, if we set the scope="session" while creating a bean, the bean will be available in the session and I would be able to accccess the object from session as session.getAttribute("beanclass")?
Will this work?
Or java beans are useful only for jsp-jsp communication and not for jsp-servlet communication??
I think the OP is confused about how the input parameters relate to the bean properties.
I would like to offer the following simple example.
In the first JSP page, you include various input fields and a submit button in an HTML <form> element. The Form posts to your servlet.
The servlet uses request.getParameter("parameter-name") to get each of the parameters. You can then assign each of these parameters as you like to your Bean instance. Note that as Ulf said, you can use BeanUtils to locate all the input parameter values and assign them to your bean instance automatically (or use a framework like Struts 2 to do this automatically).
You then assign the bean instance to the session (in request scope perhaps) and forward/redirect to the display JSP page.
In the display JSP page, you can use the <jsp:useBean ...> if you wish, or just use JSTL/EL, which is even easier - to access the bean properties and display them on the page.