当前位置:Gxlcms > 数据库问题 > 招新系统(jsp+servlet,实现简略前端网页注册登录+后台增删改查,分学生和管理员,Java语言,mysql数据库连接,tomcat服务器)

招新系统(jsp+servlet,实现简略前端网页注册登录+后台增删改查,分学生和管理员,Java语言,mysql数据库连接,tomcat服务器)

时间:2021-07-01 10:21:17 帮助过:19人阅读

学校工作室招新,一轮考核是写一个条目工作室的招新管理系统,心想那么怎么这么懒呢,让新生写完那么就可以直接买个云服务器拿来做下一年的招新了~。。。当然 我可能也进不去,里面大佬太多,我太菜。

架构说明

要求是采用MVC模式,所以分了下面的几个包,但是由于是第一次写,可能分的也不是很清楚:

这个是后台部分的架构:

 

技术图片

 

这个是前端的的展示:

技术图片

(那个StuLoginSuccess.html与StuLoginSuccess.jsp是重复的了,我在写后台时调用的是jsp那个,那个html是没用的。)

 

说明:

由于系统的架构必须先写后台,再从后台需要展示的时候写点前端,这里就按照我当时的步骤写就好了,不分啥前端篇后台篇的了哈哈哈。。。

 

步骤:

1)

首先写的当然就是学生类啦!

src下右键,新建一个pakage,名为pojo,旗下新建一个class文件,取名为student,敲:

 

package pojo;

public class student {
    private String id;
    private String name;
    private String sex;
    private String grade;
    private String phone;//防止电话号码过长
    private String decision;
    private String selfintroduction;
    private String password;
    
    public student(){
        
    }
    public student(String name,String sex,String id,String grade,String phone,String decision,String selfintroduction,String password){
        super();
        this.name=name;
        this.sex=sex;
        this.id=id;
        this.grade=grade;
        this.phone=phone;//防止电话号码过长
        this.decision=decision;
        this.selfintroduction=selfintroduction;
        this.password=password;
        
    }
    
    /*
     * 得到器
     */
    public String getId(){
        return id;
    }
    public String getName(){
        return name;
    }
    public String getSex(){
        return sex;
    }
    public String getGrade(){
        return grade;
    }
    public String getPhone(){
        return phone;
    }
    public String getDecision(){
        return decision;
    }
    public String getSelfintroduction(){
        return selfintroduction;
    }
    public String getPassword() {
        // TODO Auto-generated method stub
        return password;
    }

    /*
     * 更改器
     */
    public void setId(String id){
        this.id=id;
    }
    public void setName(String name){
        this.name=name;
    }
    public void  setSex(String sex){
        this.sex=sex;
    }
    public void  setGrade(String grade){
        this.grade=grade;
    }
    public void setPhone(String phone){
         this.phone=phone;
    }
    public void setDecision(String decision){
        this.decision=decision;
    }
    public void setSelfintroduction(String i){
        this.selfintroduction=i;
    }
    public void setPassword(String password){
        this.password=password;
    }
    
    @Override
    public String toString(){
        return "学生名字:"+name+"    性别:"+sex+"    学号(id):"+id+"    年级班级:"+grade+"电话:"+phone+"        方向(前端/后台):"+decision+"自我介绍:"+selfintroduction;
    }
    
}

 

(toString方法可以不要,就是打印到后台而已,在开发初期还没写html、jsp的时候有点用)

 

2)

然后连接数据库:

写这段代码前,你需要先下个mysql数据库(或者别的数据库),请自行网上查找学习。

src下右键,新建一个pakage,名为DbUtil,旗下新建一个class文件,取名为DbUtil,敲:

package DbUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class DbUtil {
    private static String dburl="jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=UTF-8";
    //用户名
    public static String userName="root";
    //密码
    public static String password="root";
    //数据驱动
    public static String jdbcName="com.mysql.jdbc.Driver";
    
    Connection con=null;
    /**
     * 获取数据库的连接
     * @return
     * @throws Exception
     */
    public Connection getcon() throws Exception {
        Class.forName(jdbcName);    //加载数据驱动
        con=DriverManager.getConnection(dburl, userName, password);//连接数据库
        System.out.println("数据库连接成功!");
        return con;
    }
    /**
     * 关闭连接
     * @param con
     * @throws Exception
     */
    public void close(Statement stmt,Connection con) throws Exception{
        if(stmt!=null){
            stmt.close();
            if (con!=null)
                con.close();
        }
    }
    public void close(PreparedStatement pstmt,Connection con) throws Exception{
        if(pstmt!=null){
            pstmt.close();
            if (con!=null)
                con.close();
        }
    }
}

 

3)

写dao层:

src下右键,新建一个pakage,名为Dao,旗下新建一个class文件,取名为Dao,敲:

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;


import DbUtil.DbUtil;
import pojo.student;
public class Dao {
    /**
     * 管理员 调用此方法  ,管理员增加用户信息将是null的密码
     * @param stu
     * @return
     * @throws Exception
     */
    public static int  AddStudent(student stu) throws Exception {
        DbUtil dbutil=new DbUtil();
        Connection con=dbutil.getcon();
        //注意要加个‘‘,否则会无法插入除数字以外的字符,除非修改数据库字符编码为gbk或者utf-8
        String sql="insert into everyone (id,name,grade,sex,decision,phone,selfintroduction) values (?,?,?,?,?,?,?)";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, stu.getId()); 
        pstmt.setString(2, stu.getName());
        pstmt.setString(3, stu.getGrade());
        pstmt.setString(4, stu.getSex());
        pstmt.setString(5, stu.getDecision());
        pstmt.setString(6, stu.getPhone());
        pstmt.setString(7, stu.getSelfintroduction());
//        pstmt.setString(8, stu.getPassword());
        int result=pstmt.executeUpdate();
        dbutil.close(pstmt, con);
        return result;
    }
    
    /**
     * 学生调用此方法,在注册的基础之上,增加原有的信息
     * @param stu
     * @return
     * @throws Exception
     */
    public static int  AddStudentStu2(student stu) throws Exception {
        DbUtil dbutil=new DbUtil();
        Connection con=dbutil.getcon();
        //注意要加个‘‘,否则会无法插入除数字以外的字符,除非修改数据库字符编码为gbk或者utf-8
        String sql="insert into everyone (id,name,grade,sex,decision,phone,selfintroduction) values (?,?,?,?,?,?,?)";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, stu.getId()); 
        pstmt.setString(2, stu.getName());
        pstmt.setString(3, stu.getGrade());
        pstmt.setString(4, stu.getSex());
        pstmt.setString(5, stu.getDecision());
        pstmt.setString(6, stu.getPhone());
        pstmt.setString(7, stu.getSelfintroduction());
//        pstmt.setString(8, stu.getPassword());
        int result=pstmt.executeUpdate();
        dbutil.close(pstmt, con);
        return result;
    }
    /**
     * 用户 调用此方法  注册用户信息
     * @param stu
     * @return
     * @throws Exception
     */
    public static int  AddStudentStu(student stu) throws Exception {
        DbUtil dbutil=new DbUtil();
        Connection con=dbutil.getcon();
        //注意要加个‘‘,否则会无法插入除数字以外的字符
        String sql="insert into everyone (phone,password,id) values (?,?,?)";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, stu.getPhone()); 
        pstmt.setString(2, stu.getPassword());
        pstmt.setString(3, stu.getId());
        int result=pstmt.executeUpdate();
        dbutil.close(pstmt, con);
        return result;
    }
    
    /**
     * 管理员删除用户信息,
     * 直接删除指定名字和学号的学生信息,
     * 如果有多条同名的学生信息,将删除全部同名的信息,
     * 因此改成名字和学号同时核验才执行删除
     * @author grass-in-life
     */
     public static int  DeleteStudent(String id) throws Exception{
            DbUtil dbutil=new DbUtil();
            Connection con=dbutil.getcon();
            String sql="delete from everyone where id=?";
            PreparedStatement pstmt=con.prepareStatement(sql);
            pstmt.setString(1, id);
            int result=pstmt.executeUpdate();
            dbutil.close(pstmt, con);
            return result;
        }
    
    
    /**
     * 调用修改类之前,先new 一个新student类,把要修改的学生那条数据的各方面信息都修改好,
     * 否则会置null给对应的列,
     * 因为在模板类student里我的构造方法里将给默认值null,false,0.
     * 解决:
     * cat1.2版本更新,不再提供学号和电话修改接口,故不需要每一项都修改,对应的数据库信息不得修改
     * 
     * 问题:修改不了name为中文的数据,故改为依据id修改哈哈哈
     * 问题:改为id修改也不行,数据库乱码,
     * 问题解决了,关键在于values那里没用一一对应,可以借助输出1,2,3,4...来判断哪个对应哪个
     * 注意:出现错误还得看看是不是模板类student出错
     * @author grass-in-life
     *
     */
    public static int  UpdateStudent(student stu) throws Exception{
        DbUtil dbutil=new DbUtil();
        Connection con=dbutil.getcon();
        String sql="update everyone set sex=?,grade=?,decision=?,selfintroduction=?,name=? where phone=? ";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, stu.getSex());
        pstmt.setString(2, stu.getGrade());
        pstmt.setString(3, stu.getDecision());
        pstmt.setString(4, stu.getSelfintroduction());
        pstmt.setString(5, stu.getName());
        pstmt.setString(6, stu.getPhone());
        int result=pstmt.executeUpdate();
        dbutil.close(pstmt, con);
        return result;
    }
    /**
     * 遍历输出,返回一个arrays list数组
     * @return 
     * @throws Exception
     */
    public static ArrayList<student> ListStudentAll() throws Exception {
        //一维数组,每个元素是student类型
        ArrayList<student> list = new ArrayList<student>();
        DbUtil dbutil=new DbUtil();
        Connection con=dbutil.getcon();//连接数据库
        String sql="select * from everyone";//写sql语句
        PreparedStatement pstmt=con.prepareStatement(sql);//preparestatement发送sql语句
        ResultSet rs=pstmt.executeQuery();//返回结果集ResultSet
        while(rs.next()) {//re.next()才是第一行,rs不是,会是一个奇怪的地方
            String name=rs.getString("name");
            String sex=rs.getString("sex");
            String id=rs.getString("id");
            String grade=rs.getString("grade");
            String phone=rs.getString("phone");
            String decision=rs.getString("decision");
            String selfintroduction=rs.getString("selfintroduction");
            String password=rs.getString("password");
            student stu=new student();//student的调用一定要放在循环里面才能保证不被覆盖,因为Java里的变量是引用不是传值!!
            stu.setName(name);
            stu.setSex(sex);
            stu.setId(id);
            stu.setGrade(grade);
            stu.setPhone(phone);
            stu.setSelfintroduction(selfintroduction);
            stu.setDecision(decision);
            stu.setPassword(password);
            list.add(stu);
        } 
        
        return list;
    }
    
    /**
     * 根据名字输出数据,可以根据中文名字输出对应类的全部数据
     * @throws Exception
     */
    public static student ListStudent1(String name ) throws Exception {
        DbUtil dbutil=new DbUtil();
        Connection con=dbutil.getcon();//连接数据库
        String sql="select * from everyone ";//写sql语句
        PreparedStatement pstmt=con.prepareStatement(sql);//preparestatement发送sql语句
        ResultSet rs=pstmt.executeQuery();//返回结果集ResultSet
        student stu=new student();
        boolean flag=false;
        while(rs.next()) {//re.next()才是第一行,rs不是,会是一个奇怪的地方
            String sex=rs.getString("sex");
            String id=rs.getString("id");
            String grade=rs.getString("grade");
            String phone=rs.getString("phone");
            String decision=rs.getString("decision");
            String selfintroduction=rs.getString("selfintroduction");
            String password=rs.getString("password");
            if(rs.getString("name").equals(name)){
            flag=true;
            stu.setName(name);
            stu.setSex(sex);
            stu.setId(id);
            stu.setGrade(grade);
            stu.setPhone(phone);
            stu.setSelfintroduction(selfintroduction);
            stu.setDecision(decision);
            stu.setPassword(password);
            }
        }
        if(flag)
            return stu;
        else {
            stu=null;
            return stu;
        }
    }
    
    /**
     * 根据id输出数据
     * @throws Exception
     */
    public static student ListStudent2(String id ) throws Exception {
        DbUtil dbutil=new DbUtil();
        Connection con=dbutil.getcon();//连接数据库
        String sql="select * from everyone ";//写sql语句
        PreparedStatement pstmt=con.prepareStatement(sql);//preparestatement发送sql语句
        ResultSet rs=pstmt.executeQuery();//返回结果集ResultSet
        student stu=new student();
        while(rs.next()) {//re.next()才是第一行,rs不是,会是一个奇怪的地方
            String sex=rs.getString("sex");
            String name=rs.getString("name");
            String grade=rs.getString("grade");
            String phone=rs.getString("phone");
            String decision=rs.getString("decision");
            String selfintroduction=rs.getString("selfintroduction");
            String password=rs.getString("password");
            if(rs.getString("id").equals(id)){
            System.out.println("!!__"+name);
            stu.setName(name);
            stu.setSex(sex);
            stu.setId(id);
            stu.setGrade(grade);
            stu.setPhone(phone);
            stu.setSelfintroduction(selfintroduction);
            stu.setDecision(decision);
            stu.setPassword(password);
            System.out.println(stu);
            return stu;
            
            }
        }
            stu=null;
            return stu;
    }
    

/**
 * 被业务逻辑层services调用,校验学生身份(学号+电话)
 * @param password
 * @return
 * @throws Exception
 */
public static boolean FindStu(String phone,String password,String id) throws Exception{
    DbUtil dbutil=new DbUtil();
    Connection con=dbutil.getcon();//连接数据库
    String sql="select * from everyone";//写sql语句,select的不是显示在屏幕中,而是在结果集里
    PreparedStatement pstmt=con.prepareStatement(sql);//preparestatement发送sql语句
    ResultSet rs=pstmt.executeQuery();//返回结果集ResultSet
    while(rs.next()) {//re.next()才是第一行,rs不是,会是一个奇怪的地方
        String PhoneCheck=rs.getString("phone");
        String passwordCheck=rs.getString("password");//
        String IdCheck=rs.getString("id");
        
        System.out.println(PhoneCheck);
        System.out.println(passwordCheck);
        System.out.println(IdCheck);
        //注意,数据库里的这三条数据任何一条都不允许为null,否则该id和phone下都会登录失败
        if(id.equals(IdCheck)&&PhoneCheck.equals(phone)&&passwordCheck.equals(password)){
            dbutil.close(pstmt, con);
            return true;
        }
    }
    return false;
    }


/**
 * 被业务逻辑层services调用,校验管理员身份(电话+密码)
 * @param name
 * @param password
 * @return
 * @throws Exception
 */
public static boolean FindAdmin(String name,String password) throws Exception{
    DbUtil dbutil=new DbUtil();
    Connection con=dbutil.getcon();//连接数据库
    String sql="select * from admin";//写sql语句,select的不是显示在屏幕中,而是在结果集里
    PreparedStatement pstmt=con.prepareStatement(sql);//preparestatement发送sql语句
    ResultSet rs=pstmt.executeQuery();//返回结果集ResultSet
    while(rs.next()) {//re.next()才是第一行,rs不是,会是一个奇怪的地方
        String nameCheck=rs.getString("name");
        String passwordCheck=rs.getString("password");
        if(password.equals(passwordCheck)&&nameCheck.equals(name)){
            dbutil.close(pstmt, con);
            return true;
        }
    }
    return false;
    }
}

这里面是最核心的操作,增删改查,每个函数我都写了用处。

4)

现在开始写前端啦!

前端我本来是用html的,但是发现展示页面需要用到list循环实现,得用java代码实现,而html好像不能插Java代码,所以就改用jsp了。

WebContent下右键,新建html/jsp,我一开始是html,取名为LoginHomeStuAndAdmin(要求用驼峰命名法,就这样写了),敲:

<!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>CAT学生招新与管理员管理——管理系统</title>
<script type="text/javascript">
function check(){
    var phone=document.getElementById("phone").value;
    var id=document.getElementById("id").value;
    var password=document.getElementById("password").value;
    
    var phoneText=document.getElementById(‘phoneText‘);
    if(phone==‘‘){
        phoneText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>联系电话不能为空</span>";
        return false;
    }
    //\d表示0-9中任一位,{9}表示有9个数字
    else if(phone.match(/^1\d{10}$/)!=phone){
        phoneText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>联系电话必须是11位数字,且第一位必须是1</span>";
        return false;
    }
    else{
        phoneText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    }
    
    var idText=document.getElementById(‘idText‘);
    if(id==‘‘){
        idText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>学号不能为空</span>";
        return false;
    }
    else if(id.match(/^[1-9]\d{5,19}$/)!=id){
        idText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>学号必须是6-20位的数字</span>";
        return false;
    }
    else{
        idText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    }
    
    var passwordText=document.getElementById(‘passwordText‘);
    if(password==‘‘){
        passwordText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>密码不能为空</span>";
        return false;
    }
    else{
        passwordText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    } 
    
}
</script>
</head>
<body>
<form action=‘CheckStuIfNull‘ method=‘post‘ >
<pre>
<!-- 总界面 -->
学生版
电话:<input type=‘text‘ name=‘phone‘ id=‘phone‘ onblur=‘check()‘ /><span id=‘phoneText‘></span><br>
学号:<input type=‘text‘ name=‘id‘ id=‘id‘ onblur=‘check()‘ /><span id=‘idText‘></span><br>
密码:<input type=‘password‘ name=‘password‘ id=‘password‘ onblur=‘check()‘ /><span id=‘passwordText‘></span><br>
<input type =‘submit‘ name=‘login‘ value=‘登录‘>
还没<a href="StuRegister.html">注册</a><a href="CheckAdmin.html">管理员登录通道</a> 
</pre>
</form>
</body>
</html>

上面这个是主界面, tomcat 运行起来是下面这样的:

(其实html运行本不需要tomcat,但是马上要用到了,就先搞好来吧,怎么装自行网上学习)

技术图片

5)

一系列的跳转:

先说管理员,因为他权限比较多,也是核心。点击管理员登录通道,就会跳转到CheckAdmin.html这个网页:(有正则表达式的知识,自行学习,也可以先不学,把下面script的东西先不当回事。)

 

<!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>管理员登录界面</title>
<script type="text/javascript">
function check(){
    var name=document.getElementById("name").value;
    var password=document.getElementById("password").value;
    
    var nameText=document.getElementById(‘nameText‘);
    if(name==‘‘){
        nameText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>账号不能为空</span>";
        return false;
    }
    else{
        nameText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    } 
    
    
    var passwordText=document.getElementById(‘passwordText‘);
    if(password==‘‘){
        passwordText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>密码不能为空</span>";
        return false;
    }
    else{
        passwordText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    } 
    
}

</script>
</head>
<body>
<form action=‘CheckAdminIfNull‘ method=‘post‘ >
<pre>
【你要是什么都不填,直接按登录,2秒后会自动转回来;
你若是填了,但是不符合要求,则会被正则提示。
有空不填也会被拦截。】
管理员版
账号:<input type=‘text‘ name=‘name‘ id=‘name‘ onblur=‘check()‘ /><span id=‘nameText‘></span><br>
密码:<input type=‘text‘ name=‘password‘ id=‘password‘ onblur=‘check()‘ /><span id=‘passwordText‘></span><br>
<input type =‘submit‘ name=‘login‘ value=‘登录‘>
</pre>
</form>
</body>
</html>

 

 这里输入后台数据库已经设定好的管理员名称和密码,点击登录,几经跳转后跳转到管理员欢迎界面(增删改查操作界面):

(点击登录后,到登录成功显示欢迎界面,这中间有个CheckAdminIfNull.java的servlet,为了不打断思路,就先不讲。)

(不知道为什么,直接在tomcat里点击登录后台会报空指针异常,说是password为null,复制地址到浏览器里打开却不会,一脸懵逼=.=!)

技术图片

这个界面就是jsp啦!因为你也看到了,界面显示了管理员登录的名字, 我是暂时不知道身为静态网页的HTML怎样实现这一功能的咯~~~,于是就让jsp登场啦!

取名为AdminLoginSuccess,敲:

<%@ page language="java" 
    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">
<%
    String name=(String)session.getAttribute("name1");
%>
<title>【欢迎你,管理员<%=name %>】</title>
</head>
<body>
<pre>
欢迎你,<%=name %>,下面是你可以进行的操作:
<!-- 注意:管理员同样不能修改学生和自己的登录信息,这是出于安全考虑(其实就是自己菜嘿嘿嘿),不予提供修改接口 -->
<a href="AdminAdd.html">增加</a>用户信息;
<a href="AdminChange.html">修改</a>用户报名信息;
<a href="Admindelete.html">删除</a>用户信息;
<a href="Adminsee.jsp">查看</a>用户信息;
</pre>
</body>
</html>

 

6)

你也看到了,上面有四个jsp/html,咋们来一个一个实现吧!

WebContent右键,新建以下四个jsp/HTML:

1】AdminAdd.html,敲:

<!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>管理员增加用户信息</title>
<script type="text/javascript">
function check(){
    var name=document.getElementById("name").value;
    var id=document.getElementById("id").value;
    //sex和ddecision是选择栏,不需要用正则
    var phone=document.getElementById("phone").value;

    var sid=document.getElementById("sid").value;
    
    var nameText=document.getElementById(‘nameText‘);
    if(name==‘‘){
        nameText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>名字不能为空</span>";
        return false;
    }
    else if(name.match(/^[\u4e00-\u9fa5]+$/)!=name){
        nameText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>名字必须为汉字、空格或中文分隔符</span>";
        return false;
    }
    else {
        nameText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        //return true;
    }
    
    var idText=document.getElementById(‘idText‘);
    if(id==‘‘){
        idText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>学号不能为空</span>";
        return false;
    }
    else if(id.match(/^[1-9]\d{5,19}$/)!=id){
        idText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>学号必须是6-20位的数字</span>";
        return false;
    }
    else{
        idText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    }
    
    var phoneText=document.getElementById(‘phoneText‘);
    if(phone==‘‘){
        phoneText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>联系电话不能为空</span>";
        return false;
    }
    //\d表示0-9中任一位,{9}表示有9个数字
    else if(phone.match(/^1\d{10}$/)!=phone){
        phoneText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>联系电话必须是11位数字,且第一位必须是1</span>";
        return false;
    }
    else{
        phoneText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    }
    
    
    var sidText=document.getElementById(‘sidText‘);
    if(sid==‘‘){
        sidText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>自我介绍不能为空</span>";
        return false;
    }
    else{
        sidText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        
    }    
}
</script>
</head>
<body>
<form action=‘AdminAdd‘ method=‘post‘ >
<!-- 上面的CheckAddAdd虽然不能直接点击跳转,但是在运行时可以跳转到Add.java,所以不用管。
另外,改了跳转页面,记得关闭eclipse在启动方可生效!
下面是普通的文本输入,写什么就输出什么    nameText是指span的id ,下同-->
<pre><!-- 电话和学号虽然属于学生登录信息,但是”超级“管理员有权限增加,虽然我觉得,管理员是不会那么闲去新增学生信息的-->
<h4>使用须知:
管理员增加学生信息,密码默认为null。
如果新增学生信息已经存在,检索时将会展现多个值;
另外,请不要留空,系统会保存最后一次的值,前一次的值将被覆盖 不可恢复。
</h4>
名字:<input type=‘text‘ name=‘name‘ id=‘name‘ onblur=‘check()‘ /><span id=‘nameText‘></span><br>
电话(谨慎填写):<input type=‘text‘ name=‘phone‘ id=‘phone‘ onblur=‘check()‘ /><span id=‘phoneText‘></span><br>
学号(谨慎填写):<input type=‘text‘ name=‘id‘ id=‘id‘ onblur=‘check()‘ /><span id=‘idText‘></span><br>
性别:<input type=‘radio‘ name=‘sex‘ id=‘sex‘ value=“男”>男<input type=‘radio‘ name=‘sex‘ id=‘sex‘ value=“女”>女<br>
年级 专业班级:<input type=‘radio‘ name=‘grade‘ id=‘sex‘ value=‘18级‘>18级<input type=‘radio‘ name=‘grade‘ id=‘sex‘ value=‘19级‘>19级<br>
<input type =‘text‘ name=‘profession&class‘ size=‘25‘ maxlength=‘100‘ value="填写你的年级班级"><br>
方向:<input type=‘radio‘ name=‘decision‘ id=‘decision‘ value=‘前端组‘>前端组<input type=‘radio‘ name=‘decision‘ id=‘decision‘ value=‘后台组‘>后台组<br>
自我介绍:<textarea rows="5" cols="19" id=‘sid‘ name=‘sid‘ onblur=‘check()‘></textarea><span id=‘sidText‘></span><br>
<input type =‘submit‘ name=‘login‘ value=‘保存‘>
</pre>
</form>
</body>
</html>

 

2】AdminChange.html,敲:

<!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>管理员修改用户信息</title>
<script type="text/javascript">
function check(){
    var name=document.getElementById("name").value;
    var id=document.getElementById("id").value;
    //sex和ddecision是选择栏,不需要用正则
    var phone=document.getElementById("phone").value;

    var sid=document.getElementById("sid").value;
    
    var nameText=document.getElementById(‘nameText‘);
    if(name==‘‘){
        nameText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>名字不能为空</span>";
        return false;
    }
    else if(name.match(/^[\u4e00-\u9fa5]+$/)!=name){
        nameText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>名字必须为汉字、空格或中文分隔符</span>";
        return false;
    }
    else {
        nameText.innerHTML="<img src=‘right.png‘ width=‘16‘ height=‘8‘/><span style=‘color:#00cc00‘>格式正确</span>";
        //return true;
    }
    
    var idText=document.getElementById(‘idText‘);
    if(id==‘‘){
        idText.innerHTML="<img src=‘wrong.png‘ width=‘16‘ height=‘15‘/><span style=‘color:#cc0000‘>学号不能为空</span>";
        
                        
                    

人气教程排行