File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Help with jsp beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Help with jsp beans" Watch "Help with jsp beans" New topic
Author

Help with jsp beans

ann anie
Greenhorn

Joined: Feb 22, 2010
Posts: 7
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: 42594
    
  65
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.


Ping & DNS - my free Android networking tools app
ann anie
Greenhorn

Joined: Feb 22, 2010
Posts: 7
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: 42594
    
  65
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.)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
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??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61603
    
  67

ann anie wrote:Or java beans are useful only for jsp-jsp communication and not for jsp-servlet communication??

Scoped variables (beans placed in context scopes) are available to both servlets and JSPs.

If you want to use something similar to property="*" in a servlet, check out the BeanUtils project.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
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.
ann anie
Greenhorn

Joined: Feb 22, 2010
Posts: 7
I got it now..thanks for the reply...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with jsp beans