標籤:
本篇來寫一個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("\\[email protected]\\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執行個體之註冊驗證