1

试图在 JSP 页面之间传递数据(值)。希望在 HTML 代码中显示用户输入的相同姓名和年龄。

我的程序有 3 个文件。流量是

 inputname.html -> printname.jsp -> printclass.jsp
  1. inputname.html:用户输入他们的姓名和年龄
  2. printname.jsp : 显示姓名和年龄并输入他们的资格
  3. printclass.jsp :显示所有三个:姓名、年龄和资格。

问题是我无法获得正确的姓名和年龄值printclass.jsp;值为null.

相关代码来自printname.jsp

<html>
<head>
<title>Welcome</title>
</head>
<body>

<%  String Name=request.getParameter("name");%> 
<%  String Age=request.getParameter("age");%>   

<h1 >
welcome
<%= Name%>,
<%= Age %>
</h1>
 <br><br>

 <form method="POST" action="http://localhost:8090/htmltojsp1/printclass.jsp">


  <% request.setAttribute("name",Name) ; %>
  <% request.setAttribute("age",Age) ; %>


  <h3>choose a Class</h3><p>
  <select name="class" size="1">
     <option>School
     <option>College
     <option>bsc
      <option>mca 
  </select>
  <br> <br>
  <center>
    <input type="SUBMIT">
  </center>
  </form>


</body>
</html>

相关代码来自printclass.jsp

<html>

<body>
<h1 >

<%  String Name=request.getParameter("name");%> 
<%  String Age=request.getParameter("age");%>   

<h1 >
welcome
<%= Name%>,
<%= Age %>
</h1>
 <br><br>

<br><br>
<h2>you are in 
<%= request.getParameter("class")%>
 <h2>

 </body>
 </html>
4

1 回答 1

1

如果您使用 request.setAttribute ,这些将仅在当前 JSP 页面上可用 - 您下一页上的请求将设置为您在表单中拥有的内容(或在您的 URL 中的参数)。相反,您可以在表单中使用隐藏的输入元素:

<input type="hidden" name="name" value="<%= Name %>" />

顺便提一句。您应该在属性名称上使用小写字母。(所以使用名称而不是名称)。

于 2013-02-18T21:43:16.883 回答