• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Getting session objects using JSTL

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Sheriff
Posts: 67752
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
Actually, you retrieve it will the EL. The syntax would be ${sessionScope.name}
 
Bob Galp
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!
Can the variable from EL be used in jsp scriplets?
 
Bear Bibeault
Sheriff
Posts: 67752
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
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
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
Sheriff
Posts: 67752
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
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 ]
 
please buy this thing and then I get a fat cut of the action:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic