一个Demo
Demo结构
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CUDR_Demo</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>com.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>com.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/logout.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentList</servlet-name>
<servlet-class>com.servlet.StudentListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentList</servlet-name>
<url-pattern>/student/list.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentSave</servlet-name>
<servlet-class>com.servlet.StudentSaveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentSave</servlet-name>
<url-pattern>/student/save.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentEdit</servlet-name>
<servlet-class>com.servlet.StudentEditServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentEdit</servlet-name>
<url-pattern>/student/edit.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentUpdate</servlet-name>
<servlet-class>com.servlet.StudentUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentUpdate</servlet-name>
<url-pattern>/student/update.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentDelete</servlet-name>
<servlet-class>com.servlet.StudentDeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentDelete</servlet-name>
<url-pattern>/student/delete.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentDeletes</servlet-name>
<servlet-class>com.servlet.StudentDeletesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentDeletes</servlet-name>
<url-pattern>/student/deletes.do</url-pattern>
</servlet-mapping>
</web-app>
数据库部分
数据库表
工具类部分
数据库工具类
DBUtil.java
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private DBUtil(){}
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root"; //数据库名
private static final String PASSWORD = "root"; //数据库密码
//取得连接
public static Connection getConn() throws SQLException{
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
return conn;
}
//关闭资源
public static void myClose(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException{
//关闭的顺序为按照 创建的顺序 逆序关闭
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}
}
UUID工具类,保证ID唯一性
UUIDUtil.java
package com.util;
import java.util.UUID;
public class UUIDUtil {
public static String getUUID(){
return UUID.randomUUID().toString().replaceAll("-","");
}
}
字段类部分
Student.java
package com.domain;
public class Student {
private String id;
private String name;
private int age;
public Student(){}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
登录部分
处理登录的后端Servlet
LoginServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到登录操作");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select count(*) from tbl_user where username=? and password=?";
boolean flag = true;
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
/*
* 创建完ps之后,养成好习惯,观察sql语句,如果有?,需要使用ps对?进行赋值
*/
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next()){
int count = rs.getInt(1);
/*
* count==0 没有查询出相应的账号密码 登录失败
* count==1 查询出来了一条记录 登录成功
* count>1 有垃圾数据 登录失败
*
*/
if(count!=1){
flag = false;
}
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上为连jdbc,判断账号密码是否正确
* 以下为根据flag值,决定登录成功还是失败
*/
//登录成功
if(flag){
request.getSession().setAttribute("username",username);
response.sendRedirect(request.getContextPath() + "/student/list.do");
//登录失败
}else{
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3 align="center">我是登录页</h3>
<div align="center">
<form action="login.do" method="post">
账号:<input type="text" name="username"/><br/><br/>
密码:<input type="text" name="password"/><br/><br/>
<input type="submit" value="提交"/>
<a href="register.jsp">需要注册?</a>
</form>
</div>
</body>
</html>
注册部分
处理注册的后端Servlet
RegisterServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.Element;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class RegisterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到注册操作");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
String sql = "insert into tbl_user(username,password) values(?,?)";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
/*
* 创建完ps之后,养成好习惯,观察sql语句,如果有?,需要使用ps对?进行赋值
*/
ps.setString(1,username);
ps.setString(2,password);
count = ps.executeUpdate();
System.out.println("受到影响的行数为:" + count + "行");
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
//注册成功跳转到登录页
if(count >= 1){
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3 align="center">我是注册页</h3>
<div align="center">
<form action="register.do" method="post">
请输入账号:<input type="text" name="username"/><br/><br/>
请输入密码:<input type="text" name="password"/><br/><br/>
<input type="submit" value="提交注册"/>
<a href="login.jsp">已注册?返回登录</a>
</form>
</div>
</body>
</html>
信息展示部分(登录进去后)
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
td{
text-align: center;
}
a{
text-decoration: none;
}
.ghbs1{
background-color:#FFFFCC;
}
.ghbs2{
background-color:#CCFF66;
}
</style>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
// window.location.href = "jsp/student/add.jsp";
$(function(){
$("#tBody>tr:even").addClass("ghbs1");
$("#tBody>tr:odd").addClass("ghbs2");
$("#addBtn").click(function(){
window.location.href = "jsp/student/add.jsp";
})
$("#editBtn").click(function(){
//取需要修改记录的id
//取得复选框的value值相当于id拿到了
var $xz = $("input[name='xz']:checked");
//使用jQuery对象的时候,可以当做dom对象来使用
//判断$xz选中了多少个dom元素
if($xz.length == 0) {
alert('请选择要修改的记录');
}else if($xz.length > 1) {
alert('只能选中一条记录进行修改');
}else{
//我们现在选中的唯一的这条记录的value拿到,就相当于id拿到了
//正常取值
var id = $xz.val();
window.location.href = "student/edit.do?id="+id
}
})
$("#deleteBtn").click(function(){
var $xz = $("input[name='xz']:checked");
if($xz.length == 0) {
alert("请选择要要删除的记录");
} else {
if(confirm("您确定要删除所选记录吗?")) {
var path = "student/deletes.do?";
for(var i = 0; i < $xz.length; i++) {
path += "id=" +$($xz[i]).val();
//如果不是最后一条,追加一个 &
if(i<$xz.length-1) {
path += "&";
}
}
window.location.href = path;
}
}
})
$("#qx").click(function(){
var $xz = $("input[name='xz']");
if($("#qx")[0].checked) {
for(var i = 0; i < $xz.length; i++) {
$xz[i].checked = true;
}
} else {
for(var i = 0; i < $xz.length; i++) {
$xz[i].checked = false;
}
}
})
})
</script>
</head>
<body>
你好:${username}
<hr/>
<h3 align="center">学生信息管理系统</h3>
<table border="0" align="center" width="70%" cellspacing="0" cellpadding="6">
<tr>
<td style="text-align: left;">
<button id="addBtn">添加学员</button>
<button id="editBtn">修改学员</button>
<button id="deleteBtn">删除学员</button>
<a href="logout.do"> 点击此处执行退出操作</a>
</td>
</tr>
</table>
<table border="1" align="center" width="70%" cellspacing="0" cellpadding="6">
<thead>
<tr>
<td><input type="checkbox" id="qx" /></td>
<td>序号</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tBody">
<c:if test="${empty sList}">
<tr>
<td colspan="6">没有相关记录</td>
</tr>
</c:if>
<c:if test="${!empty sList}">
<c:forEach items="${sList}" var="s" varStatus="vs">
<tr>
<td><input type="checkbox" name="xz" value="${s.id}"/></td>
<td>${vs.count}</td>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.age}</td>
<td><a href="student/edit.do?id=${s.id}">
修改
</a>
||
<a href="student/delete.do?id=${s.id}">
删除
</a></td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>
</body>
</html>
index.jsp效果(几个操作在下面几个部分列出)
处理信息展示的后端Servlet
StudentListServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.domain.Student;
import com.util.DBUtil;
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生信息列表操作");
response.setContentType("text/html;charset=utf-8");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select id,name,age from tbl_student";
List<Student> sList = new ArrayList<Student>();
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Student s = new Student();
s.setId(rs.getString(1));
s.setName(rs.getString(2));
s.setAge(rs.getInt(3));
sList.add(s);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上操作为取得了sList数据
*
* 以下为跳转到目标页面student/index.jsp
* 将sList数据传递到student/index.jsp
* 转发
*/
request.setAttribute("sList",sList);
request.getRequestDispatcher("/jsp/student/index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
信息添加部分
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submitBtn").click(function(){
//取得表单的jQuery对象 调用提交表单的方法
$("#myForm").submit();
})
})
</script>
</head>
<body>
<h3>我是学生信息添加页</h3>
<form action="student/save.do" method="post" id="myForm">
请输入姓名:<input type="text" name="name"/><br/><br/>
请输入年龄:<input type="text" name="age"/><br/><br/>
<!-- <input type="submit" value="提交"/> -->
<button type="button" id="submitBtn">提交表单</button>
</form>
</body>
</html>
处理信息添加的后端Servlet
StudentSaveServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
import com.util.UUIDUtil;
public class StudentSaveServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生添加操作");
//接收表单信息
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
int age = Integer.valueOf(ageStr);
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into tbl_student(id,name,age) values(?,?,?)";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1, UUIDUtil.getUUID());
ps.setString(2, name);
ps.setInt(3, age);
ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上为完成业务逻辑,执行了学生信息的添加操作
*
* 之后跳转到学生信息列表页 student/list.do
*
* 登录成功后,重定向到student/list.do
* 重定向:
* list.do
*
*/
response.sendRedirect(request.getContextPath() + "/student/list.do");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
信息修改部分
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submitBtn").click(function(){
//取得表单的jQuery对象 调用提交表单的方法
$("#myForm").submit();
})
})
</script>
</head>
<body>
<h3>我是学生信息修改页</h3>
<form action="student/update.do" method="post" id="myForm">
<input type="hidden" name="id" value="${s.id}"/>
请编辑姓名:<input type="text" name="name" value="${s.name}"/><br/><br/>
请编辑年龄:<input type="text" name="age" value="${s.age}"/><br/><br/>
<!-- <input type="submit" value="提交"/> -->
<button type="button" id="submitBtn">提交表单</button>
</form>
</body>
</html>
处理信息修改的后端Servlet
StudentUpdateServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
import com.util.UUIDUtil;
public class StudentUpdateServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生修改操作");
//接收表单信息
String id = request.getParameter("id");
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
int age = Integer.valueOf(ageStr);
Connection conn = null;
PreparedStatement ps = null;
String sql = "update tbl_student set name=?,age=? where id=?";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setInt(2,age);
ps.setString(3,id);
ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
*
* 登录成功后,修改后重定向到student/list.do
*
*
*/
response.sendRedirect(request.getContextPath() + "/student/list.do");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
信息删除部分
单点删除
StudentDeleteServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class StudentDeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生删除操作");
//接收需要删除记录的id
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from tbl_student where id=?";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1,id);
ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上为完成业务逻辑,执行了学生信息的删除操作
*
* 之后跳转到学生信息列表页 student/list.do
*
* 重定向:
* list.do
*
*/
response.sendRedirect(request.getContextPath() + "/student/list.do");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
批量删除
StudentDeletesServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class StudentDeletesServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生删除操作");
/*
*
* 事务机制:
* 成功时同时成功,失败时同时失败
* 将jdbc自动提交机制改为手动提交机制
* 失败时,jdbc回滚
*
* */
//接收需要删除记录的id数组
//String id = request.getParameter("id");
String ids[] = request.getParameterValues("id");
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from tbl_student where id=?";
//id=?&id=?&id=?
try{
conn = DBUtil.getConn();
//将jdbc的自动提交机制改为手动提交
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
for(String id:ids) {
ps.setString(1,id);
ps.executeUpdate();
}
//提交事务
conn.commit();
}catch(SQLException e){
try {
//回滚事务
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上为完成业务逻辑,执行了学生信息的删除操作
*
* 之后跳转到学生信息列表页 student/list.do
*
* 重定向:
* list.do
*
*/
response.sendRedirect(request.getContextPath() + "/student/list.do");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
退出操作
LogoutServlet.java
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 java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.DBUtil;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到退出操作");
//session 销毁
request.getSession().invalidate();
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
结束语
时人不识凌云木,直待凌云始道高。——杜荀鹤《小松》