流程

Eclipse配置Tomcat.新建Dinamic Web Project
Class.
doGet
doPost
XML
servlet
index.html

实现

浏览器填写,提交表单:

得到信息,并浏览器返回结果

代码

class

package hServlet;

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 hServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
       

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setCharacterEncoding("UTF-8");
	String username= request.getParameter("username");
	String password= request.getParameter("password");
	System.out.println(username);
	System.out.println(password);
	String[] hobby= request.getParameterValues("hobby");
		
		
	response.setCharacterEncoding("UTF-8");
//		response.addHeader("Context-Type", "text/html;charset=utf-8");
	response.setContentType("text/html;charset=utf-8");
	PrintWriter out= response.getWriter();
	out.println("恭喜"+username+"注册成功!<br>");
	out.println("===========================<br>");
	out.println("请妥善保存好您的账户信息!<br>");
	out.println("===========================<br>");
	out.println("用户名:"+username+"<br>");
	out.println("密码:"+password+"<br>");
	out.println("爱好:");
	for (String i:hobby) {
		out.println(i+"/");
	}
	out.println("<br>===========================<br>");
	out.println("感谢您的光临!");
//		response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doGet(request, response);
}

}

xml servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   id="WebApp_ID" version="2.5">
<display-name>httpServlet</display-name>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
  <description></description>
  <display-name>hServlet1</display-name>
  <servlet-name>hServlet1</servlet-name>
  <servlet-class>hServlet.hServlet1</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>hServlet1</servlet-name>
  <url-pattern>/hServlet1</url-pattern>
</servlet-mapping>
</web-app>

html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<form action="hServlet1" method="post"> 
用户名:<input name="username" type="text">
密码:<input name="password" type="password">
<br>
爱好:<input name="hobby" type="checkbox" value="篮球">篮球
<input name="hobby" type="checkbox" value="足球">足球
<input name="hobby" type="checkbox" value="排球">排球
<br>
<input type="submit" value="注册">
</form>
</body>
</html>