permaculture playing cards*
The moose likes JSP and the fly likes c:out doesn't print values as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "c:out doesn Watch "c:out doesn New topic
Author

c:out doesn't print values as expected

L Duperval
Ranch Hand

Joined: May 14, 2003
Posts: 63
Hi,
I'm having trouble getting attributes to be printed properly using using JSTL tags. I did a small test, which looks like this:

The resulting HTML is:

What is wrong with my setup (or my understanding)?
L


Live Free, Live Happy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

"session" is not one of the JSTL implicit objects. Is it something that you have set up prior to the code snippet shown?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
L Duperval
Ranch Hand

Joined: May 14, 2003
Posts: 63
No. Actually, I wanted the names of all the attributes defined in the session. Evidently not the way to do it.
L
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Right. session is an implicit scripting variable, and the JSTL has no access to scripting variables. Remember, the whole purpose of JSTL is to minimize/eliminate on-page scripting.
JSTL's variable focus is on "scoped variables" -- attributes placed on the page, request, session and application contexts. As such, JSTL is most useful in an environment where the JSP pages have controllers that set things up -- including scoped variables -- on behalf of the page. It takes a little bit of turning your thinking around from the days when scriptlets were the norm, but once you've adjusted your thinking, it's amazing how much simpler doing things the JSTL way makes your life and the JSP pages.
For your example, the controller could set up a collection/array of the names it read from the session to use with the forEach action.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

For a slightly more interesting example: let's say that your page wanted to show both the attribute names, as well as their values. The controller would set up a Map of the name value pairs -- let's say as request attribute "sessionAttributes":

then on-page:
L Duperval
Ranch Hand

Joined: May 14, 2003
Posts: 63
Ah! That clears up a few things. Let me update my code and try again.
Thanks!
L
 
 
subject: c:out doesn't print values as expected
 
Similar Threads
JSTL listing all keys of a property file
iterating through a Map
Have the rules for EL in a JSP changed?
Acessing dynamically generated session scoped attributes in JSP
Baffling NPE in <c:forEach