源码自行下载:同学录源码
todo:本文已经包含了所有代码,因此不用积分下载也可以。
本文目录
- 0. 前言
- 1. 开发环境
- 2. 涉及技术点
- 3. 实现功能
- 4. 界面展示
- 5. 项目目录及实现
- 5.1 数据库连接
- DBUtils.java
- jdbc.properties
- 5.2 显示层相关代码
- 登录页面login.jsp
- 同学录主页list.jsp
- 查找结果页面find.jsp
- 5.3 数据库访问代码
- UserDao.java
- StudentDao.java
- 5.4 实体类
- Student.java
- User.java
- 5.5 控制类
- LoginServlet.java
- ShowSevlet.java
- DeleteServlet.java
- FindServlet.java
- UpdateServlet.java
- AddServlet.java
- 5.6 过滤器
- LoginFilter.java
- 6. 总结与反思
本文记录了完整的实现代码,若有不足之处,欢迎批评指正!
Eclipse
Mysql
jdk1.8
Tomcat 7
MVC模式、JavaWeb、JDBC、jsp、css、bootstrap
1. 学生信息的输入、更新、修改、删除、保存
2. 学生信息的查询(按名字查询、学号查询、地区查询)
数据库包含两个表,对应字段请查看实体类部分
package com.utils;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class DBUtils {
private static String driver;
private static String url;
private static String user;
private static String password;
static Connection conn = null;
static {
Properties properties = new Properties(); // 创建一个properties对象
Reader inReader; // 一个reader属性;
try {
String path = Thread.currentThread().getContextClassLoader().getResource("jdbc.properties").getPath();
inReader = new FileReader(path); // 用这个方法获取properties配置文件;
properties.load(inReader); // 加载load内部的地址;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 通过properties.getProperty("driver");方法获取值
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
}
public static Connection open() {
try {
Class.forName(driver);
System.out.println("连接成功!");
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
System.out.println("连接数据库失败!");
}
return null;
}
public static Connection close() {
if (conn != null) {
try {
conn.close();// 关闭数据库
System.out.println("关闭...");
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库关闭失败...");
}
}
return null;
}
}
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/javaweb_final?useUnicode=true&characterEncoding=utf8
user=root
password=root
因为写这个系统的时候还没有学过样式,所以这个样式也是在网上搜索到的。有时间一定要把CSS掌握。
该样式使用来源:一款很漂亮的CSS3登录界面
自己用js语句加了一个“未登录”及“账号/密码错误”情况的提示框,tip的值分别来源于LoginServlet和LoginFilter(Servlet用于判断账户名和密码,Filter用来防止未登录就查看主页信息)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>登录界面title>
<link rel="stylesheet" href="css/style.css">
head>
<body>
<%-- <p><font color="red">${tip}font>p> --%>
<script>
var errori ="${tip}";
if(errori=='账号或密码错误'){
alert("账号或密码错误,请重新登录!");
}else if(errori=='未登录'){
alert("未登录,请先登录!");
}
script>
<div class="wrap">
<div class="container">
<h1>Welcomeh1>
<form action="LoginServlet" method="post">
<input type="text" name="username"placeholder="your username"/>
<input type="password" name="password"placeholder="your password"/>
<input type="submit" value="Login"/>
form>
div>
<ul>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
ul>
div>
body>
html>
一开始只用了一个非常简单的显示表格,看起来非常丑,所以使用了bootstrap。在head里链接相关包即可。然后按照模态框的对着写就可以了,添加编辑删除部分都是采用的模态框,查找部分的话就直接展示在页面上了,可以进行条件选择。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<script src="<%=path%>/resource/jquery-1.11.1.js">script>
<link rel="stylesheet" href="<%=path%>/resource/bootstrap.css">
<script src="<%=path%>/resource/bootstrap.min.js">script>
<title>同学录管理系统title>
head>
<body>
<c:if test="${list==null }">
<jsp:forward page="ShowServlet">jsp:forward>
c:if>
<h2 style="text-align: center;">同学录管理系统h2>
<section class="container">
<div class="row">
<div class="col-md-3">
<button class="btn btn-danger" data-toggle="modal"
data-target="#addModal" >添加好友button>
div>
div>
<div>
<div>
<center>
<form action="FindServlet" method="post">
按 <select name="findway">
<option value="id">学号option>
<option value="name">姓名option>
<option value="place">地区option>
select>
<input type="text" name="type"/>
<button class="submit" type="submit" name="query" value="true" tabindex="80">查询button>
form>
center>
div>
<br/>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>序号th>
<th>姓名th>
<th>年龄th>
<th>性别th>
<th>地区th>
<th>电话th>
<th>操作th>
tr>
<c:forEach items="${list}" var="list">
<tr>
<th>${list.id}th>
<th>${list.name}th>
<th>${list.age}th>
<th>${list.sex}th>
<th>${list.place}th>
<th>${list.tel}th>
<th ><button class="btn btn-primary edBtn"
data-toggle="modal" data-target="#edModal"
value="${list.id}"
n="${list.name}"
a="${list.age}"
s="${list.sex}"
p="${list.place}"
t="${list.tel}"
>修改button>
<button class="btn btn-danger delBtn" data-toggle="modal"
data-target=".delModal" value="${list.id }">删除button>
th>
tr>
c:forEach>
table>
div>
section>
body>
<div class="modal fade" id="addModal" tabindex="-1" role="dialog"
aria-labelledby="addModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×span>
button>
<h4 class="modal-title" id="exampleModalLabel">添加信息h4>
div>
<form action="AddServlet" method="post">
<div class="modal-body">
<input id="edId" name="id" value="" hidden>
<div class="form-group">
<label for="name" class="control-label">姓名:label>
<input type="text" name="name" class="form-control" value="" >
div>
<div class="form-group">
<label for="sex" class="control-label">性别:label>
<input type="text" name="sex" class="form-control" value="" >
div>
<div class="form-group">
<label for="age" class="control-label">年龄:label>
<input type="text" name="age" class="form-control" value="" >
div>
<div class="form-group">
<label for="place" class="control-label">地区:label>
<input type="text" name="place" class="form-control" value="" >
div>
<div class="form-group">
<label for="tel" class="control-label">电话:label>
<input type="text" name="tel" class="form-control" value="" >
div>
div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取消button>
<button type="submit" class="btn btn-primary">添加button>
div>
form>
div>
div>
div>
<div class="modal fade delModal" tabindex="-1" role="dialog"
aria-labelledby="gridSystemModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×span>
button>
<h4 class="modal-title" id="gridSystemModalLabel">提示h4>
div>
<div class="modal-body">
<h4>确定要删除吗?h4>
div>
<div class="modal-footer">
<form action="DeleteServlet">
<input id="delid" name="id" value="" hidden>
<button type="submit" class="btn btn-danger">删除button>
<button type="button" class="btn btn-primary" data-dismiss="modal">取消button>
form>
div>
div>
div>
div>
<div class="modal fade" id="edModal" tabindex="-1" role="dialog"
aria-labelledby="edModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×span>
button>
<h4 class="modal-title" id="exampleModalLabel">用户信息h4>
div>
<form action="UpdateServlet" method="post">
<div class="modal-body">
<input id="id" name="id" value=" " type="hidden">
<div class="form-group">
<label for="name" class="control-label">姓名:label> <input
type="text" name="name" class="form-control" id="name"
value="" >
div>
<div class="form-group">
<label for="age" class="control-label">年龄:label> <input
type="text" name="age" class="form-control" id="age"
value="" >
div>
<div class="form-group">
<label for="sex" class="control-label">性别:label> <input
type="text" name="sex" class="form-control" id="sex"
value="" >
div>
<div class="form-group">
<label for="place" class="control-label">地区:label> <input
type="text" name="place" class="form-control" id="place"
value="" >
div>
<div class="form-group">
<label for="tel" class="control-label">电话:label> <input
type="text" name="tel" class="form-control" id="tel"
value="" >
div>
div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取消button>
<button type="submit" class="btn btn-primary">更新button>
div>
form>
div>
div>
div>
<script type="text/javascript">
// 删除按钮
$(".delBtn").on("click", function() {
$("#delid").val($(this).val()); // 获取当前点击按钮的值value值,并赋值给删除模态框的
});
// 编辑按钮
$(".edBtn").on("click", function() {
$("#id").val($(this).val()); // 获取当前点击按钮的值value值,并赋值给编辑模态框的
$("#name").val($(this).attr("n"));
$("#age").val($(this).attr("a"));
$("#sex").val($(this).attr("s"));
$("#place").val($(this).attr("p"));
$("#tel").val($(this).attr("t"));
});
script>
html>
和主页差不多,修改和删除功能的话没有花心思加弹出框了。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<script src="<%=path%>/resource/jquery-1.11.1.js">script>
<link rel="stylesheet" href="<%=path%>/resource/bootstrap.css">
<script src="<%=path%>/resource/bootstrap.min.js">script>
<title>查找结果title>
head>
<body>
<section class="container">
<div class="row">
<center><p><h3>查找结果h3>p>
<p><a href="ShowServlet">返回a>p>
center>
<center>
<table class="table table-striped table-bordered table-hover">
<tr>
<td>学号td>
<td>姓名td>
<td>年龄td>
<td>性别td>
<td>地区td>
<td>电话td>
<td>操作td>
tr>
<c:forEach items="${find}" var="list">
<form method="post" name="myForm${list.id}" action="UpdateServlet?id=${list.id}">
<tr>
<td>${list.id}td>
<td><input type="text" value="${list.name}" style="border:none" name="name"/>td>
<td><input type="text" value="${list.age}" style="border:none" name="age"/>td>
<td><input type="text" value="${list.sex}" style="border:none" name="sex"/>td>
<td><input type="text" value="${list.place}" style="border:none" name="place"/>td>
<td><input type="text" value="${list.tel}" style="border:none" name="tel"/>td>
<td><a href="javascript:document.myForm${list.id}.submit();">修改a>
<a href="DeleteServlet?id=${list.id}">删除td>
tr>
form>
c:forEach>
table>
center>
div>
section>
body>
html>
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.JavaBean.Student;
import com.JavaBean.User;
import com.utils.DBUtils;
public class UserDao {
public User checkLogin(User user) {
String sql = "select id,username,password from user where username=? and password=?";
User u= null;
try {
Connection conn = DBUtils.open();
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, user.getUserName());
pst.setString(2, user.getPassword());
ResultSet rs = pst.executeQuery();
while (rs.next()) {
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("username"));
user.setPassword(rs.getString("password"));
u=user;
System.out.println("uuu"+u);
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return u;
}
}
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.JavaBean.Student;
import com.utils.DBUtils;
public class StudentDao {
/**
* 所有学生信息
*
* @return
*/
public List<Student> findallStudent() {
List<Student> list = new ArrayList<>();
Connection conn = DBUtils.open();
String sql = "select * from student";
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
list.add(student);
System.out.println("--------StudenDao:"+student);
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 添加学生
*
* @param student
* @return
*/
public boolean addStudent(Student student) {
String sql = "insert into student (name,age,sex,place,tel) values(?,?,?,?,?)";
Connection conn = DBUtils.open();
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setString(2, student.getAge());
pst.setString(3, student.getSex());
pst.setString(4, student.getPlace());
pst.setString(5, student.getTel());
int count = pst.executeUpdate();//执行
pst.close();
return count > 0 ? true : false;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 更新学生信息
*
* @param student
* @return
*/
public boolean updateStudent(Student student) {
String sql = "update student set name=?,age=?,sex=?,place=?,tel=? where id=?";
Connection conn = DBUtils.open();
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setString(2, student.getAge());
pst.setString(3, student.getSex());
pst.setString(4, student.getPlace());
pst.setString(5, student.getTel());
pst.setInt(6,student.getId());
int count = pst.executeUpdate();
pst.close();
return count > 0 ? true : false;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 删除学生信息
*
* @param studentId
* @return
*/
public int deleteStudent(int id) {
String sql = "delete from student where id = ?";
Connection conn = DBUtils.open();
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, id);
int count = pst.executeUpdate();//执行
pst.close();
return count > 0 ? 1 : 0;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
/**
* 根据id查询学生
*
* @param id
* @return
*/
public Student getStudentById(int id) {
Connection conn = DBUtils.open();
String sql = "select * from student where id= "+id ;
Student student = new Student();
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
}
System.out.println("FindID----------");
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
/**
* 同Id学生
*
* @param id
* @return
*/
public List<Student> FindStudentById(int id) {
List<Student> list = new ArrayList<>();
Connection conn = DBUtils.open();
String sql = "select* from student where id=?";
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
list.add(student);
System.out.println("---FindAllSameIDStudent");
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 根据name查询学生
*
* @param name
* @return
*/
public Student getStudentByName(String name) {
Connection conn = DBUtils.open();
String sql = "select * from student where name= "+"name" ;
Student student = new Student();
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
}
System.out.println("FindName----------");
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
/**
* 同姓名的所有学生
*
* @param name
* @return
*/
public List<Student> FindStudentByName(String name) {
List<Student> list = new ArrayList<>();
Connection conn = DBUtils.open();
String sql = "select * from student where name like '%"+name+"%' ";
try {
PreparedStatement pst = conn.prepareStatement(sql);
//pst.setString(1, name);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
list.add(student);
System.out.println("---FindAllSameNameStudent");
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 根据place查询学生
*
* @param place
* @return
*/
public Student getStudentByPlace(String place) {
Connection conn = DBUtils.open();
String sql = "select * from student where place= "+"place";
Student student = new Student();
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
}
System.out.println("FindPlace----------");
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
/**
* 同地区的所有学生
*
* @param place
* @return
*/
public List<Student> FindStudentByPlace(String place) {
List<Student> list = new ArrayList<>();
Connection conn = DBUtils.open();
String sql = "select * from student where place like '%"+place+"%' ";
try {
PreparedStatement pst = conn.prepareStatement(sql);
// pst.setString(1, place);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
list.add(student);
System.out.println("---FindAllSamePlaceStudent");
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
package com.JavaBean;
public class Student {
private Integer id;
private String name;
private String age;
private String place;
private String sex;
private String tel;
public Student(Integer id, String name, String age,String sex,String place,String tel) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex=sex;
this.place=place;
this.tel=tel;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex) {
this.sex=sex;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place=place;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel=tel;
}
}
package com.JavaBean;
public class User {
private Integer id;
private String userName;
private String password;
public User(Integer id, String userName, String password) {
super();
this.id = id;
this.userName = userName;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}
package com.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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.JavaBean.User;
import com.dao.UserDao;
import com.utils.DBUtils;;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+""+password);
UserDao ud=new UserDao();
User user=new User();
user.setUserName(username);
user.setPassword(password);
User u=ud.checkLogin(user);
if(u != null) {
request.getRequestDispatcher("list.jsp").forward(request, response);
System.out.println("登录成功");
}else {
System.out.println("登录失败!");
request.setAttribute("tip", "账号或密码错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
}
package com.Servlet;
import java.io.IOException;
import java.util.List;
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;
import com.JavaBean.Student;
/**
* Servlet implementation class ShowServlet
*/
@WebServlet("/ShowServlet")
public class ShowServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ShowServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
StudentDao dao = new StudentDao();
List<Student> list;
list = dao.findallStudent();
System.out.println("-----------ShowServlet:"+list);
request.setAttribute("list", list);
request.getRequestDispatcher("list.jsp").forward(request, response);
}
}
package com.Servlet;
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;
import java.io.IOException;
@WebServlet("/DeleteServlet")
@SuppressWarnings("serial")
public class DeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
StudentDao dao = new StudentDao();
int i;
i=dao.deleteStudent(id);
if(i>0) {
request.getRequestDispatcher("ShowServlet").forward(request, response);
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
}
package com.Servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.JavaBean.Student;
import com.dao.StudentDao;
@WebServlet("/FindServlet")
public class FindServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public FindServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, 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 findway=request.getParameter("findway");
String type=request.getParameter("type");
System.out.println("findway="+findway);
System.out.println("findtype="+type);
StudentDao dao=new StudentDao();
List<Student> find =new ArrayList<>();
System.out.println("---FindServlet-----");
if(findway.equals("id")) {
int id=Integer.parseInt(type);
System.out.println("findid="+id);
find = dao.FindStudentById(id);
System.out.println("---IDfind-----");
}else if(findway.equals("name")) {
String name=type;
System.out.println("findname="+name);
find = dao.FindStudentByName(name);
System.out.println("---NAMEfind-----");
}else if(findway.equals("place")) {
String place=type;
System.out.println("findplace="+place);
find = dao.FindStudentByPlace(place);
System.out.println("---Placefind-----");
}
request.setAttribute("find",find);
request.getRequestDispatcher("find.jsp").forward(request, response);
}
}
package com.Servlet;
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.JavaBean.Student;
import com.dao.StudentDao;
import java.io.IOException;
@WebServlet("/UpdateServlet")
@SuppressWarnings("serial")
public class UpdateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* int id = Integer.parseInt(request.getParameter("id")); StudentDao dao = new
* StudentDao(); Student student = dao.getStudentById(id);
* request.setAttribute("student", student);
* request.getRequestDispatcher("update.jsp").forward(request, response);
*/
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Student student = new Student();
int id = Integer.parseInt(request.getParameter("id"));
System.out.println("更新前得到的id为:******"+id);
String name = request.getParameter("name");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
String place = request.getParameter("place");
String tel = request.getParameter("tel");
System.out.println("更新后得到的age+sex+place+tel为:**************"+age+sex+place+tel);
student.setId(id);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setPlace(place);
student.setTel(tel);
StudentDao dao = new StudentDao();
dao.updateStudent(student);
System.out.println("dao.updateStudent运行完毕");
request.getRequestDispatcher("ShowServlet").forward(request, response);
}
}
package com.Servlet;
import java.io.IOException;
import java.sql.SQLException;
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.JavaBean.Student;
import com.dao.StudentDao;
@WebServlet("/AddServlet")
@SuppressWarnings("serial")
public class AddServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
String place = request.getParameter("place");
String tel = request.getParameter("tel");
Student student = new Student();
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setPlace(place);
student.setTel(tel);
StudentDao dao = new StudentDao();
boolean flag;
flag=dao.addStudent(student);
if(flag) {request.getRequestDispatcher("ShowServlet").forward(request,response);
System.out.println("添加成功");
}else {
System.out.println("添加失败");
//request.getRequestDispatcher("add.jsp").forward(request,response);
}
}
}
package com.Filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@WebFilter(urlPatterns=("*.jsp"),initParams= {
@WebInitParam(name="loginPage",value="login.jsp"),
@WebInitParam(name="loginServlet",value="LoginServlet")
})
public class LoginFilter implements Filter {
private FilterConfig config;
public LoginFilter() {
// TODO Auto-generated constructor stub
}
public void destroy() {
// TODO Auto-generated method stub
this.config=null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
String loginPage=config.getInitParameter("loginPage");
String loginServlet=config.getInitParameter("loginServlet");
//获取会话对象
HttpSession session =((HttpServletRequest)request).getSession();
//获取资源路径
String requestPath=((HttpServletRequest)request).getServletPath();
if(session.getAttribute("username")!= null&&session.getAttribute("password")!= null
||requestPath.endsWith(loginPage)
||requestPath.endsWith(loginServlet)) {
chain.doFilter(request, response);
}else {
request.setAttribute("tip", "未登录");
request.getRequestDispatcher(loginPage).forward(request,response);
}
}
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
this.config=config;
}
}
1. 首先独立完成一个项目收获比较大,与小组作业完全不一样。虽然这是一个非常简单的项目,但是过程也并不一帆风顺。在写的过程中激发了我对页面设计的兴趣,想要进一步学习bootstrap和css相关知识。
2. 另外,我希望自己能够养成记录的习惯,每一次修改好的东西都要保存起来。当时写这个同学录系统其实也有过好几个版本,可是现在都没法找到了,甚至也没法想起来当时是为什么要修改成现在这样。