• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with jsp beans

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ann anie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now..thanks for the reply...
 
reply
    Bookmark Topic Watch Topic
  • New Topic