重点学习:
1:<form name="form1" method="post" action="/webproject2/servlet/survey">
2:XML映射:
3: 用来得到前台表达中多个数据;
String os[] = request.getParameterValues("os");
out.print("<ul>");
for (int i = 0; i < os.length; i++) {
out.print("<li>" + os[i] + "</li>");
}
4:
============================survey.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<p>网络调查表
</p>
<form name="form1" method="post" action="/webproject2/servlet/survey">
<table width="386" border="0">
<tr>
<td width="178">姓名:</td>
<td width="192"><input name="name" type="text" id="name" size="40" maxlength="10"></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" type="text" id="email" size="40"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="radio" name="age" value="radiobutton">
小于18
<input type="radio" name="age" value="radiobutton">
18-40
<input type="radio" name="age" value="radiobutton">
40以上</td>
</tr>
<tr>
<td height="31">编程时间:</td>
<td><select name="code" id="code">
<option value="never" selected>不编程</option>
<option value="6">小于6个月</option>
<option value="6-12">6-12个月</option>
<option value="12-24">12-24个月</option>
<option value="24">大于24个月</option>
</select></td>
</tr>
<tr>
<td>操作系统:</td>
<td><select name="os" size="4" multiple id="os">
<option value="xp">winxp</option>
<option value="WIN2000" selected>windows2000</option>
<option value="WIN2003">WINDOW2003</option>
<option value="linux">lINUX</option>
</select></td>
</tr>
<tr>
<td>编程语言:</td>
<td><input name="language" type="checkbox" id="language" value="java">
JAVA
<input name="language" type="checkbox" id="language" value="c">
C
<input name="language" type="checkbox" id="language" value="c++">
C++
<input name="language" type="checkbox" id="language" value="csharp">
C#
<input name="language" type="checkbox" id="language" value="vb">
VB</td>
</tr>
<tr>
<td>建议:</td>
<td><textarea name="comment" cols="40" rows="5" id="comment"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="提交"></td>
<td><input type="reset" name="reset" value="重置"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
====================================web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginFormServlet</servlet-name>
<servlet-class>com.v512.webproject2.LoginFormServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SurveyServlet</servlet-name>
<servlet-class>com.v512.webproject2.SurveyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginFormServlet</servlet-name>
<url-pattern>/servlet/loginForm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SurveyServlet</servlet-name>
<url-pattern>/servlet/survey</url-pattern>
</servlet-mapping>
</web-app>
=====================================surveyServlet.java
package com.v512.webproject2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SurveyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
out.print("姓名:" + request.getParameter("name") + "<br>");
out.print("Email:" + request.getParameter("email") + "<br>");
out.print("年纪:" + request.getParameter("age") + "<br>");
out.print("编程时间:" + request.getParameter("code") + "<br>");
out.print("操作系统:");
String os[] = request.getParameterValues("os");
out.print("<ul>");
for (int i = 0; i < os.length; i++) {
out.print("<li>" + os[i] + "</li>");
}
out.print("</ul><br>");
out.print("编程语言:");
String language[] = request.getParameterValues("language");
out.print("<ul>");
for (int i = 0; i < language.length; i++) {
out.print("<li>" + language[i] + "</li>");
}
out.print("</ul><br>");
out.print("编程时间:" + request.getParameter("comment") + "<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}