`

[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 = "[a-zA-Z1-9_]{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 = "[0-9]{6}";
		return regex(regex,value);		
	}
	
	public static boolean checkCertificate(String value)
	{
		String regex = "^[0-9]{15}$|^[0-9]{18}$";
		return regex(regex,value);		
	}
	
	public static boolean checkTelno(String value)
	{
		//电话
		String regex = "^[0-9]{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 ="^[0-9]{1,10}$|^[0-9]{1,9}[.][0-9]{1}$|^[0-9]{1,8}[.][0-9]{1,2}$";
		return regex(regex,value);		
	}
	
	public static boolean isFloat7(String value) //价格不超过7位数字(可含两位小数)
	{
		String regex ="^[0-9]{1,7}$|^[0-9]{1,6}[.][0-9]{1}$|^[0-9]{1,5}[.][0-9]{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 = "^[0-9]{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传参数回表单页面,控制错误信息的显示.
分享到:
评论

相关推荐

    JAVA上百实例源码以及开源项目

    得到RSA密钥对,产生Signature对象,对用私钥对信息(info)签名,用指定算法产生签名对象,用私钥初始化签名对象,将待签名的数据传送给签名对象(须在初始化之后),用公钥验证签名结果,使用公钥初始化签名对象,用于...

    JAVA上百实例源码以及开源项目源代码

    得到RSA密钥对,产生Signature对象,对用私钥对信息(info)签名,用指定算法产生签名对象,用私钥初始化签名对象,将待签名的数据传送给签名对象(须在初始化之后),用公钥验证签名结果,使用公钥初始化签名对象,用于...

    JAVA面试题最全集

    1.Web安全性的考虑(表单验证、浏览器Basic方式的验证,应用程序的安全性,SSL,代码考虑) 2.简单介绍您所了解的MVC。 3.简单介绍所了解的XML。 4.文档和编码规范 5.Java中的分页、效率考虑。 6.简单介绍您所...

    java面试宝典

    166、如何用JQuery进行表单验证? 39 167、Prototype如何实现页面局部定时刷新? 40 168、Prototype如何为一个Ajax添加一个参数? 40 169、Ajax请求总共有多少种Callback? 41 170、Javascript如何发送一个Ajax请求?...

    net学习笔记及其他代码应用

    答:服务器端向客户端发送一个进程编号,一个程序域编号,以确定对象的位置。 24.在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别。 答:string str = null 是不给他分配...

    正则表达式

    如果想在正则表达式中使用特殊的标点符号,必须在它们之前加上一个 "\" . 2.字符类 将单独的直接符放进中括号内就可以组合成字符类.一个字符类和它所包含的任何一个字符都匹配,所以正则表达式 / [abc] / 和字母 ...

    千方百计笔试题大全

    166、如何用JQuery进行表单验证? 39 167、Prototype如何实现页面局部定时刷新? 40 168、Prototype如何为一个Ajax添加一个参数? 40 169、Ajax请求总共有多少种Callback? 41 170、Javascript如何发送一个Ajax请求?...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的WEB站点。本教程并不想让你完全了解这种语言,只是能使你尽快加入开发动态web站点的行列。我假定你有一些HTML...

    asp.net知识库

    Server Side ViewState 在服务器端存贮ViewState (ASP.NET 2.0) VS2005 ASP.NET本地化学习笔记&感受 在自定义Server Control中捆绑JS文件 Step by Step 深度解析Asp.Net2.0中的Callback机制 使用 Web 标准生成 ASP...

Global site tag (gtag.js) - Google Analytics