- 本次处理,完全更新了表结构
- 因为表结构改变代码相应的修改
- 对用户密码进行了加密和解密处理(自写函数封装在工具类中)
- 注册加密,登陆解密(之前存储的用户信息清空)
- 优化查询,可以进行模糊查询,不限制输入
- 页面发生相应的改变
- 这次几乎全部都进行了修改,遇到了很多错误(特别是对查询的优化,卡了好久)
代码结构:
- 表结构:
- 其中:sysuser用来存储用户信息(账号、密码、学校、生日等等,其中密码加密存储):
注册界面发生相应的改变(验证方式相应的也改变):
-
一般用户界面:左侧显示当前登录用户信息,查询时候不再限制输入,都可匹配:
学生选课信息栏(可以输入任何信息进行查询):
-
代码如下:(Course.java)
package entity;
public class Course {
private String sno;
private String name;
private int grade;
private String cno;
private String course_name;
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
public Course(String sno , String name ,String cname, int grade) {
this.course_name = cname;
this.grade = grade;
this.sno = sno;
this.name = name;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getCno() {
return cno;
}
public void setCno(String cno) {
this.cno = cno;
}
}
- User.java
package entity;
public class User {
private String username;
private String password;
private String image_path;
private String school_name;
private String mobile;
private String birthday;
public String getImage_path() {
return image_path;
}
public void setImage_path(String image_path) {
this.image_path = image_path;
}
public String getSchool_name() {
return school_name;
}
public void setSchool_name(String school_name) {
this.school_name = school_name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
private int is_admin;
public User(String name , String pwd , int is_admin , String school , String birth , String mobile){
this.is_admin = is_admin;
this.username = name;
this.password = pwd;
this.school_name = school;
this.birthday = birth;
this.mobile = mobile;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getIs_admin() {
return is_admin;
}
public void setIs_admin(int is_admin) {
this.is_admin = is_admin;
}
}
- Servlet类:
- CourseResultServlet.java
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CourseResultServlet
*/
public class CourseResultServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String path;
String stu;
String ss;
if(request.getParameter("student_num").equals("")) {
path = "course.jsp";
}else {
stu = (request.getParameter("student_num"));
//转换编码:
ss = new String(stu.getBytes("ISO-8859-1"),"utf-8");
path = "search_result/course_result.jsp";
request.setAttribute("stu", ss);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- RegistServlet.java
package servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import db_connect.DBConnect;
import util.Encryption;
/**
* Servlet implementation class RegistServlet
*/
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection connection = (Connection) DBConnect.getConnection();
Statement statement = null;
ResultSet resultSet = null;
String username = (String)request.getParameter("username");
String password1 = (String)request.getParameter("password");
System.out.println(password1);
String password2 = (String)request.getParameter("password2");
Encryption asd = new Encryption();
String mi;
String school_1 = (String)request.getParameter("school");
String school = new String(school_1.getBytes("ISO-8859-1"),"utf-8");
String birthday = (String)request.getParameter("birthday");
String birth = new String(birthday.getBytes("ISO-8859-1"),"utf-8");
String phone = (String)request.getParameter("phone");
System.out.println(school + " " + birth + " " + phone);
String path = "";
String msg = "";
int temp = 0;//判断注册账户是否已经存在
//接收客户端浏览器提交上来的验证码
String use_code = request.getParameter("check");
//提取图片的验证码
String system_code = (String) request.getSession().getAttribute("checkcode");
String user = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
//密码必须同时有字母和数字
String pwd = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
if(username != null && password1 != null && password2 != null && use_code != null
&& system_code != null) {
//转换成小写
use_code = use_code.toLowerCase();
system_code = system_code.toLowerCase();
if(use_code.equals(system_code)) {
if(username.matches(user)) {
if(password1.equals(password2)) {
if(password1.matches(pwd)) {
mi = asd.secreat(password1);
String sql = "insert into sysuser values(\'"+ username + "\',\'" +
mi + "\',\'" + school + "\',\'" + phone + "\'," + "\'" +
birth + "\',0," + "\'\')";
//将数据库中的数据提取出来
try {
statement = connection.createStatement();
}catch(SQLException e) {
e.printStackTrace();
}
//查询语句
String sql2 = "select * from sysuser";
try {
//将所有信息存入结果集
resultSet = statement.executeQuery(sql2);
}catch(SQLException e) {
e.printStackTrace();
}
try {
while(resultSet.next()) {
String user_name = (String)resultSet.getString("UserID");
System.out.println(user_name);
if(username.equals(user_name)) {
temp = 1;
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(temp);
if(temp == 0) {
try {
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
path = "login.jsp";
msg = "注册成功,请登录";
}else {
path = "regist.jsp";
msg = "用户已经存在,请换一个用户名注册";
}
}else {
path = "regist.jsp";
msg = "密码格式错误";
}
}else {
path = "regist.jsp";
msg = "两次输入密码不相同";
}
}else {
path = "regist.jsp";
msg = "账号格式错误";
}
}else {
msg = "验证码输入错误";
path = "regist.jsp";
}
System.out.println(msg + path);
request.setAttribute("username", username);
request.setAttribute("msg", msg);
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
- 加密方法:
package util;
public class Encryption {
String pwd = null;
char [] c;
public String secreat(String a) {
c = a.toCharArray();
for(int i = 0 ; i < c.length ; i++) {
c[i] = (char)(c[i]+64);
}
pwd = new String(c);
return pwd;
}
public String free(String a) {
c = a.toCharArray();
for(int i = 0 ; i < c.length ; i++) {
c[i] = (char)(c[i]-64);
}
pwd = new String(c);
return pwd;
}
public static void main(String [] args) {
//用来测试
String name = "Hello!";
Encryption a = new Encryption();
String mi = a.secreat(name);
System.out.println(mi);
String free = a.free(mi);
System.out.println(free);
}
}
- jsp文件几乎都发生了改变,先列出一个吧:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="util.Read_user" %>
<%@ page import="util.Read_student" %>
<%@ page import = "util.Read_course" %>
<%@ page import = "entity.Student" %>
<%@ page import = "entity.Course" %>
<%@ page import="entity.User" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<SCRIPT LANGUAGE=javascript>
function logout() {
var msg = "您真的确定要退出吗?";
if (confirm(msg)==true){
return true;
}else{
return false;
}
}
</SCRIPT>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style type="text/css">
/* 媒体查询 根据 用户设备的屏幕尺寸来显示, */
#all { width: 80%; height: 550px; background-color: pink;
margin: auto;overflow-y: auto;}
#nav{height: 100px; background-color: lightblue; text-align:center;color:yellow;}
#mid{height: 350px;}
#mid #midleft{width: 20%; height:350px ;float: left; text-align:center;}
#mid #midmid{width: 70%; height:350px ; background-color: lime;float: left;overflow-y: auto;overflow-x: auto;}
#mid #midright{width: 10%; height:350px ; background-color: khaki;float: left;}
#foot{height: 90px; background-color: yellow; text-align:center;padding-top:15px;
padding-left:30px;align:center;}
@media only screen and (max-width:1400px ) {
#all{width: 80%;}
}
@media only screen and (max-width:700px ) {
#all{width: 100%;}
}
#all ul{ margin: 0px; padding: 0px; list-style-type: none;
height: 60px; background-color: #C71585;}
#all li{ float: left; width: 20%;height: 60px;}
#all ul li a{display: block;height: 45px; text-align: center; font-size: 22px;
font-family: "courier new"; font-weight: 700; color: white; text-decoration: none;
/* 塞泡沫 */
padding-top:15px;
border-right: 2px solid white;
}
#all ul li a#a1{background-color: blue;}
#all ul li a#a2{background-color: forestgreen;}
#all ul li a#a3{background-color: black;}
#all ul li a#a4{background-color: forestgreen;}
#all ul li a#a5{background-color: blue;}
#all ul li a:hover {background-color: crimson;}
#all ul li a#a1:hover {background-color: gray;}
#all ul li a#a2:hover {background-color: gray;}
.search{
margin-left:36%;
width: 30%;
display: flex;
text-align:center;
float:left;
/*border: 1px solid red;*/
}
.search input{
float: left;
flex: 3;
height: 30px;
outline: none;
border: 1px solid red;
box-sizing: border-box;
padding-left: 15px;
}
.search button{
float: right;
flex: 1;
height: 30px;
background-color: red;
color: white;
border-style: none;
outline: none;
}
.search button i{
font-style: normal;
}
.search button:hover{
font-size: 16px;
}
</style>
</head>
<body>
<div id="all">
<div id="nav">
<ul>
<li><a href="user_main.jsp" id="a1" >学生列表</a></li>
<li><a href="" id="a2">选修课程</a></li>
<li><a href="" id = "a3">课程成绩</a></li>
<li><a href="" id = "a4">综合排名</a></li>
<li><a href="login.jsp" id = "a5" onclick="javascript:return logout()">退出登录</a></li>
</ul>
课程信息如下
</div>
<div id="mid">
<div id="midleft">
<br>
当前用户:
<br><br>
<table border = '1' cellspacing="1">
<tr>
<%
String username = (String)session.getAttribute("username");
System.out.println(username);
Read_user readuser = new Read_user();
List<?> list = readuser.read_user(username);
for(Object u1:list){
User u = (User)u1;
%>
<td width="200px">姓名</td>
<td width="200px"><%=u.getUsername() %></td>
</tr>
<tr>
<td>学校</td>
<td><%=u.getSchool_name() %></td>
</tr>
<tr>
<td>生日</td>
<td><%=u.getBirthday() %>
</tr>
<tr>
<td>电话</td>
<td><%=u.getMobile() %></td>
</tr>
<tr>
<td>身份</td>
<td>
<%
if(u.getIs_admin() == 1){
out.print("管 理 员");
}else{
out.print("普通用户");
}
%>
</td>
</tr>
<tr>
<td>头像</td>
<td>null</td>
</tr>
<%
}
%>
</table>
</div>
<div id="midmid">
<table border = '1' style="margin:auto ;text-align:center" >
<tr>
<td width="200px">学号</td>
<td width="200px">姓名</td>
<td width="200px">课程</td>
<td width="200px">分数</td>
</tr>
<%
Read_course readcourse = new Read_course();
List<?> list2 = readcourse.read_course();
for(Object u1:list2){
Course u = (Course)u1;
%>
<tr>
<td> <%=u.getSno() %> </td>
<td> <%=u.getName() %> </td>
<td> <%=u.getCourse_name()%> </td>
<td> <%=u.getGrade() %> </td>
</tr>
<%
}
%>
</table>
</div>
<div id="midright"></div>
</div>
<div id="foot">
<!-- <li><a href=" " id="a1" >增加信息</a></li>
<li><a href=" " id="a2">删除信息</a></li>
<li><a href=" ">修改信息</a></li>
-->
<form action="CourseResultServlet">
<div class="search" >
<input type="text" id = "v" placeholder="学号、姓名、课程名(模糊查询)" name="student_num" />
<button><i>搜索</i></button>
</div>
</form>
</div>
</div>
</body>
</html>