1. BaseServlet 的作用

  • 让一个Servlet可以处理多种不同的请求,不同的请求调用Servlet的不同方法.

2. 实现原理

  • 客户端发送请求时, 必须多给出一个参数, 用来说明要调用的方法!! 这样,BaseServlet 通过该参数来

    调用目标方法.
  • 请求处理方法的签名必须与 service 相同, 即方法的返回值和参数,以及声明的异常都相同.

// 代码示例
public class AServlet extends HttpServlet{ // service 方法
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ // 获取参数, 用来识别客户端想请求的方法
// 然后判断是哪一个方法, 是哪一个方法,就调用哪一个方法. // 我们这里给参数的名字为 method
String methodName = req.getParameter("method"); if(methodName.equals("addUser")){
addUser(req,resp);
}else if(methodName.equals("editUser")){
editUser(req,resp);
}else if(methodName.equals("deleteUser")){
deleteUser(req,resp);
}
} // 添加客户的方法
public void addUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()....");
} // 编辑客户的方法
public void editUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()....");
} // 删除客户的方法
public void deleteUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()....");
}
} // 升级版
/*
* 思路:
* 得到方法名称, 是否可以通过反射来调用方法?
* 步骤:
* 1. 得到方法名, 通过方法名再得到 Method 类的对象
* 2. 需要得到 class, 然后调用它的方法进行查询! 得到 Method
* 3. 我们要查询的是当前类的方法, 所以我们需要得到当前类的 Class
*/ public abstact class BaseServlet extends HttpServlet{ public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ // 获取参数, 用来识别用户想请求的方法
String methodName = req.getParameter("method"); // 判断该参数是否存在, 不存在,抛出异常
if(methodName == null || methodName.trim().isEmpty()){
throw new RuntimeException("您没有传递 method 参数! 无法确定您想调用的方法");
} // 得到当前类的 class 对象
Class c = this.getClass(); // 查询方法, 参数需要: 方法名和该方法的参数类型
// 该方法的参数类型必须与 service 中的参数类型一致
Method method = null;
try{
method = c.getMethod(methodName,
HttpServletRequest.class, HttpServletResponse.class);
} catch(Exception e){
throw new RuntimeException("您要调用的方法"+methodName+",它不存在!");
} // 调用 method 方法
// 反射调用, 第一参数表示当前类,
// 正常调用: this.method(req,resp)
try{
method.invoke(this,req,resp);
} catch(Exception e){
throw new RuntimeException(e);
}
} // AServlet 继承 BaseServlet
public void class AServlet extends BaseServlet{
// 添加客户的方法
public void addUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()....");
} // 编辑客户的方法
public void editUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()....");
} // 删除客户的方法
public void deleteUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()....");
}
} // 处理转发和重定向问题
public void class BServlet extends BaseServlet{ // 添加客户的方法
public String addUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()...."); // 返回表示转发的字符串, "f" 表示 forward
return "f:/index.jsp";
} // 编辑客户的方法
public String editUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()...."); // 返回表示重定向的字符串, "r" 表示 redirect
return "r:/index.jsp";
} // 删除客户的方法
public String deleteUser(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{ System.out.println("addUser()...."); return null;
}
} // BaseServlet 升级
public void abstract BaseServlet extends HttpServlet{
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException{ String methdoName = req.getParameter("method"); if(methodName == null || methodName.trim().isEmpty()){
throw new RuntimeException("您没有传递method参数,无法确定要调用的方法!");
} Class c = this.getClass(); Method method=null;
try{
method = c.getMethod(methodName,
HttpServletRequest.class,HttpServletResponse.class);
}catch(Exception e){
throw new RuntimeException("您要调用的"+methodName+"方法,它不存在!");
} // 调用 method 方法 try{ String result = (String)method.invoke(this,req,resp); /*
* 获取请求处理方法执行后返回的字符串, 它表示转发或重定向的路径!
* 完成转发或重定向.
*
* 如果用户返回的字符串为 null, 或为 "", 那么我们什么也不做!
*
* 查看返回的字符串中是否包含冒号, 如果没有, 表示转发
* 如果有, 使用冒号分割字符串, 得到前缀和后缀!!
* 其中前缀如果是 f, 表示转发, 如果是 r, 表示重定向, 后缀就是要转发或重定向的路径了!
*/ if(result == null || result.trim().isEmpty()){
return;
} // 如果不为空
if(result.contains(":")){
// 使用冒号分割字符串, 得到前缀和后缀
int index = result.indexOf(":"); // 获取冒号的位置
String s = result.substring(0,index); // 获取前缀
String path = result.subString(index+1); // 获取后缀, 即路径 if(e.equalsIgnoreCase("r")){ // 如果前缀是 r, 重定向
resp.sendRedirect(req.getContextPath()+path);
}else if(e.equalsIgnoreCase("f")){
req.getRequestDispatcher(path).forward(req,resp);
} else {
throw new RuntimeException("您指定的操作:"+s+",当前版本不支持!");
} } else { // 没有冒号, 默认为转发
req.getRequestDispatcher(result).forward(req,resp);
} }catch(Exception e){
throw new RuntimeException(e);
}
}
}

参考资料:

BaseServlet 介绍的更多相关文章

  1. day18(JDBC事务&连接池介绍&DBUtils工具介绍&BaseServlet作用)

    day18总结 今日思维导图: 今日内容 事务 连接池 ThreadLocal BaseServlet自定义Servlet父类(只要求会用,不要求会写) DBUtils à commons-dbuti ...

  2. 十三、事务、连接池 、ThreadLocal 、BaseServlet自定义Servlet父类 、 DBUtils à commons-dbutils

    l 事务 l 连接池 l ThreadLocal l BaseServlet自定义Servlet父类(只要求会用,不要求会写) l DBUtils à commons-dbutils 事务 l 事务的 ...

  3. CSS3 background-image背景图片相关介绍

    这里将会介绍如何通过background-image设置背景图片,以及背景图片的平铺.拉伸.偏移.设置大小等操作. 1. 背景图片样式分类 CSS中设置元素背景图片及其背景图片样式的属性主要以下几个: ...

  4. MySQL高级知识- MySQL的架构介绍

    [TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...

  5. Windows Server 2012 NIC Teaming介绍及注意事项

    Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...

  6. Linux下服务器端开发流程及相关工具介绍(C++)

    去年刚毕业来公司后,做为新人,发现很多东西都没有文档,各种工具和地址都是口口相传的,而且很多时候都是不知道有哪些工具可以使用,所以当时就想把自己接触到的这些东西记录下来,为后来者提供参考,相当于一个路 ...

  7. JavaScript var关键字、变量的状态、异常处理、命名规范等介绍

    本篇主要介绍var关键字.变量的undefined和null状态.异常处理.命名规范. 目录 1. var 关键字:介绍var关键字的使用. 2. 变量的状态:介绍变量的未定义.已定义未赋值.已定义已 ...

  8. HTML DOM 介绍

    本篇主要介绍DOM内容.DOM 节点.节点属性以及获取HTML元素的方法. 目录 1. 介绍 DOM:介绍DOM,以及对DOM分类和功能的说明. 2. DOM 节点:介绍DOM节点分类和节点层次. 3 ...

  9. HTML 事件(一) 事件的介绍

    本篇主要介绍HTML中的事件知识:事件相关术语.DOM事件规范.事件对象. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三 ...

随机推荐

  1. 网页中多一些常见效果之伸缩菜单(主要是学习js的书写方法)

    效果如下图: 代码很简单,如下: <!doctype html> <html lang="en"> <head> <meta charse ...

  2. 用Darwin开发RTSP级联server(拉模式转发)(附源代码)

    源代码下载地址:https://github.com/EasyDarwin orwww.easydarwin.org 在博客 在Darwin进行实时视频转发的两种模式 中,我们描写叙述了流媒体serv ...

  3. IP报文格式及各字段意义

    IP数据包由报头和数据两部分组成.报头的前一部分是固定长度,共20字节.在报头的固定部分的后面是可选部分——IP选项和填充域. 首部各字段的含义如下 1.版本      占4位,指IP协议的版本. 2 ...

  4. 设计模式之里氏替换原则(LSP)

    在java等面向对象编程语言里面,我想继承性应该是一大特色吧!所以今天所要讲解的里氏替换原则主要是针对这一特性而提出来的,当我们定义对象的时候,尽量找出对象之间的相同点,然后将其抽象成基类对象.比如水 ...

  5. gitlab 把或名改成IP

    [root@GitLab assets]# cat /etc/gitlab/gitlab.rb # Change the external_url to the address your users ...

  6. 美团HD(7)-添加取消搜索按钮

    DJSelectCityViewController.m #pragma mark - UISearchBar 代理方法 /** SearchBar开始编辑 */ - (void)searchBarT ...

  7. 李洪强iOS开发之大神必备的Xcode插件

    iOS开发大神必备的Xcode插件 写在前面 工欲善其事,必先利其器,iOS开发中不仅要学会Xcode的基本操作,而且还得学会一些Xcode的使用技巧,如掌握常用的快捷键等,还有就是今天要说到的Xco ...

  8. 基于GitLab的前端Assets发布体系

    以SVN+RMS为核心的发布系统,对前端开发的影响上来看,存在以下问题: 覆盖式的发布,容易导致线上问题. js一旦发布,就有可能被任意其他页面使用.被引用的越多,就越重要.一旦核心js出现故障,影响 ...

  9. 我的 Android 开发实战经验总结

    我的 Android 开发实战经验总结 字数4440 阅读5137 评论43 喜欢250 曾经一直想写一篇总结 Android 开发经验的文章,预计当时的我还达不到某种水平,所以思路跟不上,下笔又捉襟 ...

  10. apple touch icon 大小总结

    <!-- For Chrome for Android: --> <link rel="icon" sizes="192x192" href= ...