使用JSP获取文本框值

时间:2013-09-29 01:38:48

标签: jsp

我正在创建一个购物门户。在此门户网站中,我有一个购物车页面,显示所选项目,其中包含用于输入所需数量的文本字段。我在cart.jsp中使用while循环对它们进行了编号。我正在bill.jsp中检索这些值。我得到了第一个值,但其他值也与第一个值相同,而不是我们给出的值。有谁知道出了什么问题?

cart.jsp

<html>
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
<title>Shopping cart</title>

<style type="text/css">
                       * { font-size: 12px; font-family: Verdana }
</style>
<base href="%3C%=basePath%%3E">
<title>My JSP 'bill.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<img src="images/add.png" onclick="document.location='index.jsp'" alt="** PLEASE DESCRIBE THIS IMAGE **"> <img src="images/remove.png" onclick="document.location='remove.jsp'" alt="** PLEASE DESCRIBE THIS IMAGE **"> <img src="images/cart.png" onclick="document.location='cart.jsp'" alt="** PLEASE DESCRIBE THIS IMAGE **">
<h2>The shopping cart</h2>
<![CDATA[
    int i = 1;
    java.util.Enumeration content = session.getAttributeNames();
    out.println("<form name=f1 action=bill.jsp>");
    out.println("<table>");
    while (content.hasMoreElements()) {
        out.println("<tr>");
        out.println("<td>");
        out.println(i);
        out.println(content.nextElement());
        out.println("</td>");
        out.println("<td> <input type=text name=i>");
        out.println("</td>");
        i++;
    }
    out.println("</table>");
]]>
<a href="index.jsp"></a>
<h3><a href="index.jsp"><strong>Continue Shopping</strong></a></h3>
<a href="remove.jsp"><strong>Remove items From Cart</strong></a>
<form><input type="submit" value="generate bill"></form>

bill.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!-- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> -->
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
<![CDATA[ 
    int i = 1;
    java.util.Enumeration content = session.getAttributeNames();
    out.println("<form name=f2>");
    out.println("<table>");
    while (content.hasMoreElements()) {
        out.println("<tr>");
        out.println("<td>");
        out.println(i);
        out.println(content.nextElement());
        out.println("</td>");
        out.println("<td>");
        String a = request.getParameter("i");
        out.println(a);
        out.println("</td>");
        i++;
    }
    out.println("</table>");
    out.println("</form>");
]]>
<br>
</body>
</html>       

1 个答案:

答案 0 :(得分:1)

更改

String a = request.getParameter("i");

String[] a = request.getParameterValues("i");