web容器调用Filter和Servlet顺序学习
1. 下面是一个简单的时序图:
2. 对上面时序图中用到的主要类进行分析
1) ApplicationFilterChain类,有两个主要函数,下面是省略过会的代码
public voiddoFilter(request, response) {//暴露在外面的调用接口
if( Globals.IS_SECURITY_ENABLED ) {
finalServletRequest req = request;
finalServletResponse res = response;
internalDoFilter(req,res);
return null;
} else {
internalDoFilter(request,response);
}
}
private voidinternalDoFilter(request, response) {
if (pos < n) {//判断是否还有filter需要执行
ApplicationFilterConfig filterConfig = filters[pos++];
Filter filter = null;
filter = filterConfig.getFilter();
filter.doFilter(request, response, this);
return ;
}
{//filter执行完后,执行servlet
if ((request instanceofHttpServletRequest) &&
(response instanceof HttpServletResponse)) {
servlet.service((HttpServletRequest) request,
(HttpServletResponse) response);
}
}
void addFilter(ApplicationFilterConfig filterConfig) {
if (n == filters.length) {
ApplicationFilterConfig[] newFilters =
new ApplicationFilterConfig[n + INCREMENT];
System.arraycopy(filters, 0, newFilters, 0, n);
filters = newFilters;
}
filters[n++] = filterConfig;
}
2) Servlet类的主要方法,以HttpServlet类为例,其主要方法是service(Request,Response)
public voidservice(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);//内部的方法
}
protected voidservice(HttpServletRequest , HttpServletResponse)
throws ServletException, IOException {
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
doGet(req, resp); //常用的方法
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
} catch (IllegalArgumentException iae) {
ifModifiedSince = -1;
}
if (ifModifiedSince < (lastModified / 1000 * 1000)) {
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);//常用的方法
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);
} else {
String errMsg =lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
web容器调用Filter和Servlet顺序学习的更多相关文章
- 深入刨析tomcat 之---第23篇 聊一下web容器的filter配置和defaultservet
writedby 张艳涛,在一个webapp应用程序内如何配置filter? <?xml version="1.0" encoding="ISO-8859-1&qu ...
- servlet入门学习之Web容器
一.web服务器 web服务器(程序/软件) 即:www服务器或http服务器.提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL.向浏览器提供服务的程序. 1.Web服务器是指 ...
- Javaweb里面的filter,listener,servlet
Filter 1Filter是什么:是过滤器简称 2Filter有什么作用:在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装 ...
- Introducation of Servlet filter(servlet过滤器介绍 )
本文章向大家介绍Servlet Filter,主要包括 Servlet Filter使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下. 过滤器是一个可以转换 ...
- filter in Servlet
Servlet过滤器: Servlet过滤器是用于执行某些过滤任务的对象.过滤器可以应用于servlet,jsp或html. Servlet过滤器主要用于以下任务: 预处理: Servlet过滤器用于 ...
- 【转载并整理】filter、servlet、interceptor、listener区别
参考文章:https://blog.csdn.net/u013087513/article/details/56835894 一.servlet servlet是一种运行服务器端的java应用程序,具 ...
- Spring Cloud Gateway 不小心换了个 Web 容器就不能用了,我 TM 人傻了
个人创作公约:本人声明创作的所有文章皆为自己原创,如果有参考任何文章的地方,会标注出来,如果有疏漏,欢迎大家批判.如果大家发现网上有抄袭本文章的,欢迎举报,并且积极向这个 github 仓库 提交 i ...
- Web开发中Listener、Filter、Servlet的初始化及调用
我们在使用Spring+SpringMVC开发项目中,web.xml中一般的配置如下: <?xml version="1.0" encoding="UTF-8&qu ...
- httpServletRequest对象、filter、servlet、servlet容器、catalina、tomcat、以及web容器之间的关系
学习servlet的时候经常感到疑惑 HttpServletRequest是服务器创建的?还是servlet容器创建的? 过滤器是服务器创建的?还是servlet容器创建的? serlet容器和tom ...
随机推荐
- Linux 进程间通信(一)(经典IPC:消息队列、信号量、共享存储)
有3种称作XSI IPC的IPC:消息队列.信号量.共享存储.这种类型的IPC有如下共同的特性. 每个内核中的IPC都用一个非负整数标志.标识符是IPC对象的内部名称,为了使多个合作进程能够在同一IP ...
- JavaSctipt语句for循环的思考
一.语法 for (语句 1; 语句 2; 语句 3) { 被执行的代码块; } 语句 1: 在循环(代码块)开始前执行, 可选. 语句 2: 定义运行循环(代码块)的条件, 可选, 如果省略了语句 ...
- linux环境配置nginx导致页面不刷新
在linux环境下,配置了nginx负载均衡,由于可能在虚拟主机的配置文件nginx.conf中,对缓存机制未配置成功,导致页面不刷新,仍然显示缓存中的内容. 最后通过注释nginx.conf文件中的 ...
- Centos之目录处理命令(七)
linux中 关于目录 有几个重要概念 一个是 / 根目录 还有一个当前用户的家目录 比如 root用户的家目录是 /root 普通用户的家目录是/home/xxx 下 root登录 默认家目录 ...
- 如何在Pycharm设置ES6语法环境
首先 如果不进行相关设置就刚ES6 语法的话,会出现下面提示性错误(运行还是能正常出效果的): (let 飘红, 这只是其中之一, 其他语法也会飘红) 接着,就是解决问题: 首先打开设置: 接着找到下 ...
- python并发编程&多进程(二)
前导理论知识见:python并发编程&多进程(一) 一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_cou ...
- Mac开发环境配置 就喜欢折腾
暑假放假,开始用自己的电脑配置开发环境进行学习. 虚拟机安装VMware tools也遇到一些问题.折腾之后解决,遇到各种问题,谷歌后都能解决. 折腾了几天虚拟机下的Ubuntu,发现mac带虚拟机太 ...
- gitattributes手册
gitattributes(5) Manual Page 1.gitattributes是什么? gitattributes用于定义每个路径的属性. 其语法是:pattern attr1 attr2 ...
- EXCEL 从网页复制的内容 单/多选框 在EXCEL删不掉 及 2007添加开发工具选项卡
从网页复制到Excel中的单选.多选框等,有时候删除时怎么都删不掉,很是恶心.这时候需要使用“开发工具”来删除.它是设计模式下的一种组件或者说控件. Excel 2007 的可以用下图方式按delet ...
- 解决远程桌面关闭后teamviewer不能连接的问题
使用windows远程桌面连接远程电脑,在关闭远程桌面后,windows系统会锁定,此时再用teamviewer连接会出现“无法捕捉画面”或者“拒绝连接”的问题. 解决办法:设置要连接的远程电脑上的t ...