gpt4 book ai didi

java - 从一个jsp获取值到另一个jsp

转载 作者:行者123 更新时间:2023-11-30 04:28:12 25 4
gpt4 key购买 nike

我开发了简单的网络应用程序。在一个jsp中,我使用getattribute和set attribute从先前的servlet文件获取值。我得到了值。但现在我希望该值从当前 jsp 到另一个 jsp 文件。我使用了 getattribute 和 setattribute,但值应显示为 null。

第一个jsp文件:

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="javax.servlet.http.*" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="payment.jsp" method="POST">
<h1>Confirmation</h1>
<%
Integer amount=(Integer)request.getAttribute("amt");
String service=(String )request.getAttribute("service");
String month=(String )request.getAttribute("month");
Integer day=(Integer)request.getAttribute("day");
String time=(String)request.getAttribute("time");
String city=(String)request.getAttribute("city");
out.println("<center>");
out.println("<table><tr>");
out.println("<td><h2>Service:"+service+"</td></tr>");
out.println("<tr><td><h2>Month:"+month+"</td></tr>");
out.println("<tr><td><h2>Date:"+day+"</td></tr>");
out.println("<tr><td><h2>Time:"+time+"</td></tr>");
out.println("<tr><td><h2>City:"+city+"</td></tr>");
out.println("<tr><td><h2>Total Amount:Rs."+amount+"</td></tr>");
out.println("</center>");
request.setAttribute("amt",amount);







%>


<center>
<input type="submit" value="Confirm"></input>
</center>

</form>
</body>
</html>

付款.jsp:

            <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Select bank</h2>
<%!Integer money;%>
<%
if(request.getAttribute("amt")!=null)
{
money=(Integer) request.getAttribute("amt");
out.println(""+money);
}
%>
</body>
</html>

最佳答案

您可以使用session而不是request隐式对象。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="payment.jsp" method="POST">
<h1>Confirmation</h1>
<%
Integer amount=(Integer)request.getAttribute("amt");
String service=(String )request.getAttribute("service");
String month=(String )request.getAttribute("month");
Integer day=(Integer)request.getAttribute("day");
String time=(String)request.getAttribute("time");
String city=(String)request.getAttribute("city");
out.println("<center>");
out.println("<table><tr>");
out.println("<td><h2>Service:"+service+"</td></tr>");
out.println("<tr><td><h2>Month:"+month+"</td></tr>");
out.println("<tr><td><h2>Date:"+day+"</td></tr>");
out.println("<tr><td><h2>Time:"+time+"</td></tr>");
out.println("<tr><td><h2>City:"+city+"</td></tr>");
out.println("<tr><td><h2>Total Amount:Rs."+amount+"</td></tr>");
out.println("</center>");
session.setAttribute("amt",amount);//Changed to session







%>


<center>
<input type="submit" value="Confirm"></input>
</center>

</form>
</body>
</html>

付款.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Select bank</h2>
<%!Integer money;%>
<%
if(session.getAttribute("amt")!=null)//changed to session
{
money=(Integer) session.getAttribute("amt");//changed to session
out.println(""+money);
session.removeAttribute("amt");
}
%>
</body>
</html>

关于java - 从一个jsp获取值到另一个jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318986/

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