• JSP+JavaBean 登陆验证


    1.java

     1 package cn.gs.ly;
     2 
     3 import java.util.HashMap;
     4 import java.util.Map;
     5 
     6 public class Register {
     7     private String name;
     8     private String age;
     9     private String email;
    10     private Map<String,String>errors = null;  //存放登陆错误  提示信息
    11     
    12     public Register() {
    13         name="";
    14         age="";
    15         email="";
    16         errors = new HashMap<String, String>();
    17     }
    18     //定义一个方法,数据验证操作
    19     public boolean isValidate(){
    20         boolean flag = true;
    21         if(!(this.name.matches("\w{2,6}"))){  //输入不符合正则表达式
    22             flag = false;  //触发flag
    23             this.name="";  //name 赋空值
    24             errors.put("errname", "请输入2~6位的**"); //登陆错误提示信息 放入集合
    25         }
    26         if(!(this.age.matches("\d{2}"))){
    27             flag = false;
    28             this.age="";
    29             errors.put("errage", "请输入2位的数字");
    30         }
    31         if(!(this.email.matches("[1-9]\d{4,10}@[qQ][qQ].com"))){
    32             flag = false;
    33             this.email="";
    34             errors.put("erremail", "请输入正确的邮箱");
    35         }
    36         return flag;
    37     }
    38     //定义一个语言    登陆错误提示信息
    39     public String getErrorMsg(String key){
    40         String value = this.errors.get(key); 
    41         return value==null?"":value;  //value为null赋空值 ,不为null 赋值value
    42     }
    43     
    44     public String getName() {
    45         return name;
    46     }
    47     public void setName(String name) {
    48         this.name= name;
    49     }
    50     public String getAge() {
    51         return age;
    52     }
    53     public void setAge(String age) {
    54         this.age = age;
    55     }
    56     public String getEmail() {
    57         return email;
    58     }
    59     public void setEmail(String email) {
    60         this.email = email;
    61     }
    62     
    63 }
     1 package cn.gs.ly;
     2 
     3 import java.util.HashMap;
     4 import java.util.Map;
     5 
     6 public class Register {
     7     private String name;
     8     private String age;
     9     private String email;
    10     private Map<String,String>errors = null;  //存放登陆错误  提示信息
    11     
    12     public Register() {
    13         name="";
    14         age="";
    15         email="";
    16         errors = new HashMap<String, String>();
    17     }
    18     //定义一个方法,数据验证操作
    19     public boolean isValidate(){
    20         boolean flag = true;
    21         if(!(this.name.matches("\w{2,6}"))){  //输入不符合正则表达式
    22             flag = false;  //触发flag
    23             this.name="";  //name 赋空值
    24             errors.put("errname", "请输入2~6位的**"); //登陆错误提示信息 放入集合
    25         }
    26         if(!(this.age.matches("\d{2}"))){
    27             flag = false;
    28             this.age="";
    29             errors.put("errage", "请输入2位的数字");
    30         }
    31         if(!(this.email.matches("[1-9]\d{4,10}@[qQ][qQ].com"))){
    32             flag = false;
    33             this.email="";
    34             errors.put("erremail", "请输入正确的邮箱");
    35         }
    36         return flag;
    37     }
    38     //定义一个语言    登陆错误提示信息
    39     public String getErrorMsg(String key){
    40         String value = this.errors.get(key); 
    41         return value==null?"":value;  //value为null赋空值 ,不为null 赋值value
    42     }
    43     
    44     public String getName() {
    45         return name;
    46     }
    47     public void setName(String name) {
    48         this.name= name;
    49     }
    50     public String getAge() {
    51         return age;
    52     }
    53     public void setAge(String age) {
    54         this.age = age;
    55     }
    56     public String getEmail() {
    57         return email;
    58     }
    59     public void setEmail(String email) {
    60         this.email = email;
    61     }
    62     
    63 }

    2.jsp

     1 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html>
     3 <html>
     4 <head>
     5 <title>index</title>
     6 </head>
     7 <body>
     8     <jsp:useBean id="reg" scope="request" class="cn.gs.ly.Register" ></jsp:useBean>
     9     
    10     <form action="check.jsp" method="post">
    11             <table border="1" cellpadding="0" cellspacing="0">
    12                 <tr>
    13                     <td>姓名:</td>
    14                     <td><input type="text" name="name" value='<jsp:getProperty property="name" name="reg" />'><%=reg.getErrorMsg("errname") %></td>
    15                 </tr>
    16                 <tr>
    17                     <td>年龄:</td>
    18                     <td><input type="text" name="age" value='<jsp:getProperty property="age" name="reg" />'><%=reg.getErrorMsg("errage") %> </td>
    19                 </tr>
    20                 <tr>
    21                     <td>邮箱:</td>
    22                     <td><input type="text" name="email" value='<jsp:getProperty property="email" name="reg" />'><%=reg.getErrorMsg("erremail")%> </td>
    23                 </tr>
    24             </table>
    25                 <input type="submit" value="提交"  />
    26         </form>
    27 </body>
    28 </html>
     1 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html>
     3 <html>
     4 <head>
     5 <title>check login</title>
     6 </head>
     7 <body>
     8     <jsp:useBean id="reg" scope="request" class="cn.gs.ly.Register" ></jsp:useBean>
     9     <jsp:setProperty name="reg" property="*" />        
    10     <%        
    11         if(reg.isValidate()){                                
    12     %>
    13         <jsp:forward page="success.jsp" />
    14     <%                 
    15         }else{
    16     %>
    17         <jsp:forward page="index.jsp" />
    18     <%                 
    19         }
    20     %>
    21 </body>
    22 </html>
     1 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html>
     3 <html>
     4 <head>
     5 <title>login success</title>
     6 </head>
     7 <body>
     8     <%
     9     request.setCharacterEncoding("UTF-8");
    10     response.setCharacterEncoding("UTF-8");
    11     %>        
    12     <jsp:useBean id="reg" scope="request" class="cn.gs.ly.Register" ></jsp:useBean>
    13     <h1>登陆成功,登陆信息为:</h1><hr color="red">
    14     <h1>姓名:<jsp:getProperty property="name" name="reg" /></h1><hr color="red">
    15     <h1>年龄:<jsp:getProperty property="age" name="reg" /></h1><hr color="red">
    16     <h1>邮箱:<jsp:getProperty property="email" name="reg" /></h1><hr color="red">
    17 </body>
    18 </html>
    19     
  • 相关阅读:
    Codeforces 1009F Dominant Indices
    UOJ #35 后缀排序 哈希做法
    bzoj 3670 [Noi2014]动物园
    动态规划 笔记

    常用模块和面向对象 类
    常用模块
    包的使用和常用模块
    日志
    复习列表,模块
  • 原文地址:https://www.cnblogs.com/liuyangv/p/8135383.html
Copyright © 2020-2023  润新知