Bruce_Yee

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  2 随笔 :: 0 文章 :: 0 评论 :: 206 阅读

关于本篇博客内容,我大概分成三个部分进行表述:对源代码的解读、二次开发程序以及自己在做完他人代码解读和重开发后的感想。

一、源代码解读

在本部分解读中主要分为三个部分:该软件功能的解读、实现该功能所使用的技术和我认为该软件所存在的缺陷之处。首先介绍源代码的来源,来自代码交流网站——CSDN,该处也放出网页地址:https://blog.csdn.net/weixin_59369584/article/details/123091992

1、功能解读

首先需要安装tomcat和MySQL等软件,我的配置为:apache-tomcat-10.0.13、mysql-5.5.36、IntelliJ IDEA 2019.3.3。

然后启动服务器运行于Web端。由上图可知,主要功能:实现部门的CRUD【增删改查】。

上图的4条部门信息我通过操作数据库添加的。

再进入各个功能中进行实际使用体验:

1)、增加部门

 部门编号为主键,不可重复,如果输入重复的编号将跳转操作失败页面

 

 

反之则增加一条记录

 

 

 

 

 

 

2)显示部门详细信息

点击详情

 

 

 

 其余部门详情就不展示了。

 

3)修改部门信息

点击修改

 

 可以将信息修改成如上图所示,注意:部门编号在修改页面虽然也展示,但是它只是“ReadOnly”只读不可修改的。

 

 这是修改以后的数据。

4)删除部门

点击删除部门就可以直接删除部门。

 

 

 上图就是点击“安保部2”的“删除”后的页面。

 

 

 

 

2、实现功能所使用的技术

复制代码
import com.bjpowernode.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.*;

public class DeptListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取根路径
        String contextPath = request.getContextPath();
        //输出到浏览器
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //静态页面
        out.print("<!DOCTYPE html>");
        out.print("<html>");
        out.print("<head>");
        out.print("<meta charset='utf-8'>");
        out.print("<title>部门列表页面</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1 align='center'>部门列表</h1>");
        out.print("<hr /> <!--横线-->");
        out.print("<table border='1px' align='center' width='50%'>");
        out.print("<tr>");
        out.print("<th>序号</th>");
        out.print("<th>部门编号</th>");
        out.print("<th>部门名称</th>");
        out.print("<th>操作</th>");
        out.print("</tr>");


        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            //注册驱动【静态代码块中】、获取连接
            conn = DBUtil.getConnection();
            //获取数据库操作对象
            String sql = "select deptno,dname,loc from dept";
            ps = conn.prepareStatement(sql);
            //执行SQL
            rs = ps.executeQuery();
            //处理结果集
            int i = 0;
            while(rs.next()){
                i++;
                String deptno = rs.getString("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                //动态页面
                out.print("<tr>");
                out.print("<td>"+i+"</td>");
                out.print("<td>"+deptno+"</td>");
                out.print("<td>"+dname+"</td>");
                out.print("<td>");
                out.print("<a href='"+contextPath+"/dept/delete?deptno="+deptno+"'>删除</a>");
                out.print("<a href='"+contextPath+"/dept/modify?deptno="+deptno+"'>修改</a>");
                out.print("<a href='"+contextPath+"/dept/detail?abc="+deptno+"'>详情</a>");
                out.print("</td>");
                out.print("</tr>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }

        //静态界面
        out.print("</table>");
        out.print("<hr >");
        out.print("<a href='"+contextPath+"/add.html'>新增部门</a>");
        out.print("</body>");
        out.print("</html>");
    }
}
复制代码

这是列表页面,这里使用数据库查询语句"select deptno,dname,loc from dept",在数据库里查询所有的数据,然后将数据输出到浏览器。每条数据后都添加“删除”、“修改”、“详情”的超链接。由于这是超链接是前端代码,需要在路径前面添加根路径,所以用“contextPath”动态获取根路径。当然,最后还有一个新增部门的超链接。

 

下面为add.html,新增部门页面,有一个表单提交,将表单里的数据提交到“/oa/dept/save”(在web.xml)里配置。

复制代码
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>新增部门</title>
    </head>
    <body>
        <h1>新增部门</h1>
        <hr >
        <form action="/oa/dept/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="保存" /><br>
        </form>
    </body>
</html>
复制代码

 

 

下面是增加部门信息的类

复制代码
import com.bjpowernode.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.ResultSet;
import java.sql.SQLException;

public class DeptSaveServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取部门信息
        //注意乱码问题
        request.setCharacterEncoding("UTF-8");
        String deptno = request.getParameter("deptno");
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");
        //连接数据库
        Connection conn =null;
        PreparedStatement ps = null;
        ResultSet rs = 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();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        if(count == 1){
            //增加成功
            request.getRequestDispatcher("/dept/list").forward(request,response);
        }
        else{
            request.getRequestDispatcher("/error.html").forward(request,response);
        }
    }
}
复制代码
复制代码
    
这是web.xml里增加部门的类和路径的配置
   <servlet> <servlet-name>save</servlet-name> <servlet-class>com.bjpowernode.oa.web.action.DeptSaveServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>save</servlet-name> <!--这里不带项目名--> <url-pattern>/dept/save</url-pattern> </servlet-mapping>
复制代码

以上利用向?传值,防止了sql注入问题,最后通过了转发机制完成了页面跳转。

 

下面是部门详情

复制代码
import com.bjpowernode.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 DeptDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //输出到浏览器
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.print("<!DOCTYPE html>");
        out.print("<html>");
        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 > ");

        //获取请求里的deptno
        String deptno = request.getParameter("abc");
        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        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("部门编号:"+deptno+" <br> ");
                out.print("部门名称:"+dname+"<br>");
                out.print("部门位置:"+loc+"<br>");



            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        out.print("<input type='button' value='后退' onclick='window.history.back()'/>");
        out.print("</body>");
        out.print("</html>");
    }
}
复制代码

也是对数据库进行操作,这里就不细提了,讲一讲onclick='window.history.back()'  ;这是JS代码,点击的话会回到上一个界面。

复制代码
这是xml配置
<!--
部门详情--> <servlet> <servlet-name>detail</servlet-name> <servlet-class>com.bjpowernode.oa.web.action.DeptDetailServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>detail</servlet-name> <!--这里不带项目名--> <url-pattern>/dept/detail</url-pattern> </servlet-mapping>
复制代码

 

 

下面是部门修改功能

复制代码
import com.bjpowernode.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 DeptModifyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取根路径
        String contextPath = request.getContextPath();
        //获取部门信息
        String deptno = request.getParameter("deptno");
        //设置输出方式
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.print("<!DOCTYPE html>");
        out.print("<html>");
        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+"/dept/realModify' method='post'>");



        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        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='修改' /><br>");
        out.print("        </form>");
        out.print("    </body>");
        out.print("</html>");
    }
}
复制代码

以上代码没有实际地对数据库进行修改,而是将数据库里的数据先展示出来。【后面表单提交后再进行修改】  修改界面里的部门编号用了“readonly”,防止用户修改编号。其余的都是基础和重复的,就不多重复地讲解了。

下面是真正修改数据库里面的数据的代码

复制代码
import com.bjpowernode.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.ResultSet;
import java.sql.SQLException;

public class DeptRealModifyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //防止乱码
        request.setCharacterEncoding("UTF-8");
        //获取部门信息
        String dname = request.getParameter("dname");
        String deptno = request.getParameter("deptno");
        String loc = request.getParameter("loc");
        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "update dept set dname=?,loc=? where deptno=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,dname);
            ps.setString(2,loc);
            ps.setString(3,deptno);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        if(count == 1){
            //修改成功
            //跳转页面
            request.getRequestDispatcher("/dept/list").forward(request,response);
        }

    }
}
复制代码

这里面的代码大多数都是重复的,也没必要再讲解了。

下面是web.xml里的配置

复制代码
 <!--修改部门页面-->
    <servlet>
        <servlet-name>modify</servlet-name>
        <servlet-class>com.bjpowernode.oa.web.action.DeptModifyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>modify</servlet-name>
        <!--这里不带项目名-->
        <url-pattern>/dept/modify</url-pattern>
    </servlet-mapping>


    <!--真正的修改部门操作-->
    <servlet>
        <servlet-name>realmodify</servlet-name>
        <servlet-class>com.bjpowernode.oa.web.action.DeptRealModifyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>realmodify</servlet-name>
        <!--这里不带项目名-->
        <url-pattern>/dept/realModify</url-pattern>
    </servlet-mapping>
复制代码

 

下面是删除部门功能

复制代码
import com.bjpowernode.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.ResultSet;
import java.sql.SQLException;

public class DeptDeleteServlet 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();
            //获取数据库操作对象
            String sql = "delete from dept where deptno=?";
            ps = conn.prepareStatement(sql);
            //给?传值
            ps.setString(1,deptno);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }

        //判断是删除失败了还是删除成功了
        if (count == 1){
            //删除成功了!
            //仍然跳转到部门列表页面
            //部门列表页面的显示需要执行另一个Servlet。怎么办?转发
            request.getRequestDispatcher("/dept/list").forward(request,response);
        }else{
            request.getRequestDispatcher("/error.html").forward(request,response);
        }
    }
}
复制代码

代码也是重复的,对数据库操作,转发机制跳转页面。

以下是xml配置

复制代码
    <!--删除部门-->
    <servlet>
        <servlet-name>delete</servlet-name>
        <servlet-class>com.bjpowernode.oa.web.action.DeptDeleteServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>delete</servlet-name>
        <!--这里不带项目名-->
        <url-pattern>/dept/delete</url-pattern>
    </servlet-mapping>
复制代码

 

 

3、我认为该软件所存在的缺陷之处

由以上代码讲解就可以很容易知道,有两个很大的毛病:那就是xml配置文件里写的东西太多了、类太多了。

就这么简单的CRUD功能使得xml写得满满当当,如果大项目的话,xml直接爆炸了。还有就是类太多了,看起来太冗余了。

当然,还有一个小毛病,那就是删除功能的时候,没有提醒用户,这样很容易在以后产生纠纷。像删除数据之类的操作,最好提示用户一下。

 

 

 

二、二次开发程序


对于以上提出的三个缺陷,我将利用三个技术来进行改进:利用注解代替xml文件的配置、利用模板设计模式将CRUD全部功能写在一个类中、使用JS技术提示用户是否删除数据。

 以下为我改进的代码

只有一个DeptServlet类

复制代码
import com.bjpowernode.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
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.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet({"/dept/list","/dept/detail","/dept/delete","/dept/save","/dept/modify",
        "/dept/realModify"})
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if ("/dept/list".equals(servletPath)){
            doList(request,response);
        }
        else if ("/dept/detail".equals(servletPath)){
            doDetail(request,response);
        }
        else if ("/dept/delete".equals(servletPath)){
            doDel(request,response);
        }
        else if ("/dept/save".equals(servletPath)){
            doSave(request,response);
        }
        else if ("/dept/modify".equals(servletPath)){
            doModify(request,response);
        }
        else if ("/dept/realModify".equals(servletPath)){
            doRealModify(request,response);
        }
    }

    private void doRealModify(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //防止乱码
        request.setCharacterEncoding("UTF-8");
        //获取部门信息
        String dname = request.getParameter("dname");
        String deptno = request.getParameter("deptno");
        String loc = request.getParameter("loc");
        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "update dept set dname=?,loc=? where deptno=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,dname);
            ps.setString(2,loc);
            ps.setString(3,deptno);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        if(count == 1){
            //修改成功
            //跳转页面
            request.getRequestDispatcher("/dept/list").forward(request,response);
        }
    }

    private void doModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取根路径
        String contextPath = request.getContextPath();
        //获取部门信息
        String deptno = request.getParameter("deptno");
        //设置输出方式
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.print("<!DOCTYPE html>");
        out.print("<html>");
        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+"/dept/realModify' method='post'>");



        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        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='修改' /><br>");
        out.print("        </form>");
        out.print("    </body>");
        out.print("</html>");
    }

    private void doSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取部门信息
        //注意乱码问题
        request.setCharacterEncoding("UTF-8");
        String deptno = request.getParameter("deptno");
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");
        //连接数据库
        Connection conn =null;
        PreparedStatement ps = null;
        ResultSet rs = 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();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        if(count == 1){
            //增加成功
            request.getRequestDispatcher("/dept/list").forward(request,response);
        }
        else{
            request.getRequestDispatcher("/error.html").forward(request,response);
        }
    }

    private void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //根据部门编号删除部门
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //获取部门编号
        String deptno = request.getParameter("deptno");
        //连接数据库删除部门
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtil.getConnection();
            //获取数据库操作对象
            String sql = "delete from dept where deptno=?";
            ps = conn.prepareStatement(sql);
            //给?传值
            ps.setString(1,deptno);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }

        //判断是删除失败了还是删除成功了
        if (count == 1){
            //删除成功了!
            //仍然跳转到部门列表页面
            //部门列表页面的显示需要执行另一个Servlet。怎么办?转发
            //request.getRequestDispatcher("/dept/list").forward(request,response);
            response.sendRedirect("/oa2/dept/list");
            out.print("<script type=\"text/javascript\">" +
                    "alert('删除成功!')" +
                    "</script>");
        }else{
            request.getRequestDispatcher("/error.html").forward(request,response);
        }
    }

    private void doDetail(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        //输出到浏览器
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.print("<!DOCTYPE html>");
        out.print("<html>");
        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 > ");

        //获取请求里的deptno
        String deptno = request.getParameter("abc");
        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        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("部门编号:"+deptno+" <br> ");
                out.print("部门名称:"+dname+"<br>");
                out.print("部门位置:"+loc+"<br>");



            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }
        out.print("<input type='button' value='后退' onclick='window.history.back()'/>");
        out.print("</body>");
        out.print("</html>");
    }

    private void doList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取根路径
        String contextPath = request.getContextPath();
        //输出到浏览器
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //静态页面
        out.print("<!DOCTYPE html>");
        out.print("<html>");
        out.print("<head>");
        out.print("<meta charset='utf-8'>");
        out.print("<title>部门列表页面</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<script type=\"text/javascript\">\n" +
                "\t\t\tfunction dele(dno){\n" +
                "\t\t\t\tif(confirm(\"亲,是否要删除?\")){\n" +
                "\t\t\t\t\tdocument.location.href = \"/oa2/dept/delete?deptno=\"+dno+\"\"\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}\n" +
                "\t\t</script>");
        out.print("<h1 align='center'>部门列表</h1>");
        out.print("<hr /> <!--横线-->");
        out.print("<table border='1px' align='center' width='50%'>");
        out.print("<tr>");
        out.print("<th>序号</th>");
        out.print("<th>部门编号</th>");
        out.print("<th>部门名称</th>");
        out.print("<th>操作</th>");
        out.print("</tr>");


        //连接数据库
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            //注册驱动【静态代码块中】、获取连接
            conn = DBUtil.getConnection();
            //获取数据库操作对象
            String sql = "select deptno,dname,loc from dept";
            ps = conn.prepareStatement(sql);
            //执行SQL
            rs = ps.executeQuery();
            //处理结果集
            int i = 0;
            while(rs.next()){
                i++;
                String deptno = rs.getString("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                //动态页面
                out.print("<tr>");
                out.print("<td>"+i+"</td>");
                out.print("<td>"+deptno+"</td>");
                out.print("<td>"+dname+"</td>");
                out.print("<td>");
                out.print("<a href='javascript:void(0)' onclick='dele("+ deptno +")'>删除</a>");
                out.print("<a href='"+contextPath+"/dept/modify?deptno="+deptno+"'>修改</a>");
                out.print("<a href='"+contextPath+"/dept/detail?abc="+deptno+"'>详情</a>");
                out.print("</td>");
                out.print("</tr>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,rs);
        }

        //静态界面
        out.print("</table>");
        out.print("<hr >");
        out.print("<a href='"+contextPath+"/add.html'>新增部门</a>");
        out.print("</body>");
        out.print("</html>");
    }


}
复制代码

在类上添加注解@WebServlet({"/dept/list","/dept/detail","/dept/delete","/dept/save","/dept/modify","/dept/realModify"})

只要路径为以上几个路径就可以访问到这个类,节省了web.xml文件。

在类中不使用doGet()和doPost(),而是使用service(HttpServletRequest request, HttpServletResponse response) 然后就通过equals方法比较路径来实现哪个具体的方法(模板设计方法),这样就可以只写一个类。

至于删除需要提醒的话,就比较简单了,只要会JS代码应该不成问题,以下就是该JS代码(我在类中已经把它嵌入)

复制代码
<script type="text/javascript">
        
            function dele(dno){
                if(confirm("亲,是否要删除?")){
                    document.location.href = "/oa/dept/delete?deptno="+dno+""
                    alert("删除成功!")
                }
            
            }
</script>


<a href="javascript:void(0) onclick="dele(1)">删除</a> 
复制代码

 最终功能其他一致,唯有删除功能不太一样,我只列举删除功能

 

 

 

 当然,这里我使用的是重定向,我发现这里面好像都是用的是转发机制,有时候会有一些小bug,所以尽量都使用重定向,这里就不一一修改了。

 

三、重开发后的感想

1、自从重开发后,我发现我学习的动力就更强烈了,当然,学无止境,不要局限于DOS窗口的小黑屏,虽然逻辑的确很重要,但是要学会学以致用。当然,后期我将持续学习Spring、SpringMVC、MyBatis三大框架,这样开发效率将大大提高。

2、我的前端技术还是有些欠缺,我也建议各位不要局限于后端开发,尽量做全栈工程师。Vue我以后也会找时间多学习。

3、前后端都慢慢掌握了,再去努力精通一些比如分布式、微服务、SpringBoot等学习。

最后,我希望我的感想能给一些迷茫的同学带来一点帮助,同时,您也可以通过下方留言和我探讨技术。

posted on   Bruce_Yee  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
点击右上角即可分享
微信分享提示