register.java//用于生成javabean
package info.haowei.javabean; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class Register { 7 8 private String name; 9 private String age; 10 private String email; 11 private Map<String,String>errors = null; 12 13 14 15 public Register(){ 16 this.name= ""; 17 this.age=""; 18 this.email=""; 19 this.errors = new HashMap<String, String>(); 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 public String getAge() { 28 return age; 29 } 30 public void setAge(String age) { 31 this.age = age; 32 } 33 public String getEmail() { 34 return email; 35 } 36 public void setEmail(String email) { 37 this.email = email; 38 } 39 40 public boolean isValidate(){ 41 boolean flag = true; 42 if(!this.name.matches("\\w{6,15}")){ 43 flag = false; 44 this.name = ""; 45 this.errors.put("errorname", "用户名是6-15位的数字"); 46 } 47 if(!this.email.matches("\\w+@\\w+\\.\\w+\\.?\\w*")){ 48 flag = false; 49 this.email = ""; 50 this.errors.put("erroremail", "email地址不合法"); 51 } 52 if(!this.age.matches("\\d+")){ 53 flag = false; 54 this.age = ""; 55 this.errors.put("errorage", "age输入不合法"); 56 } 57 58 return flag; 59 } 60 61 public String getErrorMsg(String key){ 62 String value = this.errors.get(key); 63 if(value==null){ 64 return ""; 65 } 66 return value; 67 } 68 69 70 71 }
//check.jsp用于判断输入是否正确
<%@page contentType="text/html" pageEncoding="GBK"%> <html> <head> </head> <% request.setCharacterEncoding("GBK"); //解决乱码问题 %> <jsp:useBean id="reg" scope="request" class="info.haowei.javabean.Register"/> <jsp:setProperty property="*" name="reg"/> <body> <% if(reg.isValidate()){ %> <jsp:forward page="success.jsp"></jsp:forward> <% }else{ %> <jsp:forward page="index.jsp"></jsp:forward> <%} %> </body> </html>
//index.jsp 利用表单输入并通过javabean传递信息
1 <%@page contentType="text/html" pageEncoding="GBK"%> 2 <html> 3 <head> 4 </head> 5 <% request.setCharacterEncoding("GBK"); //解决乱码问题 %> 6 <jsp:useBean id="reg" scope="request" class="info.haowei.javabean.Register"/> 7 8 <body> 9 <form action="check.jsp" method="post"> 10 用户名:<input type="text" name="name" value="<jsp:getProperty name="reg" property="name"/>"/> 11 <%=reg.getErrorMsg("errorname") %><br> 12 年 龄:<input type="text" name="age" value="<jsp:getProperty name="reg" property="age"/>"/> 13 <%=reg.getErrorMsg("errorage") %><br> 14 email: <input type="text" name="email" value="<jsp:getProperty name="reg" property="email"/>"/> 15 <%=reg.getErrorMsg("erroremail") %><br> 16 17 <input type="submit" value = "submit"> 18 <input type="reset" value = "reset"> 19 </form> 20 </body> 21 </html>
// 注册成功界面
1 <%@page contentType="text/html" pageEncoding="GBK"%> 2 <html> 3 <head> 4 </head> 5 <% request.setCharacterEncoding("GBK"); //解决乱码问题%> 6 <jsp:useBean id="reg" scope="request" class="info.haowei.javabean.Register"/> 7 8 <body> 9 10 用户名:<jsp:getProperty name="reg" property="name"/><br> 11 12 年 龄:<jsp:getProperty name="reg" property="age"/><br> 13 14 email: <jsp:getProperty name="reg" property="email"/><br> 15 16 17 18 </body> 19 </html>