使用Servlet和JSP开发Java应用程序 ---- 学生登录页面(数据库)
1. HTML页面
目标页面 HTML页面的样式BootStrap 该页面主要使用的是bootstrap样式,自行下载添加即可
HTML代码
这个地方的代码和上一篇文章的页面是基本相似的 使用Servlet和JSP开发Java应用程序 ---- 查询四六成绩
这个地方的 action=’‘url’’ 这里就先不设置了,等到后面注册好了Servlet在进行添加
<!DOCTYPE html>
<html>
<head>
<title>用户登录</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="static/css/bootstrap.min.css"/>
<style>
.search-form{
width: 320px;
padding: 30px;
margin: 150px auto;
border-radius: 10px;
}
</style>
</head>
<body>
<div >
<form action="" class="bg-success text-center text-success form-inline search-form">
<h1>用户登录</h1>
<hr>
学 号:<input name="id" type="text" class="form-control">
<br><br>
密 码:<input name="pwd" type="text" class="form-control">
<br><br>
<button class="btn btn-default">登录</button>
</form>
</div>
</body>
</html>
2. 创建数据库
CREATE DATABASE qdu character set utf8 collate utf8_general_ci;
USE qdu;
CREATE TABLE student
(
sid char(10) primary key,
sname varchar(30) NOT NULL,
spassword varchar(30),
sgender char(1),
sbatch varchar(30)
);
INSERT INTO `student` VALUES ('2020204627', '王铸镔', '204627', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204628', '韩明睿', '204628', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204629', '李吉超', '204629', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204630', '代新天', '204630', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204631', '魏贤龙', '204631', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204632', '李泽宁', '204632', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204633', '宋本航', '204633', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204634', '张晓东', '204634', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204635', '崔圣豪', '204635', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204636', '刘子源', '204636', '女', '20软件BD02');
INSERT INTO `student` VALUES ('2020204637', '商天翔', '204637', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204638', '娄熙康', '204638', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204639', '刘树鑫', '204639', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204640', '葛瑜', '204640', '女', '20软件BD02');
INSERT INTO `student` VALUES ('2020204641', '王晨宇', '204641', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204642', '姜佳杞', '204642', '女', '20软件BD01');
INSERT INTO `student` VALUES ('2020204643', '王凯悦', '204643', '女', '20软件BD03');
INSERT INTO `student` VALUES ('2020204644', '吴镇宇', '204644', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204645', '薛逸飞', '204645', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204646', '张晓敏', '204646', '女', '20软件J01');
INSERT INTO `student` VALUES ('2020204647', '张梦梅', '204647', '女', '20软件J01');
INSERT INTO `student` VALUES ('2020204648', '陈志浩', '204648', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204649', '张传鹏', '204649', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204650', '鲁豪', '204650', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204651', '董轩华', '204651', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204652', '丁迅', '204652', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204653', '张嘉骏', '204653', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204654', '张昭昱', '204654', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204655', '崔晓晨', '204655', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204656', '高凯旋', '204656', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204657', '任彤彤', '204657', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204659', '孟睿', '204659', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204660', '王矽蕤', '204660', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204661', '薛玉慧', '204661', '女', '20软件BD01');
INSERT INTO `student` VALUES ('2020204662', '高晨驰', '204662', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204663', '杨骏鹏', '204663', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204664', '贾继强', '204664', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204665', '马红昊', '204665', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204666', '郑舒翊', '204666', '女', '20软件BD03');
INSERT INTO `student` VALUES ('2020204667', '柳轶伟', '204667', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204668', '崔方可', '204668', '女', '20软件BD02');
INSERT INTO `student` VALUES ('2020204669', '王冠澳', '204669', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204670', '陈俊如', '204670', '女', '20软件BD01');
INSERT INTO `student` VALUES ('2020204671', '吕俊毅', '204671', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204672', '徐文涛', '204672', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204673', '周成龙', '204673', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204675', '夏一帆', '204675', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204676', '李传瑜', '204676', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204677', '窦义尊', '204677', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204678', '王如', '204678', '女', '20软件BD02');
INSERT INTO `student` VALUES ('2020204679', '赵义', '204679', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204680', '闵祥超', '204680', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204681', '禚宣伟', '204681', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204682', '陈朋', '204682', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204683', '庞鲁汉', '204683', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204684', '杨孙哲', '204684', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204685', '范沐晨', '204685', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204686', '陈博文', '204686', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204687', '张嘉珺', '204687', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204688', '刘家琛', '204688', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204689', '夏培翔', '204689', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204691', '陈英杰', '204691', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204692', '郭克选', '204692', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204693', '王明瑞', '204693', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204694', '王昕', '204694', '女', '20软件BD01');
INSERT INTO `student` VALUES ('2020204695', '秦玉龙', '204695', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204696', '王岩', '204696', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204697', '沈资皓', '204697', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204698', '罗雅琪', '204698', '女', '20软件J01');
INSERT INTO `student` VALUES ('2020204699', '陈福正', '204699', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204700', '许亦非', '204700', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204701', '姜运金', '204701', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204702', '姜焜', '204702', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204703', '曲树森', '204703', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204704', '高扬', '204704', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204705', '戚佳辉', '204705', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204706', '李宝君', '204706', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204707', '马伯宁', '204707', '女', '20软件BD02');
INSERT INTO `student` VALUES ('2020204708', '于越', '204708', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204709', '石银超', '204709', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204710', '杨贵帅', '204710', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204711', '孟凡博', '204711', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204712', '王庆锴', '204712', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204713', '徐利发', '204713', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204714', '谢可乾', '204714', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204715', '段博文', '204715', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204716', '赵雅菲', '204716', '女', '20软件BD01');
INSERT INTO `student` VALUES ('2020204718', '吕文臣', '204718', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204719', '王艺泽', '204719', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204720', '雷天赐', '204720', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204721', '张弘来', '204721', '女', '20软件J01');
INSERT INTO `student` VALUES ('2020204722', '盖凯欣', '204722', '女', '20软件BD03');
INSERT INTO `student` VALUES ('2020204723', '祝奕骏', '204723', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204724', '刘家彤', '204724', '女', '20软件BD03');
INSERT INTO `student` VALUES ('2020204725', '徐子朋', '204725', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204726', '王家豪', '204726', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204727', '高雪婷', '204727', '女', '20软件J01');
INSERT INTO `student` VALUES ('2020204728', '王龙威', '204728', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204729', '郭聚辉', '204729', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204730', '马蔓蔓', '204730', '女', '20软件BD03');
INSERT INTO `student` VALUES ('2020204731', '刘峥', '204731', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204732', '侯明刚', '204732', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204733', '李鹏飞', '204733', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204734', '高超', '204734', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204735', '周广晗', '204735', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204736', '赵旭', '204736', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204737', '夏立辉', '204737', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204738', '赵子健', '204738', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204739', '胡鑫', '204739', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204740', '刘硕', '204740', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204741', '林梦森', '204741', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204742', '李向帅', '204742', '男', '20软件BD03');
INSERT INTO `student` VALUES ('2020204743', '马广旭', '204743', '男', '20软件BD02');
INSERT INTO `student` VALUES ('2020204744', '庄家宝', '204744', '男', '20软件J01');
INSERT INTO `student` VALUES ('2020204745', '朱茂泉', '204745', '男', '20软件BD01');
INSERT INTO `student` VALUES ('2020204746', '曹景涛', '204746', '男', '20软件BD03');
select * from student;
3. 创建dao包
为了方便操作表,通常会专门构建Dao对象,用于对数据库表执行增删改查(CRUD)操作
导入jar包
在WEB-INF下建立lib文件,然后将java的jar包导入 在源包下建立com.qdu.dao包,然后再该包下建立StudentDao文件
dao文件源码 注意改一下数据库的账号和密码
package com.qdu.dao;
import com.qdu.entity.Student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class StudentDao {
public Student findOneById(String sid) {
String driverClass = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/qdu?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "011216";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student s = null;
try {
Class.forName(driverClass);
con = DriverManager.getConnection(url, username, password);
ps = con.prepareStatement("select * from Student where sid=?");
ps.setString(1, sid);
rs = ps.executeQuery();
if (rs.next()) {
String sname = rs.getString(2);
String spwd = rs.getString(3);
String sgender = rs.getString(4);
String sbatch = rs.getString(5);
s = new Student(sid, sname, spwd, sgender, sbatch);
}
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (null != rs) {
rs.close();
}
if (null != ps) {
ps.close();
}
if (null != con) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
return s;
}
public List<Student> findByBatchname(String batchName) {
String driverClass = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/qdu?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "011216";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student s = null;
List<Student> list=new ArrayList<>();
try {
Class.forName(driverClass);
con = DriverManager.getConnection(url, username, password);
ps = con.prepareStatement("select * from Student where sbatch=?");
ps.setString(1, batchName);
rs = ps.executeQuery();
while (rs.next()) {
String sid = rs.getString(1);
String sname = rs.getString(2);
String spwd = rs.getString(3);
String sgender = rs.getString(4);
String sbatch = rs.getString(5);
s = new Student(sid, sname, spwd, sgender, sbatch);
list.add(s);
}
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (null != rs) {
rs.close();
}
if (null != ps) {
ps.close();
}
if (null != con) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(StudentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
return list;
}
}
4. 创建entity包
创建一个Student实体类,用于存储一个学生的五项信息
在源包下建立一个com.qdu.entity包,然后在该包下建立Student文件
5个私有变量
private String sid;
private String sname;
private String spassword;
private String sgender;
private String sbatch;
然后生成主函数
public Student(){
}
public Student(String sid, String sname, String spassword, String sgender, String sbatch) {
this.sid = sid;
this.sname = sname;
this.spassword = spassword;
this.sgender = sgender;
this.sbatch = sbatch;
}
get、set函数
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSpassword() {
return spassword;
}
public void setSpassword(String spassword) {
this.spassword = spassword;
}
public String getSgender() {
return sgender;
}
public void setSgender(String sgender) {
this.sgender = sgender;
}
public String getSbatch() {
return sbatch;
}
public void setSbatch(String sbatch) {
this.sbatch = sbatch;
}
完整代码
package com.qdu.entity;
public class Student {
private String sid;
private String sname;
private String spassword;
private String sgender;
private String sbatch;
public Student(){
}
public Student(String sid, String sname, String spassword, String sgender, String sbatch) {
this.sid = sid;
this.sname = sname;
this.spassword = spassword;
this.sgender = sgender;
this.sbatch = sbatch;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSpassword() {
return spassword;
}
public void setSpassword(String spassword) {
this.spassword = spassword;
}
public String getSgender() {
return sgender;
}
public void setSgender(String sgender) {
this.sgender = sgender;
}
public String getSbatch() {
return sbatch;
}
public void setSbatch(String sbatch) {
this.sbatch = sbatch;
}
}
4. Servlet编写
创建Servlet
新建com.qdu.servlet源包,并在包中新建LoginServlet.java文件,用于编写Servlet 实现Servlet
实现 servlet :implements Servlet Ctrl + Shift + I 快速导包:import javax.servlet.Servlet;
public class LoginServlet implements Servlet{
}
点小灯泡:实现所有的抽象方法
package com.qdu.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class LoginServlet implements Servlet{
@Override
public void init(ServletConfig config) throws ServletException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public ServletConfig getServletConfig() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public String getServletInfo() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void destroy() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
编写 service 方法
- 获取请求参数
String id = req.getParameter("id");
String pwd = req.getParameter("pwd");
- 编写处理请求的逻辑
StudentDao dao = new StudentDao();
Student s = dao.findOneById(id);
- 生成动态响应
res.setContentType("text/html;charset = utf-8");
PrintWriter out = res.getWriter();
out.println("<link rel=\"stylesheet\" href=\"static/css/bootstrap.min.css\"/>");
out.println("<div class=\"text-center text-success\">");
if (null != s && s.getSpassword().equals(pwd)) {
out.println("<br><h1 class=\"text-success text-center\">您好," + s.getSname() + "</h1><hr>");
out.println("<br>学号:" + s.getSid());
out.println("<br>姓名:" + s.getSname());
out.println("<br>密码:" + s.getSpassword());
out.println("<br>性别:" + s.getSgender());
out.println("<br>班级:" + s.getSbatch());
}else{
out.println("<br><h1 class=\"text-danger text-center\">登录失败,请检查学号和密码!!!</h1>");
out.println("<br>密码:" + s.getSpassword());
}
out.println("</div>");
5. Web.xml配置,注册Servlet
在WEB-INF文件下新建web.xml文件,进行Servlet的注册 直接使用代码生成
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<description>登录处理请求</description>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.qdu.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
注册好Servlet后,在html中要链接Servlet
6. 完整程序的页面
登录页面 登录失败页面 登录成功页面
|