目录
开发中的问题:
传统⽅式的开发⼀个请求对应⼀个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模块的功能⽅法
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
/**
* @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);
}
}
/**
* @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 {
}
}
/**
* @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 {
}
}