gpt4 book ai didi

java - JSP 中调用 session 属性

转载 作者:行者123 更新时间:2023-12-02 05:50:28 24 4
gpt4 key购买 nike

我目前有以下内容:

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 页面中捕获 session 变量时,它采用“Object”数据类型。我们必须将它们解析为字符串。为此,我们可以使用 .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>

关于java - JSP 中调用 session 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23578770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com