阶段五-JavaWeb综合练习-学生管理系统

本文介绍了使用JavaEE开发的一个后台管理系统,包括用户登录功能(含验证码验证)、班级和学生管理功能,以及MVC架构、数据库操作和实体类设计。详细描述了需求分析、界面划分和验证码功能的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.项目说明

1.前台 (用户使用)

        前端,后端

2.后台 (管理员使用)

        前端,后端

3.该项目为后台管理系统

项目开发流程:

1.需求分析

1.1 登录功能  

用户访问登录页面输入用户名和密码,并且输入验证码。全部输入正确后点击登录,登录成功跳转主页面;登录失败,则再次返回登录页面,并提示用户,用户名或者密码错误! 如果是验证码错误,则登录失败,并提示用户验证码错误!

1.2 班级管理功能
1.2.1 添加班级

用户访问添加班级功能,进入到班级添加的页面,输入要添加的班级信息,点击提交完成 班级信息的添加,并在当前页面中提示班级新增的结果.

1.2.2 查询班级

用户访问查询班级功能,分页显示所有的班级信息,并且可以对班级信息进行修改和删除操作.

1.2.3 各班人数

统计各班人数,以条形图的方式显示给用户

1.3 学生管理功能
1.3.1 添加学生

用户访问添加学生功能,进入学生信息添加页面,在输入添加的学生信息后,点击提交完成学生信息的添加,并且在当前页面中提示用户添加结果!

1.3.2 展示学生

用户访问展示学生信息功能,进入学生信息展示页面,并分页显示所有的学生信息,同时用户可以按照指定的条件对学生信息查询,并可以对学生信息进行修改和删除操作,以及导出查询的学生信息.

2.划分模块

页面

java代码

mysql数据库

3.库表设计

用户表user

字段说明
uid用户id
ukey用户账号
pwd密码
realname真实姓名

班级表class

字段说明
cid班级ID
cname班级名称
teacher老师
remark备注

学生表student

字段说明
sid学生id
sname学生姓名
hobby爱好
sex性别
bir生日
phone手机号
remark备注
cid班级编号

     

二、项目环境搭建

1. 创建项目

创建JavaEE项目,名字为:MUI。

2. 导入相关jar包

  1. 数据库驱动包

  2. json工具包

  3. jstl的两个包

3. 静态页面的修改

  1. 将模板中的静态资源(js、css、images)复制到项目的web目录下

  2. 在web目录下导入html文件

4. 创建项目包结构

创建MVC分层开发的包结构

5. 创建实体类

按照orm映射创建实体类,和表中的字段一一对应

Clazz班级实体类

package com.sh.pojo;


import java.io.Serializable;

public class Clazz implements Serializable {

  private int cid;
  private String cname;
  private String teacher;
  private String remark;


  @Override
  public String toString() {
    return "Clazz{" +
            "cid=" + cid +
            ", cname='" + cname + '\'' +
            ", teacher='" + teacher + '\'' +
            ", remark='" + remark + '\'' +
            '}';
  }

  public int getCid() {
    return cid;
  }

  public void setCid(int cid) {
    this.cid = cid;
  }

  public String getCname() {
    return cname;
  }

  public void setCname(String cname) {
    this.cname = cname;
  }

  public String getTeacher() {
    return teacher;
  }

  public void setTeacher(String teacher) {
    this.teacher = teacher;
  }

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public Clazz(int cid, String cname, String teacher, String remark) {
    this.cid = cid;
    this.cname = cname;
    this.teacher = teacher;
    this.remark = remark;
  }

  public Clazz() {
  }
}

Student学生实体类

package com.sh.pojo;


import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

public class Student implements Serializable {

  private int sid;
  private String sname;
  private String hobby;
  private String sex;
  private Date bir;
  private String phone;
  private String remark;
  private int cid;

  @Override
  public String toString() {
    return "Student{" +
            "sid=" + sid +
            ", sname='" + sname + '\'' +
            ", hobby='" + hobby + '\'' +
            ", sex='" + sex + '\'' +
            ", bir=" + bir +
            ", phone='" + phone + '\'' +
            ", remark='" + remark + '\'' +
            ", cid=" + cid +
            '}';
  }

  public int getSid() {
    return sid;
  }

  public void setSid(int sid) {
    this.sid = sid;
  }

  public String getSname() {
    return sname;
  }

  public void setSname(String sname) {
    this.sname = sname;
  }

  public String getHobby() {
    return hobby;
  }

  public void setHobby(String hobby) {
    this.hobby = hobby;
  }

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex;
  }

  public Date getBir() {
    return bir;
  }

  public void setBir(Date bir) {
    this.bir = bir;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public int getCid() {
    return cid;
  }

  public void setCid(int cid) {
    this.cid = cid;
  }

  public Student(int sid, String sname, String hobby, String sex, Date bir, String phone, String remark, int cid) {
    this.sid = sid;
    this.sname = sname;
    this.hobby = hobby;
    this.sex = sex;
    this.bir = bir;
    this.phone = phone;
    this.remark = remark;
    this.cid = cid;
  }

  public Student() {
  }
}

User用户类

package com.sh.pojo;


import java.io.Serializable;

public class User implements Serializable {

  private int uid;
  private String uname;
  private String pwd;
  private String realname;


  @Override
  public String toString() {
    return "User{" +
            "uid=" + uid +
            ", uname='" + uname + '\'' +
            ", pwd='" + pwd + '\'' +
            ", realname='" + realname + '\'' +
            '}';
  }

  public int getUid() {
    return uid;
  }

  public void setUid(int uid) {
    this.uid = uid;
  }

  public String getUname() {
    return uname;
  }

  public void setUname(String uname) {
    this.uname = uname;
  }

  public String getPwd() {
    return pwd;
  }

  public void setPwd(String pwd) {
    this.pwd = pwd;
  }

  public String getRealname() {
    return realname;
  }

  public void setRealname(String realname) {
    this.realname = realname;
  }

  public User(int uid, String uname, String pwd, String realname) {
    this.uid = uid;
    this.uname = uname;
    this.pwd = pwd;
    this.realname = realname;
  }

  public User() {
  }
}

StudentDto是根据需求创建的临时实体类,用来完成具体的需求

package com.sh.pojo;

import java.io.Serializable;
import java.util.Date;

public class StudentDto implements Serializable {
    private int sid;
    private String sname;
    private String phone;
    private String sex;
    private String cname;
    private Date bir;

    @Override
    public String toString() {
        return "StudentDto{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", phone='" + phone + '\'' +
                ", sex='" + sex + '\'' +
                ", cname='" + cname + '\'' +
                ", bir=" + bir +
                '}';
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public Date getBir() {
        return bir;
    }

    public void setBir(Date bir) {
        this.bir = bir;
    }

    public StudentDto(int sid, String sname, String phone, String sex, String cname, Date bir) {
        this.sid = sid;
        this.sname = sname;
        this.phone = phone;
        this.sex = sex;
        this.cname = cname;
        this.bir = bir;
    }

    public StudentDto() {
    }
}

三、实现验证码功能

1.需求

在登录页面中有验证码,用户一旦进入登录页面就能看到验证码图片。

如果看不清,用户可以点击验证码图片,然后会重新得到一个验证码图片。

2. 思路分析

  1. 编写一个Servlet,专门用来生成验证码(不用自己编写,参照资料即可,里面用到了Java的画图类,直接将该类复制过来)

  2. 将生成的验证码存储到Session中,因为后面点击登录按钮后还要将用户输入的和之前生成的验证码进行比对

  3. 在登录页面中,验证码使用的是图片,将图片标签的src属性值写为生成验证码的Servlet的访问路径即可

  4. 给图片加个点击事件,每次点击将图片标签的src值给稍微改动一下,比如在正常的Servlet地址后面拼一个随机数,目的是为了防止浏览器缓存而不会发送请求。

3. 具体实现

package com.sh.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/getCode")
public class RandomServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 在内存中创建图象
		int width = 110, height = 30;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		Graphics g = image.getGraphics();
		// 生成随机类
		Random random = new Random();
		// 设定背景色
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, width, height);
		// 设定字体
		g.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
		g.setColor(getRandColor(160, 200));
		for (int i = 0; i < 155; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.drawLine(x, y, x + xl, y + yl);
		}
		// 取随机产生的认证码(4位数字)
		String sRand = "";
		String[] strings = {"A","B","C","D","E","1","2","3","4","5"};
		for (int i = 0; i < 4; i++) {
			/*String rand = String.valueOf(random.nextInt(10));*/
			String rand = strings[random.nextInt(strings.length)];
			sRand += rand;
			// 将认证码显示到图象中
			g.setColor(new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110)));
			// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
			g.drawString(rand, 13 * i + 20, 20);
		}
		//保存验证码到Session
		request.getSession().setAttribute("randStr", sRand);
		// 图象生效
		g.dispose();
		
		try {
			ImageIO.write(image, "JPEG", response.getOutputStream());
		} catch (Exception e) {
			System.out.println("验证码图片产生出现错误:" + e.toString());
		}
		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}
	/*
	 * 给定范围获得随机颜色
	 */
	private Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}
}

修改干扰线的条数,干扰线越多越看不清 

验证码的设置

将验证码保存到session中

修改登录页面中的验证码图片地址 

四、实现登录功能

未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值