传统设计

分类管理需要:增加,删除,编辑,修改,查询5个服务端功能。

一个路径对应一个Servlet的思路,就需要设计5个Servlet类,并且在web.xml中配置5个路径。

CategoryAddServlet
CategoryDeleteServlet
CategoryEditServlet
CategoryUpdateServlet
CategoryListServlet
改进设计

每种实体类,对应了一个Servlet,而不是对应了5个,这样首先从Servlet数量上来讲,就大大的减少了。

原理流程图

那么是如何做到一个CategoryServlet类,就能完成本来需要5个Servlet类才能完成的功能的呢?

思路:(1)要有Servlet类的类名className,才能进入对应的Servlet类。

(2)要有方法名method,才能通过反射机制获得该Method对象,调用此方法。

关键在于访问路径的设计:

借助流程图来分析,为什么访问admin_category_list的时候,CategoryServlet的list()方法会被调用。

在web.xml配置文件中,让所有的请求【/*】都会经过BackServletFilter

<filter>
<filter-name>BackServletFilter</filter-name>
<filter-class>tmall.filter.BackServletFilter</filter-class>
</filter> <filter-mapping>
<filter-name>BackServletFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

*************************************************

1、过滤器BackServletFilter要完成什么事?

(1)获取访问路径的"/admin_category_list"字符串

String contextPath=request.getServletContext().getContextPath();

String uri = request.getRequestURI();

uri =StringUtils.remove(uri, contextPath);

(2)对以"/admin_"开头的路径,进行判断。

如果条件成立,实施步骤(3、4、5)。

不成立的,进行下一个路径的拦截。

       if(uri.startsWith("/admin_")){
//(3)
//(4)
//(5)
return;
}
chain.doFilter(request, response);

(3)获取访问路径中的category和list这两个字符串

String  servletName=StringUtils.substringBetween(uri,"_", "_");

String method=StringUtils.substringAfterLast(uri,"_" );

(4)确定要进入的Servlet类类名和要调用的方法名

String servletPath = servletName + "Servlet";
String method = StringUtils.substringAfterLast(uri,"_" );

(5)服务端跳转到指定的Servlet,同时在请求中传递一个参数

request.setAttribute("method", method);

req.getRequestDispatcher("/" + servletPath).forward(request, response);

2、指定的Servlet类(这里指CategoryServlet)要完成什么事?

(1)获取请求中的参数

String method = (String) request.getAttribute("method");

(2)根据参数创建Method对象m

 Method m = this.getClass().getMethod(method);

(3)调用m

m.invoke(this);

3、根据调用m后返回的结果,进行不同的处理。

String redirect = m.invoke(this);

(1)客户端跳转

if(redirect.startsWith("@"))

response.sendRedirect(redirect.substring(1));

(2)服务端跳转

else

request.getRequestDispathcher(redirect).forword(request,response);

(3)输出字符串

esle if(redirect.startsWith("%"))

response.getWriter().print(redirect.substring(1));

注意:

(1)服务端跳转的话,路径不变,跳转前后还是同一个请求,

可以以此获得请求中的参数或者session值。

(2)客户端跳转的话,路径改变,请求改变。

(3)有数值要传递的话优先服务端跳转。

在CategoryServlet类中,有5种方法可供调用,返回的结果各不相同。

1、add()方法,最后要跳转到/admin_category_list路径

return  ——>@admin_category_list

2、delete()方法,最后要跳转到/admin_category_list路径

return  ——>@admin_category_list

3、edit()方法,最后要跳转到edit.jsp页面

return   ——>admin/editCategory.jsp

4、update()方法,最后要跳转到/admin_category_list路径

return  ——>@admin_category_list

5、list()方法,最后要跳转到list.jsp页面

return   ——>admin/listCategory.jsp

注意:BaseBackServlet类重写了service()方法,

2和3所说的重复性代码被封装在这个方法里。

 

设计模式:Filter+Servlet+反射的更多相关文章

  1. web.xml中的主要元素说明(listener, filter, servlet)

    web.xml中加载的顺序为:context-param ---> listener ---> filter ---> servlet. listener:主要针对的是对象的操作,如 ...

  2. [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦

    [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...

  3. web.xml之context-param,listener,filter,servlet加载顺序及其周边

    先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如fil ...

  4. springboot 2.1.3.RELEASE添加filter,servlet源码学习

    Servlet规范中,通过ServeltContext来注册Filter.Servlet,这里分析Filter,Servlet是相同逻辑 springboot2.0中,我们通过 FilterRegis ...

  5. Spring MVC中Filter Servlet Interceptor 执行顺序

    <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springfr ...

  6. servlet反射、生命周期、接口

    什么是Servlet Servlet是JavaWeb的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet中通常需要: l  ...

  7. servlet/filter/listener/interceptor区别与联系

    转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...

  8. Servlet、Filter、Listener、Interceptor

    首先,JSP/Servlet规范中定义了Servlet.Filter.Listener这三种角色,并没有定义Interceptor这个角 色,Interceptor是某些MVC框架中的角色,比如Str ...

  9. servlet、filter、listener、interceptor之间的区别和联系

    一.概念 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层. 2.filter: ...

随机推荐

  1. 事件总线功能库,Reface.EventBus 详细使用教程

    Reface.AppStarter 中的事件总线功能是通过 Reface.EventBus 提供的. 参考文章 : Reface.AppStarter 框架初探 使用 Reface.EventBus ...

  2. Java Web:jstl处理字符串

    用法:${fn:methodName(args....)} 在使用这些函数之前必须在JSP中引入标准函数的声明<%@ taglib prefix="fn" uri=" ...

  3. 数学--数论--HDU 2674 沙雕题

    WhereIsHeroFrom: Zty, what are you doing ? Zty: I want to calculate N!.. WhereIsHeroFrom: So easy! H ...

  4. Ansible入门知识

    一.ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet,但是有一个不同和优点是我们不需要在节点中安装 ...

  5. N - Marriage Match II 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 推荐博客:https://www.cnblogs.com/liuxin13/p/4728131. ...

  6. request中跟路径有关的api的分析

    最近重在研究struts的源码,其中涉及到了request中的几个api,看了文档后还是觉得不清楚,所以在自己原来的工程中 测试了一下各个api的具体效果.在这里跟大家分享一下. 这是我具体测试的代码 ...

  7. 一次内核 crash 的排查记录

    一次内核 crash 的排查记录 使用的发行版本是 CentOS,内核版本是 3.10.0,在正常运行的情况下内核发生了崩溃,还好有 vmcore 生成. 准备排查环境 crash 内核调试信息rpm ...

  8. JDBC14 ORM03 JavaBean封装

    Javabean对象封装一条信息(推荐) 让JavaBean的属性名和类型尽量和数据库保持一致 一条记录对应一个对象,将这些查询到的对象放到容器中(List) 表信息如下 List封装多条信息 Con ...

  9. mysql 库表整体相关查询

    select table_schema,table_name from information_schema.columns where column_name = '字段名'; 查询某张表有几条记录 ...

  10. RocketMQ Windows 搭建

    一.rocketMQ 下载 官网:http://rocketmq.apache.org/ 本人使用是v4.3.0版本,百度网盘下载地址链接:https://pan.baidu.com/s/1qWewB ...