在JSP中调用会话属性

时间:2014-05-10 08:22:09

标签: java jsp session

我目前有以下内容:

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>

当我到达JSP页面时,我发现discountAdded if条件未被评估。

有人知道如何在session页面中阅读JSP属性吗?

3 个答案:

答案 0 :(得分:2)

不,当在jsp页面中捕获会话变量时,它会以“Object”数据类型出现。我们必须将它们解析为String。为此,我们可以使用.toString()方法,也可以使用

进行投射
String str=(String)session.getAttribute("session_name");

OR

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

对于您的代码,我建议使用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>

答案 1 :(得分:0)

您可以使用JSP中的sessionScope访问会话属性。

所以你需要像这样打电话:${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>

<强>建议:

如果discountAdded仅包含布尔值,则代替<c:when>条件,而使用<c:otherwise>作为另一个条件。

 <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>

答案 2 :(得分:0)

在JSP中,我们可以按如下方式访问会话变量:

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

%>