日期:2014-05-16  浏览次数:20503 次

jsp +servlet 通过表单元素收集客户数据

准备工作:创建web工程名为servlet1

??????????????????在包下创建servlet名为GetDate.java

?????????????????? 在webcontent下创建文件夹JSP,在JSP文件夹下创建register.jsp

1.register.jsp代码

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="/servlet1/GetDate" method="post">
用户名:<input type="text" name="username"><br/>
密&nbsp;&nbsp;&nbsp;码:<input type="password" name="pass"><br/>
所在市:<select name="city">
<option value="北京">北京
<option value="西安">西安
<option value="广州">广州
</select><br/>
喜好:<input type="checkbox" name="看电视">看电视
<input type="checkbox" name="上网">上网
<input type="checkbox" name="逛街">逛街
<input type="checkbox" name="购物">购物
<br/>
性别:<input type="radio" name="gender" value="女">女
      <input type="radio" name="gender" value="男">男<br/>
简介:<textarea name="introduce" rows="6" cols="60"></textarea><br/>
<input type="submit"  value="提交">
</form>
</body>
</html>


?2.GetDate.java代码

package cn.getDate;

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 GetDate extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("content-type", "text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		Enumeration e=request.getParameterNames();
	while(e.hasMoreElements()){
		String name=(String)e.nextElement();
	    String value=request.getParameter(name);
	    out.write(name+"\t"+value);
	}
	}

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

}

?

?