• JSP | 基于Servlet和JSP改造oa项目


    目录

    一: 基于Servlet和JSP改造oa项目

    第一步:将原型中的.html文件,全部修改为.jsp文件

    第二步:连接数据库,完成对应的操作

    (1)index.jsp 跳转到 list.jsp

    (2)list.jsp 跳转到detail.jsp

    (3)list.jsp 跳转到 删除操作

    (4)list.jsp 跳转到 add.jsp

    (5)list.jsp 跳转到 modify.jsp


    一: 基于Servlet和JSP改造oa项目

    使用Servlet处理业务,收集数据; 使用JSP展示数据!

    第一步:将原型中的.html文件,全部修改为.jsp文件

    (1)将之前原型模板中的".html"文件,全部修改为".jsp",然后在所有的".jsp"文件头部添加page指令(指定contentType防止中文乱码),将所有的JSP直接拷贝到web目录下。

    补充:ctrl+shift+del清除浏览器的缓存!

    <%@page contentType="text/html;charset=UTF-8"%>

    (2)完成所有页面的正常流转。(页面仍然能够正常的跳转,修改超链接的请求路径)

    ①这里就需要把所有的超连接路径更改,改成.jsp文件的格式;并加上项目名!

    ②这里就能体现出.jsp的优势,在jsp中是可以写java代码的;对于前端中的项目名,就可以通过java代码获取,不需要写死了!

    ③例如:对于欢迎页面index.jsp

    1. <%=request.getContextPath()%> 在JSP中动态的获取应用的根路径

    (3)目前所有原型页面如下:都是静态的页面,都是直接跳转到jsp展示数据,中间没有经过servlet获取数据!

    ①index.jsp 可以跳转到 list.jsp

    1. <%@page contentType="text/html;charset=UTF-8"%>
    2. html>
    3. <html>
    4. <head>
    5. <meta charset="utf-8">
    6. <title>欢迎使用OA系统title>
    7. head>
    8. <body>
    9. <a href="<%=request.getContextPath()%>/list.jsp">查看部门列表a>
    10. <hr>
    11. body>
    12. html>

    ②以list.jsp为展开对象,可以跳转到:detail.jsp、modify.jsp、add.jsp

    1. <%@page contentType="text/html;charset=UTF-8"%>
    2. html>
    3. <html>
    4. <head>
    5. <meta charset="utf-8">
    6. <title>部门列表页面title>
    7. head>
    8. <body>
    9. <script type="text/javascript">
    10. function del(dno){
    11. // 弹出确认框,用户点击确定,返回true,点击取消返回false
    12. var ok = window.confirm("亲,删了不可恢复哦!");
    13. if(ok){
    14. document.location.href = "<%=request.getContextPath()%>/dept/delete?deptno=" + dno;
    15. }
    16. }
    17. script>
    18. <h1 align="center">部门列表h1>
    19. <hr >
    20. <table border="1px" align="center" width="50%">
    21. <tr>
    22. <th>序号th>
    23. <th>部门编号th>
    24. <th>部门名称th>
    25. <th>操作th>
    26. tr>
    27. <tr>
    28. <td>1td>
    29. <td>10td>
    30. <td>销售部td>
    31. <td>
    32. <a href="javascript:void(0)" onclick="del(10)">删除a>
    33. <a href="<%=request.getContextPath()%>/modify.jsp">修改a>
    34. <a href="<%=request.getContextPath()%>/detail.jsp">详情a>
    35. td>
    36. tr>
    37. table>
    38. <hr >
    39. <a href="<%=request.getContextPath()%>/add.jsp">新增部门a>
    40. body>
    41. html>

    ③detail.jsp展示完以后在跳转到list.jsp

    1. <%@page contentType="text/html;charset=UTF-8" %>
    2. html>
    3. <html>
    4. <head>
    5. <meta charset="utf-8">
    6. <title>部门详情title>
    7. head>
    8. <body>
    9. <h1>部门详情h1>
    10. <hr >
    11. 部门编号:20 <br>
    12. 部门名称:销售部<br>
    13. 部门位置:北京<br>
    14. <input type="button" value="后退" onclick="window.history.back()"/>
    15. body>
    16. html>

    ④modify.jsp展示完以后在跳转到list.jsp

    1. <%@page contentType="text/html;charset=UTF-8"%>
    2. html>
    3. <html>
    4. <head>
    5. <meta charset="utf-8">
    6. <title>修改部门title>
    7. head>
    8. <body>
    9. <h1>修改部门h1>
    10. <hr >
    11. <form action="<%=request.getContextPath()%>/list.jsp" method="get">
    12. 部门编号<input type="text" name="deptno" value="20" readonly /><br>
    13. 部门名称<input type="text" name="dname" value="销售部"/><br>
    14. 部门位置<input type="text" name="loc" value="北京"/><br>
    15. <input type="submit" value="修改"/><br>
    16. form>
    17. body>
    18. html>

    ⑤add.jsp展示完以后在跳转到list.jsp

    1. <%@page contentType="text/html;charset=UTF-8"%>
    2. html>
    3. <html>
    4. <head>
    5. <meta charset="utf-8">
    6. <title>新增部门title>
    7. head>
    8. <body>
    9. <h1>新增部门h1>
    10. <hr >
    11. <form action="<%=request.getContextPath()%>/list.jsp" method="get">
    12. 部门编号<input type="text" name="deptno"/><br>
    13. 部门名称<input type="text" name="dname"/><br>
    14. 部门位置<input type="text" name="loc"/><br>
    15. <input type="submit" value="新增"/><br>
    16. form>
    17. body>
    18. html>

    第二步:连接数据库,完成对应的操作

    前面我们已经完成了jsp做数据展示的操作,但是并没有进行连接数据库;所以要先跳转到servlet完成数据的收集操作,然后通过servlet再跳到jsp做数据展示的操作!

    (1)index.jsp 跳转到 list.jsp

    (1)先对index.jsp模板原型进行修改;把超链接路径修改为跳转到一个servlet

    1. <a href="<%=request.getContextPath()%>/list.jsp">查看部门列表a>
    2. <a href="<%=request.getContextPath()%>/dept/list">查看部门列表a>

    (2)通过注解式开发,连接数据库,取出数据!(要有封装对象的意识---面向抽象编程

    此时取出的数据实际上是很零散的,所以不妨封装一个Dept对象,把取出来的数据封装到这个对象里面。同时在创建一个ArrayList集合depts,把所有的对象在放到这个集合里。然后在通过请求域调用setAttribute方法,把这个集合放到请求域当中。最后在调用getRequestDispatcher方法,通过转发的方式调转到另一个jsp进行数据的展示

    ①封装的Dept对象用来存放取出来的零散数据

    1. package com.bjpowernode.oa.bean;
    2. import java.util.Objects;
    3. /**
    4. * 普通的java类,张个java类可以封装零散的数据
    5. * @Author:朗朗乾坤
    6. * @Package:com.bjpowernode.oa.bean
    7. * @Project:JavaWeb
    8. * @name:Dept
    9. * @Date:2022/11/28 10:59
    10. */
    11. public class Dept {
    12. private String deptno;
    13. private String dname;
    14. private String loc;
    15. // 构造方法
    16. public Dept() {
    17. }
    18. public Dept(String deptno, String dname, String loc) {
    19. this.deptno = deptno;
    20. this.dname = dname;
    21. this.loc = loc;
    22. }
    23. // setter and getter
    24. public String getDeptno() {
    25. return deptno;
    26. }
    27. public void setDeptno(String deptno) {
    28. this.deptno = deptno;
    29. }
    30. public String getDname() {
    31. return dname;
    32. }
    33. public void setDname(String dname) {
    34. this.dname = dname;
    35. }
    36. public String getLoc() {
    37. return loc;
    38. }
    39. public void setLoc(String loc) {
    40. this.loc = loc;
    41. }
    42. // 重写toString方法
    43. @Override
    44. public String toString() {
    45. return "Dept{" +
    46. "deptno='" + deptno + '\'' +
    47. ", dname='" + dname + '\'' +
    48. ", loc='" + loc + '\'' +
    49. '}';
    50. }
    51. // 重写equals方法 和 hashCode方法
    52. @Override
    53. public boolean equals(Object o) {
    54. if (this == o) return true;
    55. if (o == null || getClass() != o.getClass()) return false;
    56. Dept dept = (Dept) o;
    57. return Objects.equals(deptno, dept.deptno) &&
    58. Objects.equals(dname, dept.dname) &&
    59. Objects.equals(loc, dept.loc);
    60. }
    61. @Override
    62. public int hashCode() {
    63. return Objects.hash(deptno, dname, loc);
    64. }
    65. }

    ②跳转到的servlet进行数据的收集

    1. package com.bjpowernode.oa.web.action;
    2. import com.bjpowernode.oa.bean.Dept;
    3. import com.bjpowernode.oa.utils.DBUtil;
    4. import javax.servlet.ServletException;
    5. import javax.servlet.annotation.WebServlet;
    6. import javax.servlet.http.HttpServlet;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. import java.sql.Connection;
    11. import java.sql.PreparedStatement;
    12. import java.sql.ResultSet;
    13. import java.sql.SQLException;
    14. import java.util.ArrayList;
    15. /**
    16. * @Author:朗朗乾坤
    17. * @Package:com.bjpowernode.oa.web.action
    18. * @Project:JavaWeb
    19. * @name:DeptServlet
    20. * @Date:2022/11/28 10:39
    21. */
    22. @WebServlet({"/dept/list"})
    23. public class DeptServlet extends HttpServlet {
    24. @Override
    25. protected void service(HttpServletRequest request, HttpServletResponse response)
    26. throws ServletException, IOException {
    27. // 获取servlet path
    28. String servletPath = request.getServletPath();
    29. if ("/dept/list".equals(servletPath)){
    30. doList(request,response);
    31. }
    32. }
    33. /**
    34. *1、 连接数据库,查询所有的部门信息,然后跳转到jsp做页面展示
    35. * @param request
    36. * @param response
    37. * @throws ServletException
    38. * @throws IOException
    39. */
    40. private void doList(HttpServletRequest request, HttpServletResponse response)
    41. throws ServletException, IOException{
    42. // 再创建一个容器,用来存储部门
    43. ArrayList depts = new ArrayList<>();
    44. Connection conn = null;
    45. PreparedStatement ps = null;
    46. ResultSet rs = null;
    47. try {
    48. // 连接数据库
    49. conn = DBUtil.getCoonetion();
    50. // 获取预编译的数据库操作对象
    51. String sql = "select deptno,dname,loc from dept";
    52. ps = conn.prepareStatement(sql);
    53. // 执行sql
    54. rs = ps.executeQuery();
    55. // 遍历查询结果集
    56. while (rs.next()) {
    57. // 取出数据
    58. String deptno = rs.getString("deptno");
    59. String dname = rs.getString("dname");
    60. String loc = rs.getString("loc");
    61. // 将零散的数据封装一个对象放进去
    62. Dept dept = new Dept();
    63. dept.setDeptno(deptno);
    64. dept.setDname(dname);
    65. dept.setLoc(loc);
    66. // 肯定不止一个对象,所以创建一个集合(容器),用来存储部门对象
    67. depts.add(dept);
    68. }
    69. } catch (SQLException e) {
    70. e.printStackTrace();
    71. }finally {
    72. DBUtil.close(conn,ps,rs);
    73. }
    74. // 将集合放到请求域当中
    75. request.setAttribute("deptList",depts);
    76. // 转发(不要重定向)
    77. request.getRequestDispatcher("/list.jsp").forward(request,response);
    78. }
    79. }

    ③通过转发的方式跳转到的另一个list.jsp,进行数据的展示

    重点:对于for循环打印,这里实际上是拆分开了,但是转化为java代码还是一个完整的for循环;第一次见到这种输出方式,可能会比较不适应!

    1. <%@ page import="com.bjpowernode.oa.bean.Dept" %>
    2. <%@ page import="java.util.List" %>
    3. <%@page contentType="text/html;charset=UTF-8"%>
    4. html>
    5. <html>
    6. <head>
    7. <meta charset="utf-8">
    8. <title>部门列表页面title>
    9. head>
    10. <body>
    11. <script type="text/javascript">
    12. function del(dno){
    13. // 弹出确认框,用户点击确定,返回true,点击取消返回false
    14. var ok = window.confirm("亲,删了不可恢复哦!");
    15. if(ok){
    16. document.location.href = "<%=request.getContextPath()%>/dept/delete?deptno=" + dno;
    17. }
    18. }
    19. script>
    20. <h1 align="center">部门列表h1>
    21. <hr >
    22. <table border="1px" align="center" width="50%">
    23. <tr>
    24. <th>序号th>
    25. <th>部门编号th>
    26. <th>部门名称th>
    27. <th>操作th>
    28. tr>
    29. <%
    30. // 从request域当中取出集合
    31. // getAttribute取出来的是Object类型,这里进行了强制类型转换
    32. List<Dept> deptList = (List<Dept>)request.getAttribute("deptList");
    33. // 循环遍历
    34. int i =0;
    35. for(Dept dept:deptList){
    36. %>
    37. <tr>
    38. <td><%=++i%>td>
    39. <td><%=dept.getDeptno()%>td>
    40. <td><%=dept.getLoc()%>td>
    41. <td>
    42. <a href="javascript:void(0)" onclick="del(10)">删除a>
    43. <a href="<%=request.getContextPath()%>/modify.jsp">修改a>
    44. <a href="<%=request.getContextPath()%>/detail.jsp">详情a>
    45. td>
    46. tr>
    47. <%
    48. }
    49. %>
    50. table>
    51. <hr >
    52. <a href="<%=request.getContextPath()%>/add.jsp">新增部门a>
    53. body>
    54. html>

    ④思考:如果只使用JSP这一个技术,能不能开发web应用?

    ①当然可以使用JSP来完成所有的功能。因为JSP就是Servlet,在JSP的<%%>里面写的代码就是在service方法当中的,所以在<%%>当中完全可以编写JDBC代码,连接数据库,查询数据,也可以在这个方法当中编写业务逻辑代码,处理业务都是可以的,所以使用单独的JSP开发web应用完全没问题。

    ②虽然JSP一个技术就可以完成web应用,但是不建议,还是建议采用servlet + jsp的方式进行开发;这样都能将各自的优点发挥出来。JSP就是做数据展示!Servlet就是做数据的收集!(JSP中编写的Java代码越少越好)一定要职责分明!

    ⑤思考:JSP文件的扩展名必须是xxx.jsp吗?

    ①jsp文件的扩展名是可以配置的,不是固定的。

    ②在CATALINA_HOME/conf/web.xml,在这个文件当中配置jsp文件的扩展名。

    ③xxx.jsp文件对于Tomcat来说,只是一个普通的文本文件,web容器会将xxx.jsp文件最终生成java程序,最终调用的是java对象相关的方法,真正执行的时候,和jsp文件没有关系。

    ④小窍门:如果JSP代码看不懂,建议把jsp翻译成java代码,就能看懂了!

    1. <servlet-mapping>
    2. <servlet-name>jspservlet-name>
    3. <url-pattern>*.jspurl-pattern>
    4. <url-pattern>*.jspxurl-pattern>
    5. servlet-mapping>

    (2)list.jsp 跳转到detail.jsp

    ①对list.jsp中的详情超链接路径进行更改;并把部门编号也传过去

    1. <a href="<%=request.getContextPath()%>/detail.jsp">详情a>
    2. <a href="<%=request.getContextPath()%>/dept/detail?deptno=<%=dept.getDeptno()%>">详情a>

    ②跳转到servlet,根据deptno进行数据的收集

    这里就和上面不同,上面我们是查询所有的数据,有很多组,需要先把数据放到对象里,再把对象放到集合里;而这里每次使根据depto编号进行查询,就一组数据,所以不需要集合!

    1. /**
    2. *2、 根据部门编号,获取部门的详细信息
    3. * @param request
    4. * @param response
    5. * @throws ServletException
    6. * @throws IOException
    7. */
    8. private void doDetail(HttpServletRequest request, HttpServletResponse response)
    9. throws ServletException, IOException{
    10. // 创建一个集合,用来存放数据
    11. Dept dept = new Dept();
    12. // 通过key获取value,获取部门标号
    13. String deptno = request.getParameter("deptno");
    14. // 根据部门编号获取部门信息,将部门信息封装成对象
    15. // 不需要集合,因为只有一个对象的数据
    16. Connection conn = null;
    17. PreparedStatement ps = null;
    18. ResultSet rs = null;
    19. try {
    20. // 获取连接
    21. conn = DBUtil.getCoonetion();
    22. // 获取预编译的数据库操作对象
    23. String sql = "select dname,loc from dept where deptno = ?";
    24. ps = conn.prepareStatement(sql);
    25. ps.setString(1,deptno);
    26. // 执行sql
    27. rs = ps.executeQuery();
    28. // 处理查询结果集,实际上就一条数据
    29. if (rs.next()){
    30. String dname = rs.getString("dname");
    31. String loc = rs.getString("loc");
    32. // 把数据封装成一个对象
    33. // Dept dept = new Dept(); 定义到这里,下面转发使用不了
    34. dept.setDeptno(deptno);
    35. dept.setDname(dname);
    36. dept.setLoc(loc);
    37. }
    38. } catch (SQLException e) {
    39. e.printStackTrace();
    40. }finally {
    41. DBUtil.close(conn,ps,rs);
    42. }
    43. // 将封装的对象放到请求域当中
    44. request.setAttribute("deptList",dept);
    45. // 转发(不要重定向)
    46. request.getRequestDispatcher("/detail.jsp").forward(request,response);
    47. }

    ③通过转发的方式跳转到的另一个detail.jsp,进行数据的展示

    1. <%@ page import="com.bjpowernode.oa.bean.Dept" %>
    2. <%@page contentType="text/html;charset=UTF-8" %>
    3. html>
    4. <html>
    5. <head>
    6. <meta charset="utf-8">
    7. <title>部门详情title>
    8. head>
    9. <body>
    10. <h1>部门详情h1>
    11. <hr >
    12. <%
    13. // 从request域当中取出数据,强制类型转换
    14. Dept dept = (Dept)request.getAttribute("deptList");
    15. %>
    16. 部门编号:<%=dept.getDeptno()%> <br>
    17. 部门名称:<%=dept.getDname()%><br>
    18. 部门位置:<%=dept.getLoc()%><br>
    19. <input type="button" value="后退" onclick="window.history.back()"/>
    20. body>
    21. html>

    (3)list.jsp 跳转到 删除操作

    ①对list.jsp中的删除超链接路径进行更改;这里是使用一个回调函数进行路径的跳

    1. <a href="javascript:void(0)" onclick="del(10)">删除a>
    2. // 修改为
    3. <a href="javascript:void(0)" onclick="del(<%=dept.getDeptno()%>)">删除a>
    4. // 回调函数如下,没有修改:
    5. <script type="text/javascript">
    6. function del(dno) {
    7. // 弹出确认框,用户点击确定,返回true,点击取消返回false
    8. var ok = window.confirm("亲,删了不可恢复哦!");
    9. if (ok) {
    10. document.location.href = "<%=request.getContextPath()%>/dept/delete?deptno=" + dno;
    11. }
    12. }
    13. script>

    ②跳转到servlet,根据deptno进行数据的收集;并且再次跳转到/dept/list页面进行数据的展示,这里使用的是重定向,因为不涉及从域中存取数据,所以尽可能使用重定向!

    1. /**
    2. *3、 根据部门编号,删除部门
    3. * @param request
    4. * @param response
    5. * @throws ServletException
    6. * @throws IOException
    7. */
    8. private void doDel(HttpServletRequest request, HttpServletResponse response)
    9. throws ServletException, IOException {
    10. // 获取部门编号
    11. String deptno = request.getParameter("deptno");
    12. // 连接数据库
    13. Connection conn = null;
    14. PreparedStatement ps = null;
    15. int count = 0;
    16. try {
    17. conn = DBUtil.getCoonetion();
    18. String sql = "delete from dept where deptno=?";
    19. ps = conn.prepareStatement(sql);
    20. ps.setString(1,deptno);
    21. count = ps.executeUpdate();
    22. } catch (SQLException e) {
    23. e.printStackTrace();
    24. }finally {
    25. DBUtil.close(conn,ps,null);
    26. }
    27. if (count==1) {
    28. // 删除成功,重定向到list页面;因为这里没有想域中放入数据,所以使用重定向
    29. // 重定向是要加项目名的,动态获取
    30. String contextPath = request.getContextPath();
    31. response.sendRedirect(contextPath+"/dept/list");
    32. }
    33. }

    (4)list.jsp 跳转到 add.jsp

    ①对list.jsp中的详新增超链接路径不要更改,因为需要一个提交的表单;从add.jsp这个表单中跳转到servlet

    注:这里是提交数据,所以应该使用post请求!

    1. <a href="<%=request.getContextPath()%>/add.jsp">新增部门a>
    2. <form action="<%=request.getContextPath()%>/list.jsp" method="get">
    3. <form action="<%=request.getContextPath()%>/dept/add" method="post">

    ②add.jsp

    1. <%@page contentType="text/html;charset=UTF-8"%>
    2. html>
    3. <html>
    4. <head>
    5. <meta charset="utf-8">
    6. <title>新增部门title>
    7. head>
    8. <body>
    9. <h1>新增部门h1>
    10. <hr >
    11. <%--<form action="<%=request.getContextPath()%>/list.jsp" method="get">--%>
    12. <form action="<%=request.getContextPath()%>/dept/add" method="post">
    13. 部门编号<input type="text" name="deptno"/><br>
    14. 部门名称<input type="text" name="dname"/><br>
    15. 部门位置<input type="text" name="loc"/><br>
    16. <input type="submit" value="新增"/><br>
    17. form>
    18. body>
    19. html>

    ③跳转到servlet,根据前端提交的数据,调用getParameter方法获取到后,进行数据的插入;然后再通过重定向/dept/list页面,进行数据的展示

    1. /**
    2. * 4、根据前端提交的数据,新增部门
    3. * @param request
    4. * @param response
    5. * @throws ServletException
    6. * @throws IOException
    7. */
    8. private void doAdd(HttpServletRequest request, HttpServletResponse response)
    9. throws ServletException, IOException {
    10. // 获取部门的信息
    11. // 注意乱码问题(Tomcat10不会出现这个问题)
    12. request.setCharacterEncoding("UTF-8");
    13. String deptno = request.getParameter("deptno");
    14. String dname = request.getParameter("dname");
    15. String loc = request.getParameter("loc");
    16. // 连接数据库执行insert语句
    17. Connection conn = null;
    18. PreparedStatement ps = null;
    19. int count = 0;
    20. try {
    21. conn = DBUtil.getCoonetion();
    22. String sql = "insert into dept(deptno, dname, loc) values(?,?,?)";
    23. ps = conn.prepareStatement(sql);
    24. ps.setString(1, deptno);
    25. ps.setString(2, dname);
    26. ps.setString(3, loc);
    27. count = ps.executeUpdate();
    28. } catch (SQLException e) {
    29. e.printStackTrace();
    30. } finally {
    31. DBUtil.close(conn, ps, null);
    32. }
    33. if (count == 1) {
    34. response.sendRedirect(request.getContextPath() + "/dept/list");
    35. }
    36. }

    (5)list.jsp 跳转到 modify.jsp

    思考:我们已经知道对于修改实际上有两次连接数据库的操作;第一次点击list页面的修改实际上就相当于查询;所以我们不妨就让 修改和详情 共享一个页面;然后在打一个标记;根据标记决定最终是跳转到detail.jsp还是modify.jsp

    ①修改list.jsp代码;两个走的是通过一个servlet(/dept/detail),有一个标记f

    1. <a href="<%=request.getContextPath()%>/dept/detail?f=modify&deptno=<%=dept.getDeptno()%>">修改a>
    2. <a href="<%=request.getContextPath()%>/dept/detail?f=detail&deptno=<%=dept.getDeptno()%>">详情a>

    ②修改/dept/detail代码,就需要获取标记f,根据标记f来决定跳转的页面

    1. // 方法1:获取这个标记,然后讨论
    2. String f = request.getParameter("f");
    3. if ("modify".equals(f)){
    4. request.getRequestDispatcher("/modify.jsp").forward(request,response);
    5. }else if ("detail".equals(f)){
    6. request.getRequestDispatcher("/detail.jsp").forward(request,response);
    7. }
    8. // 方法2:进行拼串
    9. String forward = "/"+request.getParameter("f")+".jsp";
    10. request.getRequestDispatcher(forward).forward(request,response);

    ③对modify.jsp进行修改,也要修改成post请求

    1. <%@ page import="com.bjpowernode.oa.bean.Dept" %>
    2. <%@page contentType="text/html;charset=UTF-8"%>
    3. html>
    4. <html>
    5. <head>
    6. <meta charset="utf-8">
    7. <title>修改部门title>
    8. head>
    9. <body>
    10. <h1>修改部门h1>
    11. <hr >
    12. <%
    13. Dept dept = (Dept)request.getAttribute("dept");
    14. %>
    15. <form action="<%=request.getContextPath()%>/dept/modify" method="post">
    16. 部门编号<input type="text" name="deptno" value="<%=dept.getDeptno()%>" readonly /><br>
    17. 部门名称<input type="text" name="dname" value="<%=dept.getDname()%>"/><br>
    18. 部门位置<input type="text" name="loc" value="<%=dept.getLoc()%>"/><br>
    19. <input type="submit" value="修改"/><br>
    20. form>
    21. body>
    22. html>

    ④跳转到/dept/modify,进行数据的更改

    1. /**
    2. * 5、根据部门名称,进行修改
    3. * @param request
    4. * @param response
    5. * @throws ServletException
    6. * @throws IOException
    7. */
    8. private void doModify(HttpServletRequest request, HttpServletResponse response)
    9. throws ServletException, IOException {
    10. // 解决请求体的中文乱码问题。
    11. request.setCharacterEncoding("UTF-8");
    12. // 获取表单中的数据
    13. String deptno = request.getParameter("deptno");
    14. String dname = request.getParameter("dname");
    15. String loc = request.getParameter("loc");
    16. // 连接数据库执行更新语句
    17. Connection conn = null;
    18. PreparedStatement ps = null;
    19. int count = 0;
    20. try {
    21. conn = DBUtil.getCoonetion();
    22. String sql = "update dept set dname = ?, loc = ? where deptno = ?";
    23. ps = conn.prepareStatement(sql);
    24. ps.setString(1, dname);
    25. ps.setString(2, loc);
    26. ps.setString(3, deptno);
    27. count = ps.executeUpdate();
    28. } catch (SQLException e) {
    29. e.printStackTrace();
    30. } finally {
    31. DBUtil.close(conn, ps, null);
    32. }
    33. if (count == 1) {
    34. // 更新成功
    35. response.sendRedirect(request.getContextPath() + "/dept/list");
    36. }
    37. }

  • 相关阅读:
    02-WPF_基础(二)
    python+excel自动生成小学加减乘除计算题
    为什么你总是无法做出正确的判断
    Postgresql源码(68)virtualxid锁的原理和应用场景
    插入排序——希尔排序
    放弃60万年薪考公!程序员完败公务员?
    【数据聚类】第八章第二节:谱聚类算法之切图聚类、算法流程及其实现
    《scala 编程(第3版)》学习笔记4
    食品行业仓储条码管理系统解决方案
    【Leetcode每日一题:882. 细分图中的可到达节点~~~单源最短路径Dijkstra算法】
  • 原文地址:https://blog.csdn.net/m0_61933976/article/details/128070145