HttpServlet练习增删改及相关html页面

HttpServlet练习增删改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.xiaoguan.oa.index;

import com.xiaoguan.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ModifyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest requst, HttpServletResponse response) throws ServletException, IOException {
requst.setCharacterEncoding("UTF-8");
String deptno = requst.getParameter("deptno");
String dname = requst.getParameter("dname");
String loc = requst.getParameter("loc");
Connection conn=null;
PreparedStatement ps=null;
int count=0;
try {
conn= DBUtil.getConnection();
String sql="update dept set dname=?,loc=? where deptno=?";
ps=conn.prepareStatement(sql);
ps.setString(3,deptno);
ps.setString(1,dname);
ps.setString(2,loc);
count=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,null);
}
if(count==1){
response.sendRedirect(requst.getContextPath()+"/list");
}else{
response.sendRedirect(requst.getContextPath()+"/error.html");
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.xiaoguan.oa.index;

import com.xiaoguan.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SaveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest requst, HttpServletResponse response) throws ServletException, IOException {
requst.setCharacterEncoding("UTF-8");
String deptno = requst.getParameter("deptno");
String dname = requst.getParameter("dname");
String loc = requst.getParameter("loc");
Connection conn=null;
PreparedStatement ps=null;
int count=0;
try {
conn= DBUtil.getConnection();
String sql="insert into dept(deptno,dname,loc) values(?,?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1,deptno);
ps.setString(2,dname);
ps.setString(3,loc);
count=ps.executeUpdate();
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,null);
}
if(count==1){
response.sendRedirect(requst.getContextPath()+"/list");
}else{
response.sendRedirect(requst.getContextPath()+"/error.html");
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.xiaoguan.oa.index;

import com.xiaoguan.oa.utils.DBUtil;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.*;

public class DeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String deptno = request.getParameter("deptno");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int count = 0;
try {
conn = DBUtil.getConnection();
conn.setAutoCommit(false);
String sql = "delete from dept where deptno=?";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
count = ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
if (count == 1) {
response.sendRedirect(request.getContextPath()+"/list");
} else {
response.sendRedirect(request.getContextPath()+"/error.html");
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.xiaoguan.oa.index;

import com.xiaoguan.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EditServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String contextPath = this.getServletContext().getContextPath();
response.setContentType("text/html;charset=UTF-8");
String deptno = request.getParameter("deptno");
PrintWriter out=response.getWriter();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
out.print("<!DOCTYPE html>");
out.print("<html lang='ch'>");
out.print(" <head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>修改部门</title>");
out.print(" </head>");
out.print(" <body>");
out.print(" <h1>修改部门</h1>");
out.print(" <hr>");
out.print(" <form action='"+contextPath+"/modify' method='post'>");
try {
conn= DBUtil.getConnection();
String sql="select dname,loc from dept where deptno=?";
ps=conn.prepareStatement(sql);
ps.setString(1,deptno);
rs=ps.executeQuery();
if(rs.next()){
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print(" 部门编号<input type='text' name='deptno' value='"+deptno+"' readonly/><br>");
out.print(" 部门名称<input type='text' name='dname' value='"+dname+"'/><br>");
out.print(" 部门位置<input type='text' name='loc' value='"+loc+"'/><br>");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
out.print(" <input type='submit' value='修改'>");
out.print(" </form>");
out.print(" </body>");
out.print("</html>");
}
}

相关HTML页面

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>欢迎使用OA系统</title>
</head>
<body>
<a href="/test4/list">查看部门列表</a><br>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>删除失败</title>
</head>
<body>
<h1>操作失败<a href="javascript:void(0)" onclick="document.location.href='/oa/list'">返回部门列表</a></h1>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>修改部门</title>
</head>
<body>
<h1>修改部门</h1>
<hr>
<form action="list.html" method="post">
部门编号<input type="text" name="deptno" value="10" readonly/><br>
部门名称<input type="text" name="dname" value="销售部"/><br>
部门位置<input type="text" name="loc" value="北京"/><br>
<input type="submit" value="修改">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>新增部门</title>
</head>
<body>
<h1>新增部门</h1>
<hr>
<form action="/test4/save" method="post">
部门编号<input type="text" name="deptno"/><br>
部门名称<input type="text" name="dname"/><br>
部门位置<input type="text" name="loc"/><br>
<input type="submit" value="保存">
</form>
</body>
</html>

在更改HTML页面时浏览器可能会出现刷新也不更改的情况,这时需要清空浏览器的缓存,如清空谷歌浏览器中的缓存的图片和文件,光只清除cookie不行。