JAVABEN+JSP登录

来源:互联网 发布:三星怎么看网络电视 编辑:程序博客网 时间:2024/06/07 23:04

java

package cn.anli.demo;
import java.util.*;  //导入包
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="";
    this.errors.put("errorname","6-15");
    }
        if(!this.age.matches("\\d+"))
    {
    flag=false;
    this.age="";
    this.errors.put("errorage","must number");
    }
        if(!this.email.matches("\\w+@\\w+\\.\\w.\\w*"))
    {
    flag=false;
    this.email="";
    this.errors.put("erroremail","email is not user");
    }
    return flag;
   }
   public String getErrorMsg(String key){
    String value=this.errors.get(key);
    return  value== null?"":value;
   }
    public void setName(String name){
    this.name=name;
   }
    public void setAge(String age){
     this.age=age;
    }
    public void setEmail(String email){
     this.email=email;
    }
    public String getName()
    {
      return this.name;    
    }
    public String getAge()
    {
      return this.age;    
    }
    public String getEmail()
    {
      return this.email;    
    }
}


login:

<%@ page contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
 <title>
 </title>
</head>
<body>
   <%
   request.setCharacterEncoding("utf-8");
   %>
   <jsp:useBean id="reg" scope="request" class="cn.anli.demo.Register"/>
   
<form action="check.jsp" method="post">
 姓名:<input type="text" name="name" value="<jsp:getProperty name="reg" property="name"/>"/><%=reg.getErrorMsg("errorname")%>
</br>
 年龄:<input type="text" name="age" value="<jsp:getProperty name="reg" property="age"/>"/><%=reg.getErrorMsg("errorage")%>
</br>
 email:<input type="text" name="email" value="<jsp:getProperty name="reg" property="email"/>"/><%=reg.getErrorMsg("erroremail")%>
<br>
<input type="submit" value="提交"/> <input type="reset" value="重置"/>
 </form>
</body>
</html>


check

<%@ page contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
 <title>
 </title>
</head>
<body>
   <%
   request.setCharacterEncoding("utf-8");
   %>
   <jsp:useBean id="reg" scope="request" class="cn.anli.demo.Register"/>
   
 <jsp:setProperty name="reg" property="*"/>
 <%
  if(reg.isValidate())
  {%>
 
   <jsp:forward page="success.jsp"/>
   <%
  }
  else
  {
  %>
    <jsp:forward page="login.jsp"/>
   
  <%
  }
 %>
</body>
</html>

success

<%@ page contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
 <title>
 </title>
</head>
<body>
   <%
   request.setCharacterEncoding("utf-8");
   %>
   <jsp:useBean id="reg" scope="request" class="cn.anli.demo.Register"/>
   

 姓名:<jsp:getProperty name="reg" property="name"/>
</br>
 年龄:<jsp:getProperty name="reg" property="age"/>
</br>
 email:<jsp:getProperty name="reg" property="email"/>
<br>

</body>
</html>


原创粉丝点击