繁体   English   中英

在JSP中调用会话属性

[英]Call Session Attributes in JSP

我目前有以下内容:

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条件。

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

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

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

要么

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>

您可以使用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>

在JSP中,我们可以访问会话变量,如下所示:

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

%>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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