packagecom.JAVABean;
importjava.util.HashMap;
importjava.util.Map;

publiccla***egister{
privateStringname;
privateStringage;
privateStringemail;
privateMap<String,String>errors=null;//声明一个保存全部错误信息的map集合
publicRegister(){//在构造方法中初始化属性
this.name="";
this.age="";
this.email="";//初始化age属性
this.errors=newHashMap<String,String>();//实例化map对象,保存错误信息
}
publicbooleanisRigtht(){
booleanflag=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("errmail","输入的eamil地址不合法");
}
if(!this.age.matches("\\d+")){
flag=false;
this.age="";
errors.put("errage","年龄只能是数字");
}
returnflag;//返回标志位
}
publicStringgetErrorMsg(Stringkey){//取出对应的错误信息
Stringvalue=this.errors.get(key);//从map中根据对应的key取出对应的内容
returnvalue==null?"":value;//返回value对应的内容
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetAge(){
returnage;
}
publicvoidsetAge(Stringage){
this.age=age;
}
publicStringgetEmail(){
returnemail;
}
publicvoidsetEmail(Stringemail){
this.email=email;
}
}

*****************************************************index--主界面

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<%request.setCharacterEncoding("UTF-8");//%>
<jsp:useBeanid="reg"scope="request"class="com.JAVABean.Register"/>
<title>javabean登录验证</title>
</head>

<body>
<formmethod="post"action="check.jsp">
<p>请输入喜欢的事务:</p>
<p>用户名:<inputtype="text"name="name"value="<jsp:getPropertyname="reg"property="name"/>"/>
<%=reg.getErrorMsg("errname")%><br>
</p>
<p>年龄:<inputtype="text"name="age"value="<jsp:getPropertyname="reg"property="age"/>"/>
<%=reg.getErrorMsg("errage")%><br>
</p>
<p>e_mail:<inputtype="text"name="email"value="<jsp:getPropertyname="reg"property="email"/>"/>
<%=reg.getErrorMsg("errmail")%><br>
</p>
<p><inputtype="submit"value="注册"name="btn1"/>
<inputtype="reset"value="重置"name="btn2"/>
</p>
</form>
</body>
</html>

********************************************************check

<%@pagecontentType="text/html"import="java.util.*"pageEncoding="UTF-8"%>
<%@pageimport="java.sql.*"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>thisischeck</title>
<%request.setCharacterEncoding("UTF-8");//%>
<jsp:useBeanid="reg"scope="request"class="com.JAVABean.Register"/>
<jsp:setPropertyproperty="*"name="reg"/><!--//为属性自动赋值-->
</head>
<body>
<%if(reg.isRigtht()){%>
<jsp:forwardpage="success.jsp"></jsp:forward>
<%}else{%>
<jsp:forwardpage="index.jsp"></jsp:forward>
<%}%>

</body>
</html>

*****************************success

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>javaBEAN登录成功</title>
<%request.setCharacterEncoding("UTF-8");//%>
<jsp:useBeanid="reg"scope="request"class="com.JAVABean.Register"/>
</head>

<body>
<h3align="center">成功</h3>

登录成功,的用户名<br>
用户名:<jsp:getPropertyname="reg"property="name"/><br>
年龄:<jsp:getPropertyname="reg"property="age"/><br>
e-mail:<jsp:getPropertyname="reg"property="email"/><br>
</body>
</html>