1、用户注册页面代码
<%@ 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="yhzhuce" method="post"><table align="center" border="1" weight="200" height="200"><tr align="center"><td>请填写注册信息</td></tr>
<tr><td>帐号:<input type="text" name="zh"></td></tr>
<tr><td>密码:<input type="text" name="mima"></td></tr>
<tr><td>姓名:<input type="text" name="xingming"></td></tr><tr><td>性别:<input type="text" name="sex"></td></tr><tr><td>年龄:<input type="text" name="age"></td></tr><tr><td>手机号码:<input type="text" name="num"></td></tr><tr align="center"><td><input type="submit" value="提交信息"></td></tr></table></form></body></html>
2、Servlet接受并验证数据
package lianxi;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class yhzhuce extends HttpServlet {private static final long serialVersionUID = 1L;public yhzhuce() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html");response.setCharacterEncoding("UTF-8");String z=request.getParameter("zh");String m=request.getParameter("mima");String nm=request.getParameter("xingming");String s=request.getParameter("sex");String a=request.getParameter("age");String n=request.getParameter("num");if(nm!=""&&s!=""&&a!=""&&n!=""&&z!=""&&m!=""){response.getWriter().write("注册成功!");}else{response.getWriter().write("请3秒后重新填写资料!");response.setHeader("refresh", "3;URL=zhuceye.jsp");}//response.getWriter().append("Served at: ").append(request.getContextPath());}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
3、运行结果
【1】输入有空时,运行结果
2、输入完整时,运行结果