Java Servlet抽取简单理解

目录

开发中的问题:

问题解决方案:


开发中的问题:

传统⽅式的开发⼀个请求对应⼀个Servlet:这样的话会导致⼀个模块的Servlet过多,导致整个项⽬的 Servlet都会很多. 能不能做⼀个处理?让⼀个模块⽤⼀个Servlet处理请求. 当前是商品模块, 就创建⼀个 商品Servlet

  • 传统⽅式

       查询所有的商品 :http://localhost:8080/web06/findAll

       添加商品: :http://localhost:8080/web06/add

        删除商品 :http://localhost:8080/web06/delete

  • 以模块为单位创建Servlet, 当前是创建ProductServle

       查询所有的商品 :http://localhost:8080/web06/productServlet?method=findAll

       添加商品            :http://localhost:8080/web06/productServlet?method=add

       删除商品            :http://localhost:8080/web06/productServlet?method=delete

问题解决方案:

进⾏相同模块的多个Servlet抽取, 实现代码量减少, 后期维护性增强

操作步骤:

1. 基本抽取

    解决了Servlet⽂件过多, 不便于查找与维护的烦恼

      1. 把多个相同模块的Servlet⽂件, 抽取成⼀个个新的Servlet模块⽂件

      2. 原有Servlet功能作为新Servlet模块⽂件的⼀个个⽅法⽽存在

      3. 再通过请求参数 method 值不同, 请求调度具体的功能⽅法

2. 改造Servlet模块⽂件

    解决了新功能⽅法增加, 原调度功能⽅法的代码⽆需更改,即可调⽤新⽅法

      1. 通过反射技术, 实现通过反射来调度具体的功能⽅法

3. BaseServlet抽取

     解决了多个Servlet模块⽂件中 存在相同代码的弊病

       1. 通过继承, 把多个Servlet模块⽂件 相同代码抽取到 ⽗类 BaseServlet中

        2. BaseServlet中完成 统⼀调度 每个Servlet模块的功能⽅法

基本抽取
代码 : ProductServlet
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //获取提交的参数
         String method = request.getParameter("method");
         //判断参数,调⽤不同的⽅法
         if ("add".equals(method)) {
             add(request, response);
         } else if ("delete".equals(method)) {
             delete(request, response);
         } else if ("update".equals(method)) {
            update(request, response);
         } else if ("findAll".equals(method)) {
             findAll(request, response);
         } else if ("findById".equals(method)) {
             findById(request, response);
         } else if ("findByPage".equals(method)) {
             findByPage(request, response);
         }
     }
     //添加商品功能
     public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //删除商品功能
     public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //更新商品功能
     public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找所有商品功能
     public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找指定ID对应的商品功能
     public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //分⻚查找的商品功能
     public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
     }
}
代码 : OrderServlet
 
@WebServlet("/order")
public class OrderServlet extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //获取提交的参数
         String method = request.getParameter("method");
         //判断参数,调⽤不同的⽅法
         if ("add".equals(method)) {
             add(request, response);
         } else if ("delete".equals(method)) {
             delete(request, response);
         } else if ("update".equals(method)) {
             update(request, response);
         } else if ("findAll".equals(method)) {
             findAll(request, response);
         } else if ("findByPage".equals(method)) {
             findByPage(request, response);
         }
     }
     //添加订单功能
     public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //删除订单功能(指订单的状态发⽣改变,数据库中并未删除)
     public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //更新订单功能
     public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找所有订单功能
     public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //分⻚查找的订单功能
     public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
     }
}
基本抽取优化 ( 反射 )
代码 :ProductServlet
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         try {
             //获取提交的参数
             String md = request.getParameter("method");
             //反射调⽤⽅法
             Class clazz = this.getClass();
             //获取⽅法
             Method method = clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
             //执⾏⽅法
             method.invoke(this, request, response);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
     //添加商品功能
     public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //删除商品功能
     public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //更新商品功能
     public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找所有商品功能
     public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找指定ID对应的商品功能
     public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //分⻚查找的商品功能
     public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
     }
}
代码 :OrderServlet
@WebServlet("/order")
public class OrderServlet extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         try {
             //获取提交的参数
             String md = request.getParameter("method");
             //反射,调⽤⽅法
             Class clazz = this.getClass();
             //获取⽅法
             Method method = clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
             //执⾏⽅法
             method.invoke(this, request, response);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
     //添加订单功能
     public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //删除订单功能(指订单的状态发⽣改变,数据库中并未删除)
     public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //更新订单功能
     public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找所有订单功能
     public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //分⻚查找的订单功能
     public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
     }
}
BaseServlet 抽取
 
代码 :BaseServlet
/**
* @description Servlet的基类
*/
public abstract class BaseServlet extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         try {
             //获取提交的参数
             String md = request.getParameter("method");
             //反射,调⽤⽅法
             Class clazz = this.getClass();
             //获取⽅法
             Method method = clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
             //执⾏⽅法
             method.invoke(this, request, response);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
     }
}
代码 : ProductServlet
/**
* @description 商品模块
*/
@WebServlet("/product")
public class ProductServlet extends BaseServlet {
     //添加商品功能
     public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //删除商品功能
     public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //更新商品功能
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找所有商品功能
     public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找指定ID对应的商品功能
     public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //分⻚查找的商品功能
     public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
}
代码 : OrderServlet
 
/**
* @description 订单模块
*/
@WebServlet("/order")
public class OrderServlet extends BaseServlet {
     //添加订单功能
     public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //删除订单功能(指订单的状态发⽣改变,数据库中并未删除)
     public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //更新订单功能
     public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //查找所有订单功能
     public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
     //分⻚查找的订单功能
     public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值