布局界面:
文章来源地址https://www.toymoban.com/news/detail-519548.html
Login.css
@charset "UTF-8";
html{
width: 100%;
height: 100%;
overflow: hidden;
font-style: sans-serif;
}
body{
width: 100%;
height: 100%;
font-family: 'Open Sans',sans-serif;
margin: 0;
background-color: #808069;
}
#login{
position: absolute;
top: 50%;
left:50%;
margin: -150px 0 0 -150px;
width: 300px;
height: 300px;
}
#login h1{
color: #fff;
text-shadow:0 0 10px;
letter-spacing: 1px;
text-align: center;
}
h1{
font-size: 2em;
margin: 0.67em 0;
}
input{
width: 278px;
height: 18px;
margin-bottom: 10px;
outline: none;
padding: 10px;
font-size: 13px;
color: #fff;
text-shadow:1px 1px 1px;
border-top: 1px solid #312E3D;
border-left: 1px solid #312E3D;
border-right: 1px solid #312E3D;
border-bottom: 1px solid #56536A;
border-radius: 4px;
background-color: #2D2D3F;
}
.but{
width: 300px;
min-height: 20px;
display: block;
background-color: #4a77d4;
border: 1px solid #3762bc;
color: #fff;
padding: 9px 14px;
font-size: 15px;
line-height: normal;
border-radius: 5px;
margin: 0;
}
Register.css
@charset "UTF-8";
html{
width: 100%;
height: 100%;
overflow: hidden;
font-style: sans-serif;
}
body{
width: 100%;
height: 100%;
font-family: 'Open Sans',sans-serif;
margin: 0;
background-color: #808A87;
}
#login{
position: absolute;
top: 50%;
left:50%;
margin: -150px 0 0 -150px;
width: 300px;
height: 300px;
}
#login h1{
color: #fff;
text-shadow:0 0 10px;
letter-spacing: 1px;
text-align: center;
}
h1{
font-size: 2em;
margin: 0.67em 0;
}
input{
width: 278px;
height: 18px;
margin-bottom: 10px;
outline: none;
padding: 10px;
font-size: 13px;
color: #fff;
text-shadow:1px 1px 1px;
border-top: 1px solid #312E3D;
border-left: 1px solid #312E3D;
border-right: 1px solid #312E3D;
border-bottom: 1px solid #56536A;
border-radius: 4px;
background-color: #2D2D3F;
}
.but{
width: 300px;
min-height: 20px;
display: block;
background-color: #4a77d4;
border: 1px solid #3762bc;
color: #fff;
padding: 9px 14px;
font-size: 15px;
line-height: normal;
border-radius: 5px;
margin: 0;
}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆界面</title>
<link rel="stylesheet" type="text/css" href="static/Login.css"/>
</head>
<body>
<div id="login">
<h1>学生信息管理系统</h1>
<form method="post" action="LoginServlet">
<input type="text" required="required" placeholder="用户名" name="u"></input>
<input type="password" required="required" placeholder="密码" name="p"></input>
<button class="but" type="submit">登录</button>
<label for="password">
没有账号请
<a href="register.jsp" style="color:blue">注册</a>
</label>
<br>
<label>
<%
String state = (String) session.getAttribute("A2");
session.removeAttribute("A2");
if(state != null){
out.print(state);
}
%>
</label>
</form>
</div>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
<link rel="stylesheet" type="text/css" href="static/Register.css"/>
</head>
<body>
<div id="login">
<h1>注册</h1>
<form method="post" action="RegisterServlet">
<input type="text" required="required" placeholder="用户名" name="u"></input>
<input type="password" required="required" placeholder="密码" name="p"></input>
<button class="but" type="submit">注册</button>
<label for="password">
返回
<a href="login.jsp" style="color:blue">登录</a>界面
</label>
<br>
<label>
<%
String state = (String) session.getAttribute("A1");
session.removeAttribute("A1");
if(state != null){
out.print(state);
}
%>
</label>
</form>
</div>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.bean.StudentBean" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增删改查界面</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script>
//增加
function submitit1() {
document.form1.action = "AddStudentServlet"
//document.form1.submit();
}
//修改
function submitit3() {
document.form1.action = "UpdateStudentServlet"
}
//删除
function submitit2() {
document.form2.action = "DeleteStudentServlet"
}
//查询
function submitit4() {
document.form2.action = "SelectStudentServlet"
}
</script>
<style type="text/css">
h2{
font-family:宋体;
font-weight:normal;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<h2 class="text-center">
学生信息管理系统
</h2>
</div>
</div>
<div class="row clearfix">
<div class="col-md-10 column"></div>
<div class="col-md-2 column">
你好!欢迎${username} <a href="/StudentsMessageDemo/LoginOut">退出</a>
</div>
</div>
<p></p><hr style="border: 5px solid green;"/><p></p>
<div class="row clearfix">
<div class="col-md-8 column">
<table class="table">
<thead>
<tr>
<th>
学号
</th>
<th>
姓名
</th>
<th>
班级
</th>
<th>
年龄
</th>
<th>
专业
</th>
</tr>
</thead>
<tbody>
<%
ArrayList stuMessage = (ArrayList) session.getAttribute("stuMessage");
session.removeAttribute("stuMessage");
if(stuMessage != null){
for(int i = 0;i < stuMessage.size();i++){
StudentBean a = (StudentBean)stuMessage.get(i);
%>
<tr class="success">
<td>
<%=a.getSid() %>
</td>
<td>
<%=a.getSname() %>
</td>
<td>
<%=a.getScls() %>
</td>
<td>
<%=a.getAge() %>
</td>
<td>
<%=a.getMajor() %>
</td>
</tr>
<%
}
}
%>
</tbody>
</table>
</div>
<div class="col-md-4 column">
<form class="form-horizontal" role="form" name="form1" method="post">
<div class="form-group">
<label for="inputEmail31" class="col-sm-2 control-label">学号</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail31" name="sid" />
</div>
</div>
<div class="form-group">
<label for="inputPassword32" class="col-sm-2 control-label">姓名</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword32" name="sname" />
</div>
</div>
<div class="form-group">
<label for="inputPassword33" class="col-sm-2 control-label">班级</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword33" name="scls" />
</div>
</div>
<div class="form-group">
<label for="inputPassword34" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword34" name="age" />
</div>
</div>
<div class="form-group">
<label for="inputPassword35" class="col-sm-2 control-label">专业</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword35" name="major" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" onclick="submitit1()">添加学生信息</button>
<button type="submit" class="btn btn-default" onclick="submitit3()">更改学生信息</button>
<br><br>
<%
String state = (String) session.getAttribute("B2");
session.removeAttribute("B2");
if(state != null){
out.print(state);
}
%>
</div>
</div>
</form>
<form class="form-horizontal" role="form" name="form2" method="post">
<div class="form-group">
<label for="inputEmail36" class="col-sm-2 control-label">学号</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail36" name="sid" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" onclick="submitit2()">删除学生信息</button>
<button type="submit" class="btn btn-default" onclick="submitit4()">查询学生信息</button>
<br><br>
<%
state = (String) session.getAttribute("B1");
session.removeAttribute("B1");
if(state != null){
out.print(state);
}
%>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
StudentBean.java
package com.bean;
public class StudentBean {
private String sid;
private String sname;
private String scls;
private String age;
private String major;
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 getScls() {
return scls;
}
public void setScls(String scls) {
this.scls = scls;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
AdminDao.java
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.util.DBUtil;
public class AdminDao {
//注册
public static boolean register(String admin,String password) {
Connection conn = DBUtil.getConnectDb();
String sql = "insert into auser (admin,password) values(?,?)";
int a = 0;
PreparedStatement stm = null;
boolean sta = false;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, admin);
stm.setString(2, password);
a = stm.executeUpdate();
if(a == 0) {
sta = false;
}else {
sta = true;
}
return sta;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//登录
public static boolean login(String admin,String password) {
Connection conn = DBUtil.getConnectDb();
String sql = "select * from auser where admin=? and password=?";
ResultSet rs;
PreparedStatement stm = null;
boolean sta = false;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, admin);
stm.setString(2, password);
rs = stm.executeQuery();
if(rs.next()) {
sta = true;
}else {
sta = false;
}
return sta;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
StudentDao.java
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.bean.StudentBean;
import com.util.DBUtil;
public class StudentDao {
//添加
public static boolean addStudent(String sid,String sname,String scls,String age,String major) {
Connection conn = DBUtil.getConnectDb();
String sql = "INSERT INTO stus VALUES(?,?,?,?,?)";
int rs;
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1,sid);
stm.setString(2,sname);
stm.setString(3,scls);
stm.setString(4,age);
stm.setString(5,major);
boolean a = false;
rs = stm.executeUpdate();
if(rs == 1) {
return true;
}else {
return false;
}
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
//更改
public static boolean updateStudent(String sid,String sname,String scls,String age,String major) {
Connection conn = DBUtil.getConnectDb();
String sql = "UPDATE stus SET sname=?,scls=?,age=?,major=? WHERE sid=?";
int rs;
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1,sname);
stm.setString(2,scls);
stm.setString(3,age);
stm.setString(4,major);
stm.setString(5,sid);
boolean a = false;
rs = stm.executeUpdate();
if(rs == 1) {
return true;
}else {
return false;
}
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
//删除
public static boolean deleteStudent(String sid) {
Connection conn = DBUtil.getConnectDb();
String sql = "DELETE FROM stus WHERE sid=?";
int rs;
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1,sid);
boolean a = false;
rs = stm.executeUpdate();
if(rs == 1) {
return true;
}else {
return false;
}
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
//查询所有信息
public static ArrayList getList() {
ArrayList studentArray = new ArrayList();
Connection conn = DBUtil.getConnectDb();
String sql = "SELECT * FROM stus";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while (rs.next()) {
StudentBean m = new StudentBean();
m.setSid(rs.getString("sid"));
m.setSname(rs.getString("sname"));
m.setScls(rs.getString("scls"));
m.setAge(rs.getString("age"));
m.setMajor(rs.getString("major"));
studentArray.add(m);
}
rs.close();
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return studentArray;
}
//查询单个学生信息
public static ArrayList getList(String sid) {
ArrayList studentArray = new ArrayList(); //建立一个数组集合
Connection conn = DBUtil.getConnectDb();
String sql = "SELECT * FROM stus WHERE sid=?";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1,sid);
rs = stm.executeQuery();
while (rs.next()) {
StudentBean m = new StudentBean();
m.setSid(rs.getString("sid"));
m.setSname(rs.getString("sname"));
m.setScls(rs.getString("scls"));
m.setAge(rs.getString("age"));
m.setMajor(rs.getString("major"));
studentArray.add(m);
}
rs.close();
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return studentArray;
}
}
LoginFilter.java
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebFilter("/*")
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
HttpSession session = request.getSession();
String requestURI = request.getRequestURI();
System.out.println(requestURI);
if(requestURI.contains("/login.jsp")
||requestURI.contains("/LoginServlet")
||requestURI.contains("/LoginDelete")
||requestURI.contains("/register.jsp")
||requestURI.contains("/Login.css")
||requestURI.contains("/Register.css")
||requestURI.contains("/RegisterServlet")) {
chain.doFilter(req, res);
return;
}
if(session.getAttribute("username")!=null) {
chain.doFilter(req, res);
}else{
response.sendRedirect("login.jsp");
}
}
}
AddStudentServlet.java
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.StudentDao;
/**
* Servlet implementation class AddStudentServlet
*/
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddStudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String id = request.getParameter("sid");
String name = request.getParameter("sname");
String cls = request.getParameter("scls");
String age = request.getParameter("age");
String major = request.getParameter("major");
boolean a = StudentDao.addStudent(id, name, cls, age, major);
if(a == true) {
request.getSession().setAttribute("B2", "添加学生信息成功!");
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}else {
request.getSession().setAttribute("B2", "添加失败,请检查学号!");
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}
}
}
DeleteStudentServlet.java
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.StudentDao;
/**
* Servlet implementation class DeleteStudentServlet
*/
@WebServlet("/DeleteStudentServlet")
public class DeleteStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteStudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String id = request.getParameter("sid");
boolean a = StudentDao.deleteStudent(id);
if(a == true) {
request.getSession().setAttribute("B1", "删除学生信息成功!");
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}else {
request.getSession().setAttribute("B1", "删除学生信息失败!");
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}
}
}
LoginOut.java
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginOut
*/
@WebServlet("/LoginOut")
public class LoginOut extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginOut() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.getSession().invalidate();
response.sendRedirect("/StudentsMessageDemo/login.jsp");
}
}
LoginServlet.java
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dao.AdminDao;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
//获取注册信息
String username = request.getParameter("u");
String password = request.getParameter("p");
boolean a = AdminDao.login(username, password);
//登录
if(a == true) {
HttpSession session = request.getSession();
//将username保存在session中
session.setAttribute("username",username);
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}else {
request.getSession().setAttribute("A2", "账号或密码错误请重新登录!");
response.sendRedirect("/StudentsMessageDemo/login.jsp");
}
}
}
RegisterServlet.java
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.AdminDao;
/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
//获取注册信息
String username = request.getParameter("u");
String password = request.getParameter("p");
boolean a = AdminDao.register(username, password);
//将注册信息存入数据库,在返回登录
if(a == true) {
request.getSession().setAttribute("A1", "注册账号成功!");
response.sendRedirect("/StudentsMessageDemo/register.jsp");
}else {
request.getSession().setAttribute("A1", "注册账号失败!");
response.sendRedirect("/StudentsMessageDemo/register.jsp");
}
}
}
SelectStudentServlet.java
package com.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.StudentDao;
/**
* Servlet implementation class SelectStudentServlet
*/
@WebServlet("/SelectStudentServlet")
public class SelectStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectStudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
//获取注册信息
String id = request.getParameter("sid");
ArrayList a;
if(id.equals("")) {
a = StudentDao.getList();
request.getSession().setAttribute("B1", "查询学生信息成功!");
}else {
a = StudentDao.getList(id);
}
request.getSession().setAttribute("stuMessage", a);
//System.out.println(a);
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}
}
UpdateStudentServlet.java
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.StudentDao;
/**
* Servlet implementation class UpdateStudentServlet
*/
@WebServlet("/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateStudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
//获取注册信息
String id = request.getParameter("sid");
String name = request.getParameter("sname");
String cls = request.getParameter("scls");
String age = request.getParameter("age");
String major = request.getParameter("major");
boolean a = StudentDao.updateStudent(id, name, cls, age, major);
//登录
if(a == true) {
request.getSession().setAttribute("B2", "修改学生信息成功!");
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}else {
request.getSession().setAttribute("B2", "修改失败,请检查学号!");
response.sendRedirect("/StudentsMessageDemo/index.jsp");
}
}
}
DBUtil.java
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
public static String url="jdbc:mysql://localhost:3306/students?serverTimezone=UTC&useSSL=true&characterEncoding=utf-8";
public static String username="root";
public static String password="";//密码根据自己的自行添加
static {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e) {
System.out.println(e);
}
}
public static Connection getConnectDb() {
Connection conn = null;
try {
System.out.println("正在连接数据库....");
conn=DriverManager.getConnection(url,username,password);
System.out.println("连接成功");
}
catch (SQLException e) {
System.out.println(e);
}
return conn;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>StudentsMessageDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
数据库:
最终效果图:
登陆界面
注册界面
主界面
文章来源:https://www.toymoban.com/news/detail-519548.html
到了这里,关于JAVAWEB学生信息管理系统保姆级教程(增删改查+登录注册+Filter+mysql)eclipse版的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!