• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaBeans and Servlets

 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
So much easier than I had thought, it's just:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
request.getParameter was working for me earlier.
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
The bean attributes are Strings. Maybe that's why it worked. unno:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
your bean is in session scope.
so, to get it run getAttribute on your session object.
 
Sachin Dere
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
To call a bean from servlet,
IndexBean indexBean = new Indexbean();
call get set methods of it
Sachin
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic