• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting session objects using JSTL

 
Bob Galp
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I retrieve an object stored in user's session using JSTL in a JSP?

That is, Replacing B]Object obj = session.getAttribute("name");[/B]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you retrieve it will the EL. The syntax would be ${sessionScope.name}
 
Bob Galp
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
Can the variable from EL be used in jsp scriplets?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not directly. Remember that the JSTL and EL are meant to supplant the use of scriplets in JSP pages. So, by design, there's no interaction between them.

In order to use a session-scoped variable (indeed, a scoped variable from any scope) in a sciptlet you need to fetch it from the scope via the getAttribute() method and cast it to the approriate type.
 
Bob Galp
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I need to use EL so I'll get it using the code that you provided. But how do i invoke the methods on the object that i get. I need to invoke a method that accepts a parameter, say an int.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. Not with the EL. If your aim is to write scriptless JSP pages, then calling general instance methods on an object is a no go. In the scriptless scenario it is expected that any such computations would have happened in the controller prior to the JSP gaining control, or can be handled via custom actions (tags) where you can "pass" data as attributes.
[ July 28, 2005: Message edited by: Bear Bibeault ]
 
Bob Galp
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Now I have a bean and I am getting the values from it using EL to dipalay on the page. One of the getters accepts an int. How can I invoke that getter method?

For example, invoking this method
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. A method that accepts a parameter is by definition not an accessor (aka "getter").

You can access a mutator ("setter") with the following, assuming that the bean in question is a scoped variable named myBean:



The EL will coerce the value of the value attribute to int.
[ July 29, 2005: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What it comes down to is that any value objects that you are going to use as scoped variables should strictly follow the JavaBean standard. Methods that accept parameters (outside of property mutators), constructors that take parameters, and the like are useless from the point of view of the EL.

Start thinking in terms of making the objects that you pass to your JSP page follow the JavaBeans conventions and your life will instantly become much simpler and better.

It's an easy matter to make the data EL-friendly on the Java side of the equation. Contorting on the page to try and accomodate non-standard structures will only lead to angst.
[ July 29, 2005: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic