Forums Register Login
Getting session objects using JSTL
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]
Actually, you retrieve it will the EL. The syntax would be ${sessionScope.name}
Can the variable from EL be used in jsp scriplets?
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.
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.
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 ]
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
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 ]
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 ]
What's that smell? Hey, sniff this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java

This thread has been viewed 3057 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 16, 2019 11:01:00.