File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Getting session objects using JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting session objects using JSTL" Watch "Getting session objects using JSTL" New topic
Author

Getting session objects using JSTL

Bob Galp
Greenhorn

Joined: Jul 27, 2005
Posts: 4
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

Joined: Jan 10, 2002
Posts: 61601
    
  67

Actually, you retrieve it will the EL. The syntax would be ${sessionScope.name}


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bob Galp
Greenhorn

Joined: Jul 27, 2005
Posts: 4
Thanks!!
Can the variable from EL be used in jsp scriplets?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

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

Joined: Jul 27, 2005
Posts: 4
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

Joined: Jan 10, 2002
Posts: 61601
    
  67

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

Joined: Jul 27, 2005
Posts: 4
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

Joined: Jan 10, 2002
Posts: 61601
    
  67

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

Joined: Jan 10, 2002
Posts: 61601
    
  67

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting session objects using JSTL