• 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

JavaBeans and Servlets

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I access the values of my JavaBean in a servlet? Something similar to what I do in my jsp pages (i.e., <jsp:setProperty name="indexBeanId" property="lastName" /> . Thanks.
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So much easier than I had thought, it's just:
 
Sheriff
Posts: 67746
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
Ummm not quite, I assume you meant request.getAttribute( String )?
bear
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
request.getParameter was working for me earlier.
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getParameter can't work in this case.
it returns a String, not an Object....
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The bean attributes are Strings. Maybe that's why it worked. unno:
 
Bear Bibeault
Sheriff
Posts: 67746
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
Request.getParameter(String) will return only request parameter values that were either posted to the request via a form submission, or were specified in the query string. There is no way it could return the property of a bean.
If you are getting a value from calling getParameter, it's the result of a submitted value
To answer the original question, if the bean is set as an attribute on the request, you can get a reference to the bean using getAttribute (casting the returned Object as the bean type), and then calling accessors on the bean.
hth,
bear
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I try getAttribute(), they come back as null.
Here is my indexBean:

Here is my index.jsp:

And here is my loginAction servlet:
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your bean is in session scope.
so, to get it run getAttribute on your session object.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
To call a bean from servlet,
IndexBean indexBean = new Indexbean();
call get set methods of it
Sachin
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,

Your servlet is not actually accessing the fields of your bean by using getAttribute().

To access the form fields, use getParameter() as you described before, but this does NOT access the bean either.

To access variables you have placed into the attribute space of request, session or application object, use the getAttribute() method of the object you used setAttribute() on. In your example, you never use setAttribute() on any object *explicitly*, so that's why you are getting nulls. There is no attribute called firstName. There is though, a parameter of that name, based on your form submission, which is why your example used to work. But you were never getting it from the bean.

Javabeans *implicitly* set the instance of the javabean object into whatever scope you specify in the scope parameter of jsp:useBean

But to use it in a servlet would be a two-step process. First retrieve the bean from the proper scope...

indexBean foo = (indexBean) session.getAttribute("indexBeanId");

*then* retrieve the items from the bean...
String firstName = foo.getFirstName();
[ February 24, 2003: Message edited by: Mike Curwen ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic