日期:2014-05-18  浏览次数:20884 次

关于Servlet读取Html表单数据的问题
package cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowParameters extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletExceptionIOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("<HEAD><TITLE>Reading AllRequest Parameters</TITLE></HEAD>");
out.println("<BODY>");
out.print("<h1 align=center>");
out.print("Reading AllRequest Parameters");
out.println("</h1>");

Enumeration paramNames=request.getParameterNames();
int i=0;

while(paramNames.hasMoreElements()){
i++;
String paramName=(String)paramNames.nextElement();
out.print("<p align=center>"+i+"  "+paramName+"</p>");
}

out.println("</BODY>");
out.println("</HTML>");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ShowParametersFrom.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>
<h1 align="center">
A Sample FORM using POST
</h1>
<form action="servlet/ShowParameters" method="post">
Item Number:
<input type="text" name="itemNum" />
<br />
Quantity:
<input type="text" name="quantity" />
<br />
Price Each:
<input type="text" name="price" value="$" />
<br />
<hr />
First Name:
<input type="text" name="firstname" />
<br />
Last Name:
<input type="text" name="lastname" />
<br />
Middle Initial:
<input type="text" name="intitial" />
<br />
Shipping Address:
<textarea name="address"&