Java Web笔记:JavaBean实例之注册验证

本篇来写一个jsp+JavaBean完成的用户注册程序,主要逻辑是:用户输入表单内容——发送到JavaBean进行验证——验证成功跳转到详细信息页面——验证失败指出错误。

主要页面为:

index.jsp、check.jsp、success.jsp、Register.java


Register.java

package com.zzh.test;

import java.util.HashMap;
import java.util.Map;

public class Register {
	private String name;
	private String age;
	private String email;
	private Map<String, String> errors = null;

	public Register() {
		this.name = "";
		this.age = "";
		this.email = "";
		this.errors = new HashMap<String, String>();
	}

	public boolean isValidate() {
		boolean flag = true;
		if (!this.name.matches("\\w{6,15}")) {
			flag = false;
			this.name = "";
			errors.put("errname", "用户名是6-15位的字母或数字!");
		}
		if (!this.email.matches("\\w+@\\w+\\.\\w+\\.?\\w*")) {
			flag = false;
			this.email = "";
			errors.put("erremail", "输入的email地址不合法!");
		}
		if (!this.age.matches("\\d+")) {
			flag = false;
			this.age = "";
			errors.put("errage", "年龄只能是数字!");
		}
		return flag;
	}

	public String getErrorMsg(String key) {

		String value = this.errors.get(key);
		return value == null ? "" : value;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
}


index.jsp

<%@ 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>
<jsp:useBean id="reg" scope="request" class="com.zzh.test.Register" />
<body>
	<form action="check.jsp" method="post">
		用户名:<input type="text" name="name"
			value="<jsp:getProperty name="reg" property="name"/>">
		<%=reg.getErrorMsg("errname")%><br> 
		年龄: <input type="text"
			name="age" value="<jsp:getProperty name="reg" property="age"/>">
		<%=reg.getErrorMsg("errage")%><br> 
		E-mail:<input type="text"
			name="email" value="<jsp:getProperty name="reg" property="email"/>">
		<%=reg.getErrorMsg("errmail")%><br>
		<input type= "submit" value ="注册">
		<input type= "reset" value = "重置">
	</form>
</body>
</html>


check.jsp

<%@ 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>
<jsp:useBean id="reg" scope="request" class="com.zzh.test.Register" />
<jsp:setProperty name="reg" property="*" />
<body>
	<%
		if (reg.isValidate()) {
	%>
	<jsp:forward page="success.jsp" />
	<%
		} else {
	%>
	<jsp:forward page="index.jsp" />
	<%
		}
	%>
</body>
</html>


success.jsp

<%@ 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>
<jsp:useBean id="reg" scope="request" class="com.zzh.test.Register" />
<body>
	用户名:<jsp:getProperty name ="reg" property="name"/><br>
	年龄:<jsp:getProperty name ="reg" property="age"/><br>
	Email:<jsp:getProperty name ="reg" property="email"/><br>
</body>
</html>



你可能感兴趣的:(Java Web笔记:JavaBean实例之注册验证)