图书管理系统( ( JSP + JDBC + Servlet ) )实现-05: 实现登录功能
Posted Z && Y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图书管理系统( ( JSP + JDBC + Servlet ) )实现-05: 实现登录功能相关的知识,希望对你有一定的参考价值。
01: 流程分析和数据库建表阶段
02: 编写和配置过滤器(防止页面乱码)
03:项目搭建 & 工具类的实现 &依赖导入
04: 建立数据库的实体类(pojo)
05: 实现登录功能
06: 查询所有书籍功能
07:模糊查询功能
08:页面所有功能实现(附源码下载)
1.9 实现登录功能
1.9.1 把index.jsp移到jsp文件夹下面
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>重庆理工大学图书管理系统</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1 style="text-align: center;color: orange;">
重庆理工大学图书管理系统CQUTLMS
</h1>
<a href="${pageContext.request.contextPath}/jumpToBorrow?stuId=${studentSession.stuId}"
class="btn btn-primary"
style="margin: auto;width: 200px;height: 35px;border-radius: 15px;line-height: 20px;font-size: 18px;">我借的书籍</a>
<input type="text" readonly value="当前用户: ${studentSession.stuId}" disabled="disabled"
style="margin-left: 500px;border-radius: 10px">
<a href="${pageContext.request.contextPath}/jumpToLogin"
class="btn btn-primary"
style="margin: auto;width: 200px;height: 35px;border-radius: 15px;line-height: 20px;font-size: 18px;">注销登录</a>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/add"
style="background-color:#ccc;width: 150px; color: black;font-weight: bold;font-size: 16px"
>新增</a>
</div>
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/books"
style="background-color:#ccc;width: 150px; color: black;font-weight: bold;font-size: 16px">显示所有书籍</a>
</div>
<div class="col-md-4 column">
<form method="post" action="${pageContext.request.contextPath}/getBooksByName">
<label>
<input type="text" name="bookName" class="form-control" placeholder="请输入书籍名称" required>
</label>
<input type="submit" value="查询" class="btn btn-primary"
style="background-color:#ccc;width: 50px; color: black;font-weight: bold;font-size: 16px">
</form>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>图书编号</th>
<th>图书名</th>
<th>作者</th>
<th>库存</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.get('books')}">
<tr>
<td>${book.getBookId()}</td>
<td>${book.getBookName()}</td>
<td>${book.getAuthor()}</td>
<td>${book.getInventory()}</td>
<td>
<a href="${pageContext.request.contextPath}/update?bookId=${book.getBookId()}">更改</a>
|
<a href="${pageContext.request.contextPath}/delBook?bookId=${book.getBookId()}">删除</a>
|
<a href="${pageContext.request.contextPath}/borrowBook?bookName=${book.getBookName()}&
bookId=${book.getBookId()}&author=${book.getAuthor()}&stuId=${studentSession.stuId}" onclick="x()">借书</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
1.9.2 创建登录页面 login.jsp
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<link rel="stylesheet" href="static/css/login.css">
<body class="body">
<div class="box">
<div class="login">
<h1>重庆理工大学图书管理系统CQUTLMS</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
<span class="font errorMsg">${error}</span><br>
<span class="font">学生学号: </span><input type="text" name="stuId" class="subInput" placeholder="请输入学号">
<br>
<span class="font pwd">密码: </span><input type="password" name="password" class="subInput"
placeholder="请输入密码"> <br>
<br>
<input type="submit" value="登录" class="subBtn">
</form>
</div>
</div>
</body>
</html>
1.9.3 新建样式文件 login.css
.body {
background-color: #0093E9;
}
.box .login {
margin-left: 38%;
margin-top: 15%;
}
.box .login .subInput {
width: 200px;
height: 25px;
border-radius: 10px;
margin-top: 20px;
}
.box .login .pwd {
margin-left: 34px;
}
.box .login .subBtn {
font-size: large;
font-weight: bold;
color: white;
width: 120px;
height: 40px;
margin-left: 125px;
border-radius: 10px;
background-color: orange;
}
.login h1 {
font-family: "楷体";
font-size: 36px;
font-weight: 900;
color: orange;
font-style: italic;
margin-left: -90px;
}
.font {
font-family: "楷体";
font-style: italic;
font-size: large;
font-weight: bold;
color: black;
}
.errorMsg {
color: red;
font-size: 30px;
font-style: inherit;
margin-left: 50px;
}
1.9.4 把登录页面(login.jsp)设置为首页
web.xml
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
1.9.5 了解三层架构
三层架构是指:视图层View、服务层Service、
持久层Dao,分别完成不同的功能。
- View层:用于接收用户提交请求的代码在这里编写。
- Service层:系统的业务逻辑主要在这里编写。
- Dao层:直接操作数据库的代码在这里编写。
为了更好的降低各层间的耦合度,在三层架构程序设计中,采用面向抽象编程。
即上层对下层的调用,是通过接口实现的。
而下层对上层的真正服务提供者,是下层接口的实现类。
服务标准(接口)是相同的,服务提供者(实现类)可以更换。
这就实现了层间的耦合。
1.9.6 建立 StudentDao
StudentDao.java
package com.tian.dao.student;
import com.tian.pojo.Student;
import java.sql.Connection;
/**
* ClassName: StudentDao
* Description: students表的dao层
*
* @author Tianjiao
* @date 2021/5/28 14:27
*/
public interface StudentDao {
/**
* MethodName: getLoginStudent
* Description: 通过stuId获得登录图书管理系统的学生信息
*
* @return com.tian.pojo.Student
* @date 2021/5/28 14:31
* @params: [connection, getStuId]
* @author Tianjiao
*/
public Student getLoginStudent(Connection connection, String stuId) throws Exception;
}
1.9.7 建立 StudentDaoImpl
StudentDaoImpl.java
package com.tian.dao.student;
import com.tian.pojo.Student;
import com.tian.utils.BaseDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* ClassName: StudentDaoImpl
* Description: getLoginStudent的具体实现
*
* @author Tianjiao
* @date 2021/5/28 14:27
*/
public class StudentDaoImpl implements StudentDao {
@Override
public Student getLoginStudent(Connection connection, String stuId) throws Exception {
PreparedStatement preparedStatement = null;
ResultSet rs = null;
Student student = null;
if (null != connection) {
String sql = "select * from `students` where stuId=?";
Object[] params = {stuId};
rs = BaseDao.executeQuery(connection, preparedStatement, rs, sql, params);
if (rs.next()) {
student = new Student();
student.setCollege(rs.getString("college"));
student.setGender(rs.getString("gender"));
student.setProfession(rs.getString("profession"));
student.setStartYear(rs.getString("startYear"));
student.setStuId(rs.getString("stuId"));
student.setStuName(rs.getString("stuName"));
student.setPassword(rs.getString("password"));
}
BaseDao.closeResource(null, preparedStatement, rs);
}
return student;
}
}
1.9.8 建立 StudentService
StudentService.java
package com.tian.service.student;
import com.tian.pojo.Student;
/**
* ClassName: StudentService
* Description: service层调用dao层
*
* @author Tianjiao
* @date 2021/5/28 14:52
*/
public interface StudentService {
/**
* MethodName: login
* Description: 根据stuId和password登录系统的方法
*
* @return com.tian.pojo.Student
* @date 2021/5/28 14:56
* @params: [stuId, userPassword]
* @author Tianjiao
*/
public Student login(String stuId, String userPassword);
}
1.9.9 建立 StudentServiceImpl
package com.tian.service.student;
import com.tian.dao.student.StudentDao;
import com.tian.dao.student.以上是关于图书管理系统( ( JSP + JDBC + Servlet ) )实现-05: 实现登录功能的主要内容,如果未能解决你的问题,请参考以下文章
图书管理系统( ( JSP + JDBC + Servlet ) )实现-06: 查询所有书籍功能
图书管理系统( JSP + JDBC + Servlet)实现-07:模糊查询功能
图书管理系统( JSP + JDBC + Servlet )实现-08:页面所有功能实现
图书管理系统( ( JSP + JDBC + Servlet ) )实现-04: 建立数据库的实体类(pojo)