[code]表单验证_2.用java在服务器端验证
因为做的是wap端的网站,所以不能用到js,所以验证的工作就要放在服务器端,太麻烦了.regedit.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"/> <title>注册页面</title><jsp:include page="include/css.jsp" flush="true" /></head><body><jsp:include page="include/head2.jsp" flush="true" /><form action="<%=basePath%>servlet/GetCheckno"><div>手机号:<br/><c:choose><c:when test="${mobno2==1}"><span style="color:red;"><b>手机号码格式不正确</b></span><br/></c:when><c:when test="${mobno2==3}"><span style="color:red;"><b>注册手机不能为空</b></span><br/></c:when></c:choose><input type="text" name="mobile" value="" /><br/><input class="submit" type="submit" value="获取验证码" /></div></form><form action="<%=basePath%>servlet/RegeditServlet" method=post><div>用户名:<br/><c:choose><c:when test="${userno==1}"><span style="color:red;"><b>会员名在5-20个字符内,仅可由中英文、数字、下划线组成</b></span><br/></c:when><c:when test="${userno==2}"><span style="color:red;"><b>该用户名已经存在</b></span><br/></c:when><c:when test="${userno==3}"><span style="color:red;"><b>用户名不能为空</b></span><br/></c:when></c:choose><input type="text" name="username" value="" /><br/>用户密码:<br/><c:choose><c:when test="${psw1no==1}"><span style="color:red;"><b>密码最少不能少于6位字符</b></span><br/></c:when><c:when test="${psw1no==3}"><span style="color:red;"><b>密码不能为空</b></span><br/></c:when></c:choose><input type="password" name="password" value="" /><br/>确认密码:<br/><c:choose><c:when test="${psw2no==1}"><span style="color:red;"><b>两次输入的密码不相同</b></span><br/></c:when><c:when test="${psw2no==3}"><span style="color:red;"><b>确认密码不能为空</b></span><br/></c:when></c:choose><input type="password" name="password2" value="" /><br/>注册手机:<br/><c:choose><c:when test="${mobno==1}"><span style="color:red;"><b>手机号码格式不正确</b></span><br/></c:when><c:when test="${mobno==3}"><span style="color:red;"><b>注册手机不能为空</b></span><br/></c:when></c:choose><input type="text" name="mobile" value="" /><br/>验证码:<br/><c:choose><c:when test="${chkno==1}"><span style="color:red;"><b>验证码格式不正确</b></span><br/></c:when><c:when test="${chkno==3}"><span style="color:red;"><b>验证码不能为空</b></span><br/></c:when></c:choose><input type="text" name="checkno" value="" /><br/>注册邮箱:<br/><c:choose><c:when test="${email==1}"><span style="color:red;"><b>邮箱格式不正确</b></span><br/></c:when><c:when test="${email==3}"><span style="color:red;"><b>邮箱不能为空</b></span><br/></c:when></c:choose><input type="text" name="email" value="" /><br/><a href="<%=basePath%>servlet/ShowGreetMent">阅读服务协议</a><br/><input class="submit" type="submit" value="同意并注册" /></div></form><jsp:include page="include/foot.jsp" flush="true" /></body></html>
处理的servlet.
package com.mobile.web.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.log4j.MDC;import com.mobile.dao.jdbc.AccountDaoJdbc;import com.mobile.dao.jdbc.ChecknoJdbc;import com.mobile.object.Account;import com.mobile.service.impl.AccountManagerImpl;import com.zhao.util.UtilBean;public class RegeditServlet extends HttpServlet {private Log logger = LogFactory.getLog(this.getClass());//日志操作private AccountDaoJdbc accountj;private ChecknoJdbc checkj;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request,response);}/*public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {boolean flat = true;int fail = 0;String mobile = request.getParameter("mobile");String checkno = request.getParameter("checkno");String username = request.getParameter("username");String password = request.getParameter("password");String password2 = request.getParameter("password2");String email = request.getParameter("email");String checknoSystem = checkj.getCheckno(mobile);String address="/RegeditSuccess.jsp";if(checkno.equals(checknoSystem)&&password.equals(password2)){Account account = new Account();account.setMob(mobile);account.setUser(username);account.setPsw(password);account.setEmail(email);accountj.create(account);}else{address = "/RegeditFail.jsp";if(!password.equals(password2)){fail=1;//两次密码不同address=address+"?fail=1";}else if(!checkno.equals(checknoSystem)){fail=2;//验证密不对address=address+"?fail=2";}}RequestDispatcher dispatcher = request.getRequestDispatcher(address);dispatcher.forward(request, response);}*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { MDC.put("method", "用户操作");//日志操作String address="/RegeditSuccess.jsp";boolean flat = true;int userno = 0;int psw1no =0 ;int psw2no =0;int mobno =0;int chkno =0;int emailno =0;String mobile = request.getParameter("mobile");String checkno = request.getParameter("checkno");String username = request.getParameter("username");String password = request.getParameter("password");String password2 = request.getParameter("password2");String email = request.getParameter("email");if(username.equals("")||username==null){userno=3;flat=false;}else{if(!UtilBean.checkUser(username)){userno=1;flat = false;}if(!AccountDaoJdbc.checkAccount(username)){userno=2;flat = false;}}if(password.equals("")||password==null){psw1no=3;flat = false;}else{if(!UtilBean.checkPsw(password)){psw1no=1;flat = false;}}if(password2.equals("")||password2==null){psw2no=3;flat = false;}else{if(!password.equals(password2)){psw2no=1;flat = false;}}if(mobile.equals("")||mobile==null){mobno=3;flat = false;}else{if(!UtilBean.checkMobile(mobile)){mobno=1;flat = false;}}if(checkno.equals("")||checkno==null){chkno=3;flat = false;}else{String checknoSystem = checkj.getCheckno(mobile);if(!checkno.equals(checknoSystem)){chkno=1;flat = false;}}if(email.equals("")||email==null){emailno=3;flat = false;}else{if(!UtilBean.checkEmail(email)){emailno=1;flat = false;}}//System.out.println(emailno);if(flat==true){Account account = new Account();account.setMob(mobile);account.setUser(username);account.setPsw(password);account.setEmail(email);accountj.create(account);MDC.put("username",username);//日志操作MDC.put("ip","127.0.0.1");//日志操作MDC.put("system", "前台WAP端");//日志操作logger.info("用户:"+username+"注册");//日志操作address="/RegeditSuccess.jsp";}else{address = "/regedit.jsp";request.setAttribute("userno", userno);request.setAttribute("psw1no", psw1no);request.setAttribute("psw2no", psw2no);request.setAttribute("mobno", mobno);request.setAttribute("chkno", chkno);request.setAttribute("emailno", emailno);}RequestDispatcher dispatcher = request.getRequestDispatcher(address);dispatcher.forward(request, response);}public void init() throws ServletException {accountj = new AccountDaoJdbc();checkj = new ChecknoJdbc();}}
工具类
package com.zhao.util;import java.sql.Timestamp;import java.text.DecimalFormat;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Random;import java.util.regex.Matcher;import java.util.regex.Pattern;public class UtilBean {/** * 判断字符串是否是整数 */public static boolean isInteger(String value) {try {Integer.parseInt(value);return true;} catch (NumberFormatException e) {return false;}}//得到前几天,后几天(月,年)的时间//年月日标记,正数前几天,负数后几天public static Timestamp getBeforeTime(int nyr,int nyrNum){long newlong = System.currentTimeMillis();Calendar calendar = GregorianCalendar.getInstance(); calendar.setTimeInMillis(newlong);calendar.add(nyr,nyrNum);Timestamp time = new Timestamp(calendar.getTimeInMillis());//System.out.println(UtilBean.getBeforeTime(Calendar.DAY_OF_MONTH, 100));//calendar.add(Calendar.MONTH, 1)return time;}public static boolean isFloat(String value) {try {Float.parseFloat(value);return true;} catch (NumberFormatException e) {return false;}}public static String getSixIntStr(){Random r = new Random();int sint = r.nextInt(1000000);DecimalFormat dfInt=new DecimalFormat("000000"); String str = dfInt.format(sint);return str;}public static boolean regex(String regex, String value){ Pattern p = Pattern.compile(regex); Matcher m = p.matcher(value); return m.find(); } public static boolean checkMobile(String value){//手机号码String regex = "\\d{11}"; //11位return regex(regex,value);}public static boolean checkUser2(String value){String regex = "{5,20}";return regex(regex,value);}public static boolean checkUser(String value){String regex = "\\w{5,20}";return regex(regex,value);}public static boolean checkEmail(String value){String regex = "[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+";return regex(regex,value);}public static boolean checkPsw(String value){String regex = "\\w{6}";return regex(regex,value);}public static boolean checkStr(String content,String filter){//要过虑的字符,用|分隔Matcher m=Pattern.compile(filter).matcher(content);if(m.find()){return true; //有非法字符}else{return false;}}public static boolean checkPostNo(String value){String regex = "{6}";return regex(regex,value);}public static boolean checkCertificate(String value){String regex = "^{15}$|^{18}$";return regex(regex,value);}public static boolean checkTelno(String value){//电话String regex = "^{6,20}$";return regex(regex,value);}public static boolean checkAddress(String value){//地址//String regex = "^\\w{1,100}$";//return regex(regex,value);if(value.length()<100&&value.length()>0)return true;return false;}public static boolean checkPerson(String value){//String regex = "^\\w{1,20}$";//return regex(regex,value);if(value.length()<20&&value.length()>0)return true;return false;}public static boolean isFloat2(String value) //价格不超过10位数字(可含两位小数){String regex ="^{1,10}$|^{1,9}[.]{1}$|^{1,8}[.]{1,2}$";return regex(regex,value);}public static boolean isFloat7(String value) //价格不超过7位数字(可含两位小数){String regex ="^{1,7}$|^{1,6}[.]{1}$|^{1,5}[.]{1,2}$";return regex(regex,value);}public static boolean String20(String value){//String regex = "^\\w{1,20}$";//return regex(regex,value);if(value.length()<20&&value.length()>0)return true;return false;}public static boolean String7int(String value){//String regex = "^\\w{1,20}$";//return regex(regex,value);if(value.length()<7&&value.length()>0&&isInteger(value))return true;return false;}public static boolean String500(String value){if(value.length()<500&&value.length()>0)return true;return false;}public static boolean String30(String value){if(value.length()<30&&value.length()>0)return true;return false;}public static boolean checkNum(String value){String regex = "^{1,4}$";return regex(regex,value);}public static boolean checkskren(String value)//收货人{//String regex = "^\\w{1,20}$";//return regex(regex,value);if(value.length()<25&&value.length()>0)return true;return false;}}
思路是servlet传参数回表单页面,控制错误信息的显示.
页:
[1]