我开发了simeple web应用程序。在一个jsp中,我使用getattribute和set属性从preivous servlet文件中获取值。我得到了值。但是现在我想从当前的jsp到另一个jsp文件的值。使用我使用的getattribute和setattribute但值应显示为null。
firstjsp文件:
<%@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>
payment.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>
答案 0 :(得分:1)
在HTML隐藏控件中设置您要在payment.jsp
中访问的任何值,如下所示: -
<form action="payment.jsp" method="POST">
<input type="hidden" name="amt" value="<%= amount%>" />
</form>
JSP中的请求对象仅跨越一个HTTP请求。因此,当您将请求转发到第一个JSP文件时,它只是一个请求。但是当您提交表单并加载payment.jsp
时,请求对象将被清除,因为它是对服务器的新HTTP请求。
答案 1 :(得分:1)
您可以使用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>
payment.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>
答案 2 :(得分:0)
为了访问您在第一个jsp中设置的值,最好以这种方式使用session来放置值。request.getSeession.setAttribute("amount",amount);
然后在第二个jsp访问它就像这样
<form>
<input type="hidden" value="<%=session.getAttribute("amount")%>"/>
<form>
我希望它能解决你的问题