我从Java Server Pages开始。我想制作一个包含三个步骤的注册表格。第一页index.jsp是一个包含两个字段的注册表单:Name和Surname。完成这两个字段后,我按下一个并传递到下一页PaginaDoi.jsp,在那里我完成地址和电话字段。在此之后,我在第三页PaginaTrei.jsp旁边传递所有数据,然后发送到数据库或退回以修改数据。
我想做的主要是在使用“下一步”和“后退”按钮后退和前进后,不加修改地存储所有字段。为此我做了一个会话,它工作正常,但当我从第三页回到第二页我的名字和姓氏重置为null。 我停留了大约一天,以了解我犯了错误但没有成功的地方。请帮帮我。
我提供以下代码:
的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script language="javascript" type="text/javascript">
function validateForm(){
var x=document.forms["formular"]["nume","prenume"].value;
if(x==null||x==""){
alert("Toate campurile sunt necesare");
return false;
}
}
</script>
<title>Formular de inregistrare</title>
</head>
<body>
<h1>Introduceti datele in campurile respective</h1>
<form name="formular" action="PaginaDoi.jsp" onsubmit="return validateForm()" method="post">
Nume: <input type="text" name="nume" value="<%=session.getAttribute("FirstName")!=null? session.getAttribute("FirstName"):""%>" maxlength="15"></br>
Prenume: <input type="text" name="prenume" value=" <%=session.getAttribute("LastName")!=null?session.getAttribute("LastName"):""%>" maxlength="10"></br>
<div id="subm"><input type="submit" value="Inainte"></div>
</form>
</body>
</html>
第二页(PaginaDoi.jsp)
<%String fname=request.getParameter("nume");
session.setAttribute("FirstName",fname);
String lname=request.getParameter("prenume");
session.setAttribute("LastName",lname);
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script language="javascript" type="text/javascript">
function validateForm(){
var x=document.forms["formular"]["telefon","adresa"].value;
if(x==null||x==""){
alert("Toate campurile sunt necesare");
return false;
}
}
</script>
<title>Formular de inregistrare</title>
</head>
<body>
<h1>Introduceti datele in campurile respective</h1>
Nume: <%=session.getAttribute("FirstName")%></br>
Prenume: <%=session.getAttribute("LastName")%></br>
<form name="formular" action="PaginaTrei.jsp" onsubmit="return validateForm()" method="post">
Telefon: <input type="text" name="telefon" value=" <%=session.getAttribute("ThePhone")!=null?session.getAttribute("ThePhone"):""%>" maxlength="10"></br>
Adresa: <input type="text" name="adresa" value=" <%=session.getAttribute("TheAddress")!=null?session.getAttribute("TheAddress"):""%>" maxlength="20"></br>
<div id="subm"><input type="submit" value="Inainte"></div>
</form>
<form action="index.jsp" method="post">
<div id="submInapoi"><input type="submit" value="Inapoi"></div>
</form>
</body>
</html>
第三页(PaginaTrei.jsp)
<%
String tel=request.getParameter("telefon");
session.setAttribute("ThePhone",tel);
String add=request.getParameter("adresa");
session.setAttribute("TheAddress",add);
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Datele introduse</title>
</head>
<body>
<h1>Introduceti datele in campurile respective</h1>
Nume: <%=session.getAttribute("FirstName")%></br>
Prenume: <%=session.getAttribute("LastName")%></br>
Telefon: <%=session.getAttribute("ThePhone")%></br>
Adresa: <%=session.getAttribute("TheAddress")%></br>
<form action="Salveaza.jsp" method="post">
<div id="subm"><input type="submit" value="Trimite"></div>
</form>
<form action="PaginaDoi.jsp" method="post">
<div id="submInapoi"><input type="submit" value="Inapoi"></div>
</form>
</body>
</html>
答案 0 :(得分:1)
虽然我不建议您按照自己的方式行事,但如果您在第二页中查看,则会根据您的请求参数在会话中设置属性。如果第三个表单上的链接转到第二个表单,那么您的请求参数将为空,并且您实际上是在此时从会话中删除信息。
您必须执行以下操作:
if (tel != null) session.setAttribute("ThePhone",tel);
等