目录
M:model
V:view
C:controller
分工明确,各司其职
降低代码耦合度
写一个Demo01.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h3>目前增删改查的方法</h3>
- <a href="${pageContext.request.contextPath }/book/add">增加</a>
- <a href="${pageContext.request.contextPath }/book/del">删除</a>
- <a href="${pageContext.request.contextPath }/book/edit">修改</a>
- <a href="${pageContext.request.contextPath }/book/list">查询</a>
- <!--
- 上述问题:
- 1.关于单个实体/表操作场景越多,需要新建的类越多,造成了项目中类的数量过于庞大
- 2.当新增了业务,除了要添加该业务对应的方法(load),同时还要改动原有的代码
- 3.反射相关代码,在每一个实体类对应的servlet中都存在
- 4.每一个servlet中都有doget、dopost方法
- -->
- </body>
- </html>
再写四个servlet在com.cdl.web包下
AddBookServlet
- package com.cdl.web;
-
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- * Servlet implementation class AddBookServlet
- */
- @WebServlet("/book/add")
- public class AddBookServlet extends HttpServlet {
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
-
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("处理书籍的增加业务,调用BookBiz");
- }
-
- }
DelBookServlet
- package com.cdl.web;
-
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
-
- @WebServlet("/book/del")
- public class DelBookServlet extends HttpServlet {
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
-
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("处理书籍的删除业务,调用BookBiz");
- }
-
- }
EditBookServlet
- package com.cdl.web;
-
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- * Servlet implementation class EditBookServlet
- */
- @WebServlet("/book/edit")
- public class EditBookServlet extends HttpServlet {
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
-
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("处理书籍的编辑业务,调用BookBiz");
- }
-
- }
ListBookServlet
- package com.cdl.web;
-
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- * Servlet implementation class EditBookServlet
- */
- @WebServlet("/book/list")
- public class ListBookServlet extends HttpServlet {
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
-
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("处理书籍的查询业务,调用BookBiz");
- }
-
- }

当点击增加控制台输出
![]()
点其他时

建一个BookServlet
- package com.cdl.web;
-
- import java.io.IOException;
- import java.lang.reflect.Method;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
-
- @WebServlet("/book.action")
- public class BookServlet extends HttpServlet {
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
-
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
- String methodName = request.getParameter("methodName");
-
-
- if("add".equals(methodName)){
- 如果前台传递到后台的是一个新增的请求,那么后台就调用新增方法
- add(req,resp);
- }
- else if("del".equals(methodName)) {
- del(req,resp);
- }
- else if("edit".equals(methodName)) {
- edit(req,resp);
- }
- else if("list".equals(methodName)) {
- list(req,resp);
- }
- else if("load".equals(methodName)) {
- load(req,resp);
- }
- }
-
- private void list(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 list 方法");
- }
-
- private void load(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 list 方法");
- }
-
- private void edit(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 edit 方法");
- }
-
- private void del(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 del 方法");
- }
-
- private void add(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 add 方法");
- }
-
-
- }
-
-
Demo01.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h3>目前增删改查的方法</h3>
- <a href="${pageContext.request.contextPath }/book/add">增加</a>
- <a href="${pageContext.request.contextPath }/book/del">删除</a>
- <a href="${pageContext.request.contextPath }/book/edit">修改</a>
- <a href="${pageContext.request.contextPath }/book/list">查询</a>
- <!--
- 上述问题:
- 1.关于单个实体/表操作场景越多,需要新建的类越多,造成了项目中类的数量过于庞大
- 2.当新增了业务,除了要添加该业务对应的方法(load),同时还要改动原有的代码
- 3.反射相关代码,在每一个实体类对应的servlet中都存在
- 4.每一个servlet中都有doget、dopost方法
- -->
- <h3>类数量过多问题的优化</h3>
- <a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
-
- </body>
- </html>
结果

点击修改和删除 控制台:

改变三中的BookServlet
- package com.cdl.web;
-
- import java.io.IOException;
- import java.lang.reflect.Method;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
-
- @WebServlet("/book.action")
- public class BookServlet extends HttpServlet {
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
-
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
- String methodName = request.getParameter("methodName");
- // methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
- // 前台传递什么方法,就调用当前类的对应方法
- try {
- Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
- m.setAccessible(true);
- // 调用当前类实例的methodName方法
- m.invoke(this, request,response);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
-
- // if("add".equals(methodName)){
- // 如果前台传递到后台的是一个新增的请求,那么后台就调用新增方法
- // add(req,resp);
- // }
- // else if("del".equals(methodName)) {
- // del(req,resp);
- // }
- // else if("edit".equals(methodName)) {
- // edit(req,resp);
- // }
- // else if("list".equals(methodName)) {
- // list(req,resp);
- // }
- // else if("load".equals(methodName)) {
- // load(req,resp);
- // }
- }
-
- private void list(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 list 方法");
- }
-
- private void load(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 list 方法");
- }
-
- private void edit(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 edit 方法");
- }
-
- private void del(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 del 方法");
- }
-
- private void add(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用 add 方法");
- }
-
-
- }
-
-
和三一致效果

在com.cdl.web中建一个OrderServlet
- package com.cdl.web;
-
- import java.io.IOException;
- import java.lang.reflect.Method;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
-
- @WebServlet("/order.action")
- public class OrderServlet extends HttpServlet {
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
-
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
- String methodName = request.getParameter("methodName");
- // methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
- // 前台传递什么方法,就调用当前类的对应方法
- try {
- Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
- m.setAccessible(true);
- // 调用当前类实例的methodName方法
- m.invoke(this, request,response);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
-
- // if("add".equals(methodName)){
- // 如果前台传递到后台的是一个新增的请求,那么后台就调用新增方法
- // add(req,resp);
- // }
- // else if("del".equals(methodName)) {
- // del(req,resp);
- // }
- // else if("edit".equals(methodName)) {
- // edit(req,resp);
- // }
- // else if("list".equals(methodName)) {
- // list(req,resp);
- // }
- // else if("load".equals(methodName)) {
- // load(req,resp);
- // }
- }
-
- private void list(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个order中调用 list 方法");
- }
-
- private void load(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个order中调用 list 方法");
- }
-
- private void edit(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个order中调用 edit 方法");
- }
-
- private void del(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个order中调用 del 方法");
- }
-
- private void add(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个order中调用 add 方法");
- }
-
-
- }
Demo02.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h3>目前增删改查的方法</h3>
- <a href="${pageContext.request.contextPath }/book/add">增加</a>
- <a href="${pageContext.request.contextPath }/book/del">删除</a>
- <a href="${pageContext.request.contextPath }/book/edit">修改</a>
- <a href="${pageContext.request.contextPath }/book/list">查询</a>
- <!--
- 上述问题:
- 1.关于单个实体/表操作场景越多,需要新建的类越多,造成了项目中类的数量过于庞大
- 2.当新增了业务,除了要添加该业务对应的方法(load),同时还要改动原有的代码
- 3.反射相关代码,在每一个实体类对应的servlet中都存在
- 4.每一个servlet中都有doget、dopost方法
- -->
- <h3>类数量过多问题的优化</h3>
- <a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
-
- <h3>订单类CRUD</h3>
- <a href="${pageContext.request.contextPath }/order.action?methodName=add">增加</a>
- <a href="${pageContext.request.contextPath }/order.action?methodName=del">删除</a>
- <a href="${pageContext.request.contextPath }/order.action?methodName=edit">修改</a>
- <a href="${pageContext.request.contextPath }/order.action?methodName=list">查询</a>
- <a href="${pageContext.request.contextPath }/order.action?methodName=load">回显</a>
- </body>
- </html>
运行代码:

建一个com.cdl.framework的包
建一个DispatcherServlet
- package com.cdl.framework;
-
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.cdl.web.BookAction;
-
- /**
- * 中央控制器
- * 主要职能:接收浏览器请求,找到对应的处理人
- * @author 陈冬丽
- *
- */
- public class DispatcherServlet extends HttpServlet{
- private Map<String, Action> actions = new HashMap<String, Action>();
- // 程序启动时,只会加载一次
-
- @Override
- public void init() throws ServletException {
- actions.put("/book", new BookAction());
- // actions.put("/order", new BooKAction());
- }
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doPost(req, resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //http://localhost:8080/mvc/book.aciton?methodName=list
- String uri = req.getRequestURI();
- //要拿到/book,就是最后一个/到最后一个点的位置
- uri=uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
- Action action = actions.get(uri);
- action.execute(req, resp);
- }
-
- }
ActionSupport
- package com.cdl.framework;
-
- import java.lang.reflect.Method;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class ActionSupport implements Action {
-
- @Override
- public void execute(HttpServletRequest req, HttpServletResponse resp) {
- String methodName = req.getParameter("methodName");
- // methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
- // 前台传递什么方法,就调用当前类的对应方法
- try {
- Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
- //打开访问权限
- m.setAccessible(true);
- // 调用当前类实例的methodName方法
- m.invoke(this, req,resp);
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
-
- private void list(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用list方法");
- }
-
- private void load(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用list方法");
- }
-
- private void edit(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用edit方法");
- }
-
- private void del(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用del方法");
- }
-
- private void add(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在用一个servlet中调用add方法");
- }
-
-
- }
Action
- package com.cdl.framework;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- * 子控制器
- * 对应请求的处理人
- * @author 陈冬丽
- *
- */
- public interface Action {
- void execute(HttpServletRequest req,HttpServletResponse resp);
- }