1

Update : the code is now in github along with instructions to reproduce the bug (in README.md > Instructions to reproduce jstl - el - read element from map). See my other question for the format of my jsp files. I still haven't figured this out

This works :

<%
    HttpSession ses = (HttpSession)  request.getSession(false);
    if(ses != null) {
        HashMap<String, String> m = (HashMap<String, String>) ses.getAttribute("messages");
        if(m != null) { %>
            <%=m.get(request.getParameter("r")) %>
    <%}
    }
%>

This doesn't :

<c:set var="key" value="${param.r}"/>
<c:out value="${key}" /> <!-- does print what I expect -->
<%-- ${sessionScope.messages} prints nothing --%>
sess scope : ${sessionScope} <%-- prints sess scope : {}  --%>
<c:out value="${sessionScope.messages[key]}" /> <!-- nothing !? -->

why ?

jstl-1.2.1 - tried also with 1.2

The relevant jsp (error_begin.jsp - complemented by an error_end.jsp containing just a closing }) :

<%@ page language="java" pageEncoding="UTF-8"%><!-- noway to include this ONCE -->
<%@page import="java.util.HashMap"%>
<% if(request.getAttribute("ErrorString") != null){ %>
Υπήρξε λάθος : ${requestScope.ErrorString}
<%} else { %>
<c:if test="${param.r != null}">
    <div class="success" align="center">
        <%
        HttpSession ses = (HttpSession)  request.getSession(false);
        if(ses != null){
            HashMap<String, String> m = (HashMap<String, String>) ses.getAttribute("messages");
            if(m != null){ %>
        <%=m.get(request.getParameter("r")) %>
        <%}
        }
     %><c:set var="key" value="${param.r}"></c:set>
<%--        <c:out value="${key}" /> --%>
<%--        ${sessionScope.messages[key]} --%>
<%--        ${sessionScope.messages} prints nothing --%>
        sess scope : ${sessionScope} <%-- prints {} --%>
<%--        <c:out value="${sessionScope.messages[key]}" /> --%>
<%--        <c:out value=" ${messages[param.r]}" /> --%>
    </div>
</c:if>
<c:forEach items="${messages}" var="entry">
    Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>

Relevant part of Web.xml :

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

WEB-INF/lib:

javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
joda-time-2.1-javadoc.jar
joda-time-2.1.jar
logback-classic-1.0.7.jar
logback-core-1.0.7.jar
mail.jar
mysql-connector-java-5.1.22-bin.jar
slf4j-api-1.7.2-javadoc.jar
slf4j-api-1.7.2-sources.jar
slf4j-api-1.7.2.jar
Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Did you try using ${messages[param.r]} ? – rickz Oct 13 '12 at 14:55
  • @rickz:Yes that is why I ended up setting a new var. Could it be a bug ? – Mr_and_Mrs_D Oct 13 '12 at 14:58
  • Care to explain why the downvote ? It is actually a very specific and accurate question - what's the matter ? – Mr_and_Mrs_D Oct 14 '12 at 11:28
  • What does `${sessionScope}` say? And `${sessionScope.messages}`? Note that you don't necessarily need to use `` for this, just EL in template text works as good (if it didn't you'd have severe environmental problems). – BalusC Oct 23 '12 at 16:41
  • Well, there's apparently nothing being stored in the session scope. – BalusC Oct 26 '12 at 14:13
  • @BalusC : But when I access the session scope from the scriptlet I get the map alright !? – Mr_and_Mrs_D Oct 26 '12 at 14:15
  • Is this observed in the same page on the same request? – BalusC Oct 26 '12 at 14:15
  • @BalusC: Ja ! Output `Η εργασία διεγράφη ! sess scope : {} ` - where the greek are the `<%=m.get(request.getParameter("r")) %>`. Can it be because I ommit a `}` (added in the error_end.jsp) – Mr_and_Mrs_D Oct 26 '12 at 14:19
  • This is strange. Tomcat 7, you said? How is the root declaration of webapp's `web.xml`? Is it conform Servlet 3.0? And you don't have containerspecific libraries in `/WEB-INF/lib`? – BalusC Oct 26 '12 at 14:23
  • @BalusC : edited the question again - I begin to see :) – Mr_and_Mrs_D Oct 26 '12 at 14:45
  • @BalusC : changing the `xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"` to `xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"` or `xmlns:web="http://java.sun.com/xml/ns/javaee"` did not help - see also : https://bugs.eclipse.org/bugs/show_bug.cgi?id=333259 – Mr_and_Mrs_D Oct 26 '12 at 15:25
  • What if you include <%@ page session="true" %> directive in your jsp page? – Maria Ioannidou Apr 14 '13 at 16:18
  • @MariaIoannidou : I can't even login :D - the whole site is built with page session false - to not create unnecessary sessions - will try to overcome it and post back but the session is there alright – **EDIT** you are right ! Please post an explanation - why the JSTL needs the "jsp session" - while the scriptlets get the session alright ?? Feel free to add _as many details and (preferably official) links as you want_ and I will be sure to accept your answer :) - check the [filters](https://github.com/Utumno/ted2012/tree/GitHub2/src/com/ted/filters) package for details on session logic – Mr_and_Mrs_D Apr 14 '13 at 16:41

1 Answers1

0

You should include <%@ page session="true" %> directive in your JSP page.

The session Attribute of page Directive In JSP is true by default but may have been set to false at a global location of the site.

If the value of session attribute is true then the session object refers to the current or a new session because the client must be in the HTTP session for running the JSP page on the server. If you set the value of session object false then you can not use the session object or element with scope="session" in JSP page.

See:

http://www.roseindia.net/jsp/SessionAttribute.shtml

http://www.tutorialspoint.com/jsp/page_directive.htm

Why set a JSP page session = "false" directive?

Problem with session attributes in JSP EL using Spring MVC

UPDATE:

And from JSP 2.2 Specification:

page directive - session attribute:

Indicates that the page requires participation in an (HTTP) session.

If true then the implicit script language variable named session of type javax.servlet.http.HttpSession references the current/new session for the page.

If false then the page does not participate in a session; the session implicit variable is unavailable, and any reference to it within the body of the JSP page is illegal and shall result in a fatal translation error.

Default is true.

Community
  • 1
  • 1
Maria Ioannidou
  • 1,544
  • 1
  • 15
  • 36
  • You edited while I was commenting :D ---> _can you explain why is the session available in the **scriptlet** code_ ? If I put `session="true"` on all pages that need to be true and the user session times out - I have filters on those pages checking if `request.getSession(false) == null` - will the jsps ___create a session before the filters are run___ (so `request.getSession` won't be null) ? (aside - don't read roseindia - block it - very bad code) – Mr_and_Mrs_D Apr 14 '13 at 17:25
  • For the first part of your comment: As stated in the spec, when set to false then the implicit script language variable named session is unavailable. That does not mean that you cannot access the session using request.getSession() in your scriptlets. So, if session attribute false: ${messages} \\Returns null ${sessionScope.messages} \\Returns null <%=session.getAttribute("messages")%> \\Throws exception: cannot find variable session <%=request.getSession().getAttribute("messages")%> \\Works fine - returns session attribute – Maria Ioannidou Apr 14 '13 at 17:43
  • Thanks - problem with `session="true"` is it creates a session when there is none - do you know if this is done before or after the filters are run (that is my second question essentially) ? – Mr_and_Mrs_D Apr 14 '13 at 17:49
  • For the second part, I do not know about the filters-directives execution order, but I think that using request.getSession() method will anyhow create a session if there’s not one already there. – Maria Ioannidou Apr 14 '13 at 17:55
  • And as for the roseindia link, I don't use it normally, but it gives a rather easy to understand example for the session attribute case, even with bad code ;) – Maria Ioannidou Apr 14 '13 at 17:57
  • yes of course - but I use `request.getSession(false)` so the session won't be created. I am running some tests so I can understand if the filters are run first - I think it's ok - if you could provide some info on the logic the session is created by the jsp you'll make it for a good answer indeed – Mr_and_Mrs_D Apr 14 '13 at 18:32
  • The filters are run first - before the damn creation of session by the jsp - thank God - feel free to add info on this (and to have a look at my [other question](http://stackoverflow.com/questions/12662172/page-pageencoding-utf-8-ignored-when-included-from-another-jsp?lq=1)) – Mr_and_Mrs_D Apr 14 '13 at 23:30
  • Interesting, thank you for sharing that. I actually had never tried such a compination. I'm having a look at your other question too. Thanks a lot for the bounty too. – Maria Ioannidou Apr 15 '13 at 07:13
  • And may be we should change the title of this question to something more applicable for the session attribute related problem? – Maria Ioannidou Apr 15 '13 at 07:22