浅谈Servlet的本质_2

HTTP请求刚刚进来的时候实际上只是一个HTTP请求报文,容器会自动将这个HTTP请求报文包装成一个HttpServletRequest对象,并且自动调用HttpServlet的service()方法来解析这个HTTP请求,service()方法会解析HTTP请求行,而HTTP请求行由method,uri,HTTP version三个组成,method就是get或者post,service()方法根据method来决定是执行doGet还是doPost,这一切都是服务器Tomcat/weblogic/websphere/jboss(容器)自动完成的,HTTP的格式也自动被解析。 
<p>只要你的类继承了HttpServlet,并且在web.xml里面配置了相应的servlet和mapping,服务器就会自动帮你执行以上过程。</p><p></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>孟军补充:其实这里的servlet引擎就是servletAPI规范,也就是接口规范,何必说的如此文邹邹的,真是。</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>servlet的本质其实就是servletAPI,程序员servlet编程其实就是针对servletAPI编程,web容器(例如jboss,tomcat,weblogic,webSphere)又称servlet容器其实主要做了两件事情:</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>1.对servletAPI中绝大部分规范进行了实现,如httpServletRequest接口等。就如同各大数据库厂商对jdbcAPI的实现一样,所以会有很多的web容器,如上jboss,tomcat,weblogic,webSphere等。</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>2.对servletAPI规范进行管理,web容器不但实现了绝大部分的servletAPI规范,并且还编写了大量自己的程序来管理web编程通信的内容,当然这些都是基于servletAPI规范的。说白了,规范有了,接口有了,web容器就是对规范和接口的管理,web容器是web编程或者更广一点说是http协议编程的入口和出口,进入之后,web容器自己的程序会调用servlet规范,而不必关系servlet规范的具体实现是什么(具体实现其实就是我们自己程序中继承的servlet规范了,这也就是web容器实现了绝大部分servletAPI规范,所剩下的那一极小部分让我们来实现的了),并且web容器会基于servlet规范对serveltAPI及其实现类进行管理,如内存释放等等。</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong> </strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>当然,servletAPI中也不全是抽象规范,也可能会有具体内容,例如service()方法中就有对doget()方法和都post()方法的分配,呵呵。</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong> </strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>我们编写的servlet类实际上就是实现了servletAPI规范,进而可以受web容器进行管理我们这些类(包括流转流程及内存控制等等等等)。这样,程序员就很幸福,有了web容器不必要关心服务器是怎样与http协议的另一端(客户端)是怎样通信的,web容器会帮我们完成这一切,并还帮我们管理servlet规范,这样通信数据的流入和留出就不必我们操心了,我们只要写数据流入后我们程序代码的执行情况即可,哈哈。</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>但是,如果研究方向广的话,我们完全可以去看看servlet规范都有哪些,web容器的源码程序也可以看看,看它是如何工作的。</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: SimSun; font-size: 24px;"><strong><u>前言:</u><span style="color: rgb(204, 0, 0);">一个Servlet程序其实就是一个实现了Java特殊接口的类,它由支持Servlet(具有Servlet引擎)的WEB服务器调用和启动运行。一个Servlet程序负责处理它对应的一个或者多个URL地址的访问请求,接收客户端发出的访问请求信息和产生响应内容。</span></strong></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Simsun; font-size: 24px;"><strong>   连接:<a target=_blank href="http://blog.csdn.net/ding2425/article/details/7646796" style="color: rgb(51, 102, 153); text-decoration: none;">http://blog.csdn.net/ding2425/article/details/7646796</a></strong></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-family: Simsun; font-size: 24px;">    <u>Servlet的特点:</u></span></strong></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-weight: bold;"><span style="font-family: Simsun; font-size: 24px;">    <span style="font-size: 18px;"><span style="color: rgb(51, 102, 255);">1.</span><span style="color: rgb(51, 102, 255);">Servlet其实就是一个供其他程序(Servlet引擎)调用的Java类,它不能独立运行,它的运行完全由Servlet引擎控制和调度;离开了Servlet引擎,Servlet就毫无能力了。
</span>     <span style="color: rgb(153, 102, 51);">2.Servlet引擎是一种容器程序,它负责管理和维护所有Servlet对象的生命周期。Servlet的加载、执行流程,以及如何接收客户端传过来的访问请求信息和如何将产生的响应内容传回给客户端等具体的底层事务,都通过Servlet引擎来实现。Servlet引擎负责将客户端的访问请求信息转交给Servlet程序,将Servlet程序产生的访问响应内容转交给客户端。简而言之,Servlet引擎就像是客户端与服务器端的Servlet程序打交道的中介。
</span>     <span style="color: rgb(51, 102, 255);">3.</span><span style="color: rgb(51, 102, 255);">Servlet程序的运行过程其实就是它与Servlet引擎交互的过程,Servlet程序只和Servlet引擎打交道,它与WEB服务器和客户端没有任何的交互。
</span>     <span style="color: rgb(153, 102, 51);">4.Servlet属于一种插件,它是一个提供了一些约定方法供容器调用的类,它只负责在自身的方法中接收并处理容器传过来的数据,以及生成并返回给容器需要的数据和状态信息。
</span>     <span style="color: rgb(51, 102, 255);">5.Servlet</span><span style="color: rgb(51, 102, 255);">最常见的应用在读取WEB浏览器传递给WEB服务器的请求信息并产生WEB服务器返回给WEB浏览器的响应内容(动态网页文档内容)。
</span>     <span style="color: rgb(153, 102, 51);">6.WEB服务器上可以部署多个Servlet程序,每个Servlet程序必须说明其能处理的URL请求,当收到符合的URL请求后,将由Servlet引擎去调用相应的Servlet程序处理请求。</span></span></span></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-family: Simsun; font-size: 18px; color: rgb(153, 102, 51);">     <span style="font-size: 24px; color: rgb(0, 0, 0);"><u>Servlet的运行过程:</u></span></span></strong></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-family: Simsun; font-size: 18px; color: rgb(153, 102, 51);">     <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164360_7415.jpg" width="905" height="330" style="border: none; max-width: 100%; width: 878px; height: 350px;" /></span></strong></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-family: Simsun; font-size: 18px; color: rgb(153, 102, 51);"></span></strong> </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-family: Simsun; font-size: 18px; color: rgb(153, 102, 51);">     <span style="font-size: 24px; color: rgb(0, 0, 0);"><u>浏览器访问Servlet的过程示意图:</u></span></span></strong></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">              <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164746_1090.jpg" width="250" height="200" style="border: none; max-width: 100%; width: 657px; height: 283px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">                 <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164787_2169.jpg" width="718" height="184" style="border: none; max-width: 100%; width: 624px; height: 264px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">                       <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164836_2767.jpg" width="606" height="400" style="border: none; max-width: 100%; width: 606px; height: 299px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">                    <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164862_7727.jpg" width="665" height="387" style="border: none; max-width: 100%; width: 604px; height: 294px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">                    <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164914_1277.jpg" width="634" height="407" style="border: none; max-width: 100%; width: 596px; height: 242px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">                      <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164946_7745.jpg" width="609" height="404" style="border: none; max-width: 100%; width: 579px; height: 263px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">                       <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339164982_3031.jpg" width="546" height="404" style="border: none; max-width: 100%; width: 546px; height: 260px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">                    <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339165007_6330.jpg" width="585" height="380" style="border: none; max-width: 100%; width: 562px; height: 253px;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>             <span style="font-size: 24px; color: rgb(0, 0, 0);"><u>Servelt、Servlet容器、Servlet API之间的关系:</u></span></strong></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><u><span style="font-size: 18px;"></span></u></strong>             <span style="font-size: 18px;">(不同的厂商都按照统一的API生产Servlet容器,这样编写出来的Servlet程序便可在不同容器中移植而无需修改代码~~~)</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">         <img alt="" src="https://img-my.csdn.net/uploads/201206/08/1339166530_5685.jpg" style="border: none; max-width: 100%;" /></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">              <em><span style="font-size: 18px;">上面的内容局部的剖析了Servlet底层的一些信息,如有不足之处,望各路大侠指点提出。欢迎交流。探讨~~~</span></em></p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值