最后效果:
下面开始分析代码
文件结构(创建时可以直接勾选生成web.xml)
RegServlet.java中的代码
package com.lyq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class RegServlet extends HttpServlet{
private static final long serialVersionUID=1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("GB18030");
PrintWriter out=response.getWriter();
request.setCharacterEncoding("GB18030");
String name = request.getParameter("name");
// 获取密码
String password = request.getParameter("password");
// 获取性别
String sex = request.getParameter("sex");
// 获取qq号码
String qq = request.getParameter("qq");
// 获取邮箱地址
String mail = request.getParameter("mail");
out.println("用户名:" + name + "<br>");
// 输出密码
out.println("密码:" + password + "<br>");
// 输出性别
out.println("性别:" + sex + "<br>");
// 输出qq号码
out.println("qq号码:" + qq + "<br>");
// 输出E-Mail地址
out.println("E-Mail地址:" + mail + "<br>");
out.flush();
out.close();
}
}
web.xml中的代码
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>10_1</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>RegServlet</display-name>
<servlet-name>RegServlet</servlet-name>
<servlet-class>com.lyq.servlet.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/RegServlet</url-pattern>
</servlet-mapping>
</web-app>
<%@ page language="java" contentType="text/html;charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>用户注册</title>
</head>
<body>
<form action="RegServlet" method="post">
<table align="center"width="450">
<tr>
<td align="center" colspan="2">
<h2>用户注册</h2>
<hr>
</td>
</tr>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td align="right">性别:</td>
<td>
<input type="radio" name="sex" value="男" checked="checked">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td align="right">QQ号码:</td>
<td><input type="text" name="qq" /></td>
</tr>
<tr>
<td align="right">E-Mail地址:</td>
<td><input type="text" name="mail" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="注 册">
</td>
</tr>
</table>
</form>
</body>
</html>