• Post Reply Bookmark Topic Watch Topic
  • New Topic

EL Using jsp:useBean Instead of session.getAttribute

 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get away from doing alot of session.seAttributes then in jsp using EL.

I would like to use the jsp:useBean and EL.

However, I am having difficulity getting it to work.

I have a bean which is populated by a ResultSet at app logon.

When I use the following it does not display the values already set at logon. It creates a new instance of the bean with no values. How can I tie to a bean that is already instansiated.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:I am trying to get away from doing alot of session.seAttributes then in jsp using EL.

I would like to use the jsp:useBean and EL.

Why? useBean isn't necessary with the EL. It was necessary for establishing scripting variables for Java scriptlets, but the EL can access scoped variables in any session directly.

When I use the following it does not display the values already set at logon. It creates a new instance of the bean with no values.

Yeah, that's what's going to happen. So again, why use useBean at all?

How can I tie to a bean that is already instansiated.

Place the bean in session scope as a scoped variable and just use its name. You're trying to make this way too complicated. Just drop the useBean notion immediately. It's not needed and is just gumming up the works.
 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Place the bean in session scope as a scoped variable and just use its name.


Thanks Bear for coming to my rescue again.

So the way I undersand the above is set the object to a session attribute and use the EL as normal?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Wherever the scoped variable is to be set, use:
and in the JSP: ${fred}

(Assuming, of course, session scope. Works the same for all scopes.)
 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Yes. Wherever the scoped variable is to be set, use:
and in the JSP: ${fred}

(Assuming, of course, session scope. Works the same for all scoped.)


I am doing this but a little different. This keeps me from having to do setAttribute on every property of the User class.
I want to make sure it is correct.



Then in the jsp syntax:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is no reason to pull individual properties out. That's the whole point of beans.

And it would be ${userData.fullname}. Please make sure you are using the EL correctly.
 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Yes, there is no reason to pull individual properties out. That's the whole point of beans.

And it would be ${userData.fullname}. Please make sure you are using the EL correctly.


Bear, I thought I had it all figured out until I went to reference the User object in another servlet. How do I use the getAttribute for just one of the properties of the object?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get a reference to the bean and then get the required property. Just like any other Java class.

I'm not sure why you keep thinking you need to deconstruct the bean.
 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You get a reference to the bean and then get the required property. Just like any other Java class.

I'm not sure why you keep thinking you need to deconstruct the bean.


How do I get a reference to the bean without having to use the new constructer. Wont the new constructer initialize the bean with no or new property values?

I need the reference the bean with the initialal values still intact.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor? You only use a constructor when creating objects.

To obtain a reference to a scoped variable, you simply use getAttribute() on the appropriate scope object (session, request, etc).
 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Constructor? You only use a constructor when creating objects.

To obtain a reference to a scoped variable, you simply use getAttribute() on the appropriate scope object (session, request, etc).


I am back to my original question. If I do:



The above sets the User object with all its properties/values.

Now in another servlet I need the value of the 'fullname' property. But I do not understand how to get to it. I have this so far.



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so that last line of code fetches the reference to the bean. Assign it to a variable -- you'll need to cast.

Now, that variable has a reference to the bean that you can use to call its methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!