Domanda

I currently have the following:

HttpSession session = request.getSession();         
String discountError = (String) session.getAttribute("discountError");

if (discountError.equals("true")){
    session.setAttribute("discountAdded", "false");
    forwardPage = "DiscountEnd.jsp";

}
else if (discountError.equals("false")){
    session.setAttribute("discountAdded", "true");
    forwardPage = "Confirm.jsp";                
}

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page session="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
          "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Discount(s) Added Successfully</title>
</head>

<body>

<c:choose>
    <c:when test="${discountAdded == 'true'}">
        <p align="center">  
            All Discount(s) added successfully!     
        </p>
    </c:when>
    <c:when test="${discountAdded == 'false'}">
        <p align="center">
            Error Found! No Discounts added!
        </p>
    </c:when>
</c:choose>

<br>
<form action='HotelOwnerController' method='POST' style="text-align:center">
    <input  type="submit" name="action" value="Back to Welcome Screen"/>
</form>

When I get to the JSP page, I am finding that the discountAdded if condition is not being evaluated.

Would someone know how I can read session attributes in a JSP page?

È stato utile?

Soluzione 3

You can access the session attributes using sessionScope in your JSP.

So you need to call like this: ${sessionScope.discountAdded == 'true'}

<c:choose>
    <c:when test="${sessionScope.discountAdded == 'true'}">
        <p align="center">  
            All Discount(s) added successfully!     
        </p>
    </c:when>
    <c:when test="${sessionScope.discountAdded == 'false'}">
        <p align="center">
            Error Found! No Discounts added!
        </p>
    </c:when>
</c:choose>

Suggestion:

If discountAdded is going to contain only boolean value, then instead of <c:when> conditions, use <c:otherwise> for the another condition.

 <c:choose>
        <c:when test="${sessionScope.discountAdded == 'true'}">
            <p align="center">  
                All Discount(s) added successfully!     
            </p>
        </c:when>
        <c:otherwise>
            <p align="center">
                Error Found! No Discounts added!
            </p>
        </c:otherwise>
    </c:choose>

Altri suggerimenti

No, when session varialbles are catched in jsp page, it comes in "Object" data type. we have to parse them to String. For that we can use either .toString() method or we can cast it using

String str=(String)session.getAttribute("session_name");

OR

String str=(String)session.getAttribute("session_name");

For your code I suggest use sessionScope

<c:choose>
<c:when test="${sessionScope.discountAdded == 'true'}">
    <p align="center">  
        All Discount(s) added successfully!     
    </p>
</c:when>
<c:when test="${sessionScope.discountAdded == 'false'}">
    <p align="center">
        Error Found! No Discounts added!
    </p>
</c:when>

In JSP we can access session variable as follows:

<%
String str=session.getAttribute("session_name");

%>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top