This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes JavaBeans and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JavaBeans and Servlets" Watch "JavaBeans and Servlets" New topic
Author

JavaBeans and Servlets

Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
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

Joined: Sep 14, 2002
Posts: 184
So much easier than I had thought, it's just:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

Ummm not quite, I assume you meant request.getAttribute( String )?
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
request.getParameter was working for me earlier.
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
getParameter can't work in this case.
it returns a String, not an Object....


Asher Tarnopolski
SCJP,SCWCD
Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
The bean attributes are Strings. Maybe that's why it worked. unno:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

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

Joined: Sep 14, 2002
Posts: 184
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

Joined: Jul 28, 2001
Posts: 260
your bean is in session scope.
so, to get it run getAttribute on your session object.
Sachin Dere
Ranch Hand

Joined: Jan 14, 2003
Posts: 80
hi,
To call a bean from servlet,
IndexBean indexBean = new Indexbean();
call get set methods of it
Sachin
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JavaBeans and Servlets