简体   繁体   中英

Print session scoped attribute setted from a Servlet in a JSP page via JSTL

I want to set a session scoped value from a Java Servlet and print it to a JSP page using JSTL.

This is what I have tryed:

Added dependency in pom.xml:

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-api</artifactId>
  <version>8.0.1</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

Set attribute from Servlet:

    if (error) {
        HttpSession session = request.getSession(true);
        session.setAttribute("failureReason", "test error");
        response.sendRedirect("/com/test.jsp");
    }

Now, from test.jsp page if I write:

<c:out value="HERE"/>

I see the output on the page, how can I retrieve my failureReason attribute? This is all the way that I have already tryed:

        <c:if test="${not empty failureReason}">
            <c:out value="HERE"/>
        </c:if>

        <c:if test="${not empty sessionScope.failureReason}">
            <c:out value="HERE"/>
        </c:if>

        <c:if test="${sessionScope.failureReason != null}">
            <c:out value="HERE"/>
        </c:if>

        <c:if test="${failureReason != null}">
            <c:out value="HERE"/>
        </c:if>

        <c:if test="${!empty sessionScope.failureReason}">
            <c:out value="HERE"/>
        </c:if>

        <c:if test="${!empty failureReason}">
            <c:out value="HERE"/>
        </c:if>

Nothing seems to work.

Solved. I've edited the web.xml as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM