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、輸入完整時,運行結果