学生信息系统,包括四个模块,每个模块都是独立的,可以选择学习:1、学生信息管理系统 2、学生选课信息系统 3、学生签到考勤系统 4、学生成绩管理系统
学生信息管理系统
- Javaweb目录介绍
- dao 数据库操作
- model 每个model对应数据库中的一张表
- servlet 数据库操作逻辑(前后端交互)
登录页面功能
前端login.jsp介绍
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="shortcut icon" href="favicon.ico"/>
<link rel="bookmark" href="favicon.ico"/>
<link href="h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css" />
<link href="h-ui/css/H-ui.login.css" rel="stylesheet" type="text/css" />
<link href="h-ui/lib/icheck/icheck.css" rel="stylesheet" type="text/css" />
<link href="h-ui/lib/Hui-iconfont/1.0.1/iconfont.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="h-ui/js/H-ui.js"></script>
<script type="text/javascript" src="h-ui/lib/icheck/jquery.icheck.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
$(function(){
//点击图片切换验证码
$("#vcodeImg").click(function(){
this.src="CpachaServlet?method=loginCpacha&t="+new Date().getTime();
});
//登录
$("#submitBtn").click(function(){
var data = $("#form").serialize();
$.ajax({
type: "post",
url: "LoginServlet?method=Login",
data: data,
dataType: "text", //返回数据类型
success: function(msg){
if("vcodeError" == msg){
$.messager.alert("消息提醒", "验证码错误!", "warning");
$("#vcodeImg").click();//切换验证码
$("input[name='vcode']").val("");//清空验证码输入框
} else if("loginError" == msg){
$.messager.alert("消息提醒", "用户名或密码错误!", "warning");
$("#vcodeImg").click();//切换验证码
$("input[name='vcode']").val("");//清空验证码输入框
} else if("admin" == msg){
window.location.href = "SystemServlet?method=toAdminView";
} else if("student" == msg){
window.location.href = "SystemServlet?method=toStudentView";
} else if("teacher" == msg){
window.location.href = "SystemServlet?method=toTeacherView";
}
}
});
});
//设置复选框
$(".skin-minimal input").iCheck({
radioClass: 'iradio-blue',
increaseArea: '25%'
});
})
</script>
<title>登录|学生信息管理系统</title>
<meta name="keywords" content="学生信息管理系统">
</head>
<body>
<div class="header" style="padding: 0;">
<h2 style="color: white; width: 400px; height: 60px; line-height: 60px; margin: 0 0 0 30px; padding: 0;">学生信息管理系统</h2>
</div>
<div class="loginWraper">
<div id="loginform" class="loginBox">
<form id="form" class="form form-horizontal" method="post">
<div class="row cl">
<label class="form-label col-3"><i class="Hui-iconfont"></i></label>
<div class="formControls col-8">
<input id="" name="account" type="text" placeholder="账户" class="input-text size-L">
</div>
</div>
<div class="row cl">
<label class="form-label col-3"><i class="Hui-iconfont"></i></label>
<div class="formControls col-8">
<input id="" name="password" type="password" placeholder="密码" class="input-text size-L">
</div>
</div>
<div class="row cl">
<div class="formControls col-8 col-offset-3">
<input class="input-text size-L" name="vcode" type="text" placeholder="请输入验证码" style="width: 200px;">
<img title="点击图片切换验证码" id="vcodeImg" src="CpachaServlet?method=loginCpacha"></div>
</div>
<div class="mt-20 skin-minimal" style="text-align: center;">
<div class="radio-box">
<input type="radio" id="radio-2" name="type" checked value="2" />
<label for="radio-1">学生</label>
</div>
<div class="radio-box">
<input type="radio" id="radio-3" name="type" value="3" />
<label for="radio-2">老师</label>
</div>
<div class="radio-box">
<input type="radio" id="radio-1" name="type" value="1" />
<label for="radio-3">管理员</label>
</div>
</div>
<div class="row">
<div class="formControls col-8 col-offset-3">
<input id="submitBtn" type="button" class="btn btn-success radius size-L" value=" 登 录 ">
</div>
</div>
</form>
</div>
</div>
<div class="footer">Copyright auther @ re-rebirth </div>
</body>
</html>
验证码功能
- 配置web.xml
<servlet> <description>验证码</description> <servlet-name>CpachaServlet</servlet-name> <servlet-class>com.ischoolbar.programmer.servlet.CpachaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CpachaServlet</servlet-name> <url-pattern>/CpachaServlet</url-pattern> </servlet-mapping>
<servlet>
和<servlet-mapping>
是成对出现的
- CpachaUtil画验证码
package com.ischoolbar.programmer.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* 验证码生成器
*
* @author llq
*/
public class CpachaUtil {
/**
* 验证码来源
*/
final private char[] code = {
'2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
/**
* 字体
*/
final private String[] fontNames = new String[]{
"黑体", "宋体", "Courier", "Arial",
"Verdana", "Times", "Tahoma", "Georgia"};
/**
* 字体样式
*/
final private int[] fontStyles = new int[]{
Font.BOLD, Font.ITALIC|Font.BOLD
};
/**
* 验证码长度
* 默认4个字符
*/
private int vcodeLen = 4;
/**
* 验证码图片字体大小
* 默认17
*/
private int fontsize = 21;
/**
* 验证码图片宽度
*/
private int width = (fontsize+1)*vcodeLen+10;
/**
* 验证码图片高度
*/
private int height = fontsize+12;
/**
* 干扰线条数
* 默认3条
*/
private int disturbline = 3;
public CpachaUtil(){}
/**
* 指定验证码长度
* @param vcodeLen 验证码长度
*/
public CpachaUtil(int vcodeLen) {
this.vcodeLen = vcodeLen;
this.width = (fontsize+1)*vcodeLen+10;
}
/**
* 生成验证码图片
* @param vcode 要画的验证码
* @param drawline 是否画干扰线
* @return
*/
public BufferedImage generatorVCodeImage(String vcode, boolean drawline){
//创建验证码图片
BufferedImage vcodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = vcodeImage.getGraphics();
//填充背景色
g.setColor(new Color(246, 240, 250));
g.fillRect(0, 0, width, height);
if(drawline){
drawDisturbLine(g);
}
//用于生成伪随机数
Random ran = new Random();
//在图片上画验证码
for(int i = 0;i < vcode.length();i++){
//设置字体
g.setFont(new Font(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)], fontsize));
//随机生成颜色
g.setColor(getRandomColor());
//画验证码
g.drawString(vcode.charAt(i)+"", i*fontsize+10, fontsize+5);
}
//释放此图形的上下文以及它使用的所有系统资源
g.dispose();
return vcodeImage;
}
/**
* 获得旋转字体的验证码图片
* @param vcode
* @param drawline 是否画干扰线
* @return
*/
public BufferedImage generatorRotateVCodeImage(String vcode, boolean drawline){
//创建验证码图片
BufferedImage rotateVcodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = rotateVcodeImage.createGraphics();
//填充背景色
g2d.setColor(new Color(246, 240, 250));
g2d.fillRect(0, 0, width, height);
if(drawline){
drawDisturbLine(g2d);
}
//在图片上画验证码
for(int i = 0;i < vcode.length();i++){
BufferedImage rotateImage = getRotateImage(vcode.charAt(i));
g2d.drawImage(rotateImage, null, (int) (this.height * 0.7) * i, 0);
}
g2d.dispose();
return rotateVcodeImage;
}
/**
* 生成验证码
* @return 验证码
*/
public String generatorVCode(){
int len = code.length;
Random ran = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0;i < vcodeLen;i++){
int index = ran.nextInt(len);
sb.append(code[index]);
}
return sb.toString();
}
/**
* 为验证码图片画一些干扰线
* @param g
*/
private void drawDisturbLine(Graphics g){
Random ran = new Random();
for(int i = 0;i < disturbline;i++){
int x1 = ran.nextInt(width);
int y1 = ran.nextInt(height);
int x2 = ran.nextInt(width);
int y2 = ran.nextInt(height);
g.setColor(getRandomColor());
//画干扰线
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 获取一张旋转的图片
* @param c 要画的字符
* @return
*/
private BufferedImage getRotateImage(char c){
BufferedImage rotateImage = new BufferedImage(height, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotateImage.createGraphics();
//设置透明度为0
g2d.setColor(new Color(255, 255, 255, 0));
g2d.fillRect(0, 0, height, height);
Random ran = new Random();
g2d.setFont(new Font(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)], fontsize));
g2d.setColor(getRandomColor());
double theta = getTheta();
//旋转图片
g2d.rotate(theta, height/2, height/2);
g2d.drawString(Character.toString(c), (height-fontsize)/2, fontsize+5);
g2d.dispose();
return rotateImage;
}
/**
* @return 返回一个随机颜色
*/
private Color getRandomColor(){
Random ran = new Random();
return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220));
}
/**
* @return 角度
*/
private double getTheta(){
return ((int) (Math.random()*1000) % 2 == 0 ? -1 : 1)*Math.random();
}
/**
* @return 验证码字符个数
*/
public int getVcodeLen() {
return vcodeLen;
}
/**
* 设置验证码字符个数
* @param vcodeLen
*/
public void setVcodeLen(int vcodeLen) {
this.width = (fontsize+3)*vcodeLen+10;
this.vcodeLen = vcodeLen;
}
/**
* @return 字体大小
*/
public int getFontsize() {
return fontsize;
}
/**
* 设置字体大小
* @param fontsize
*/
public void setFontsize(int fontsize) {
this.width = (fontsize+3)*vcodeLen+10;
this.height = fontsize+15;
this.fontsize = fontsize;
}
/**
* @return 图片宽度
*/
public int getWidth() {
return width;
}
/**
* 设置图片宽度
* @param width
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return 图片高度
*/
public int getHeight() {
return height;
}
/**
* 设置图片高度
* @param height
*/
public void setHeight(int height) {
this.height = height;
}
/**
* @return 干扰线条数
*/
public int getDisturbline() {
return disturbline;
}
/**
* 设置干扰线条数
* @param disturbline
*/
public void setDisturbline(int disturbline) {
this.disturbline = disturbline;
}
}
- servlet应用CpachaUtil
package com.ischoolbar.programmer.servlet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ischoolbar.programmer.util.CpachaUtil;
/**
*
* @Description 验证码servlet
* @author 个人电脑7.0 Email:2387221494@qq.com
* @version 功能实现:CpachaServlet+CpachaUtil+login.jsp
* @date Apr 7, 2020 8:40:38 AM
*
*/
public class CpachaServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 3734898030184595150L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
doPost(request, response);
}
public void doPost(HttpServletRequest request ,HttpServletResponse response) throws IOException{
String method = request.getParameter("method");
if("loginCpacha".equals(method)) {
generateLoginCpacha(request, response);
return;
}
response.getWriter().write("error method");
}
private void generateLoginCpacha (HttpServletRequest request ,HttpServletResponse response) throws IOException {
CpachaUtil cpachaUtil =new CpachaUtil();
String generatorVCode = cpachaUtil.generatorVCode();
request.getSession().setAttribute("loginCpacha", generatorVCode);
BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode,true);
ImageIO.write(generatorRotateVCodeImage, "gif", response.getOutputStream());
}
}
连接mysql数据库
- 创建连接mysql数据库工具
package com.ischoolbar.programmer.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @Description MySQL数据库连接util
* @author 个人电脑7.0 Email:2387221494@qq.com
* @version
* @date Apr 8, 2020 10:40:03 AM
*
*/
public class DbUtil {
private String dbUrl = "jdbc:mysql://localhost:3306/db_student_manager_web?useUnicode=true&characterEncoding=utf8";
private String dbUser = "root";
private String dbPassword = "52815562";
private String jdbcName = "com.mysql.jdbc.Driver";
private Connection connection = null;
public Connection getConnection() throws SQLException{
try {
Class.forName(jdbcName);
connection = DriverManager.getConnection(dbUrl, dbUser,dbPassword);
System.out.println("数据库连接成功!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("数据库连接失败!");
e.printStackTrace();
}
return connection;
}
public void closeCon() {
if(connection != null)
try {
connection.close();
System.out.println("数据库连接已关闭!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
DbUtil dbUtil = new DbUtil();
try {
dbUtil.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 记得在lib中导入mysql-connector-java.jar
验证登陆
package com.ischoolbar.programmer.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ischoolbar.programmer.dao.AdminDao;
import com.ischoolbar.programmer.model.Admin;
import com.ischoolbar.programmer.util.StringUtil;
/**
*
* @Description 登陆验证servlet
* @author 个人电脑7.0 Email:2387221494@qq.com
* @version
* @date Apr 8, 2020 9:48:45 AM
*
*/
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -5870852067427524781L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
doPost(request, response);
}
public void doPost(HttpServletRequest request ,HttpServletResponse response) throws IOException{
//前端传值,后端接收
String vcode = request.getParameter("vcode");
String name = request.getParameter("account");
String password = request.getParameter("password");
int type = Integer.parseInt(request.getParameter("type"));
String loginCpacha = request.getSession().getAttribute("loginCpacha").toString();
if(StringUtil.isEmpty(vcode)) {
response.getWriter().write("vcodeError");
return;
}
//.toUpperCase()小写字母转换成大写字母
if(!vcode.toUpperCase().equals(loginCpacha.toUpperCase())) {
response.getWriter().write("vcodeError");
return;
}
// if(vcode.toUpperCase().equals(loginCpacha.toUpperCase())){
// response.getWriter().write("success");
// return;
// }
//验证码验证通过,对比用户名密码是否正确
AdminDao adminDao = new AdminDao();
Admin admin = adminDao.login(name, password);
adminDao.closeCon();
switch (type) {
case 1:
{
if(admin == null) {
response.getWriter().write("loginError");
return;
}
HttpSession session = request.getSession();
session.setAttribute("user", admin);
session.setAttribute("userType",type);
response.getWriter().write("admin");
break;
}
default:
break;
}
//用户名密码正确
}
}