前端,后端
前端,后端
用户访问登录页面输入用户名和密码,并且输入验证码。全部输入正确后点击登录,登录成功跳转主页面;登录失败,则再次返回登录页面,并提示用户,用户名或者密码错误! 如果是验证码错误,则登录失败,并提示用户验证码错误!
用户访问添加班级功能,进入到班级添加的页面,输入要添加的班级信息,点击提交完成 班级信息的添加,并在当前页面中提示班级新增的结果.
用户访问查询班级功能,分页显示所有的班级信息,并且可以对班级信息进行修改和删除操作.
统计各班人数,以条形图的方式显示给用户
用户访问添加学生功能,进入学生信息添加页面,在输入添加的学生信息后,点击提交完成学生信息的添加,并且在当前页面中提示用户添加结果!
用户访问展示学生信息功能,进入学生信息展示页面,并分页显示所有的学生信息,同时用户可以按照指定的条件对学生信息查询,并可以对学生信息进行修改和删除操作,以及导出查询的学生信息.
页面
java代码
mysql数据库
用户表user
字段 | 说明 |
---|---|
uid | 用户id |
ukey | 用户账号 |
pwd | 密码 |
realname | 真实姓名 |
班级表class
字段 | 说明 |
---|---|
cid | 班级ID |
cname | 班级名称 |
teacher | 老师 |
remark | 备注 |
学生表student
字段 | 说明 |
---|---|
sid | 学生id |
sname | 学生姓名 |
hobby | 爱好 |
sex | 性别 |
bir | 生日 |
phone | 手机号 |
remark | 备注 |
cid | 班级编号 |
创建JavaEE项目,名字为:MUI。
数据库驱动包
json工具包
jstl的两个包
将模板中的静态资源(js、css、images)复制到项目的web目录下
在web目录下导入html文件
创建MVC分层开发的包结构
按照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.需求
在登录页面中有验证码,用户一旦进入登录页面就能看到验证码图片。
如果看不清,用户可以点击验证码图片,然后会重新得到一个验证码图片。
编写一个Servlet,专门用来生成验证码(不用自己编写,参照资料即可,里面用到了Java的画图类,直接将该类复制过来)
将生成的验证码存储到Session中,因为后面点击登录按钮后还要将用户输入的和之前生成的验证码进行比对
在登录页面中,验证码使用的是图片,将图片标签的src属性值写为生成验证码的Servlet的访问路径即可
给图片加个点击事件,每次点击将图片标签的src值给稍微改动一下,比如在正常的Servlet地址后面拼一个随机数,目的是为了防止浏览器缓存而不会发送请求。
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中
修改登录页面中的验证码图片地址
未完待续