Servlet类源码说明
servlet是学习java web不可不懂的一个类,网上各种教程都参杂太多,每次理解都感觉像把别人吐出来的食物再放在嘴里咀嚼,小编一怒之下,直接打开源码,原汁原味的芬芳扑面而来:
/**
* Defines methods that all servlets must implement.
*
* <p>A servlet is a small Java program that runs within a Web server.
* Servlets receive and respond to requests from Web clients,
* usually across HTTP, the HyperText Transfer Protocol.
*
* <p>To implement this interface, you can write a generic servlet
* that extends
* <code>javax.servlet.GenericServlet</code> or an HTTP servlet that
* extends <code>javax.servlet.http.HttpServlet</code>.
*
* <p>This interface defines methods to initialize a servlet,
* to service requests, and to remove a servlet from the server.
* These are known as life-cycle methods and are called in the
* following sequence:
* <ol>
* <li>The servlet is constructed, then initialized with the <code>init</code> method.
* <li>Any calls from clients to the <code>service</code> method are handled.
* <li>The servlet is taken out of service, then destroyed with the
* <code>destroy</code> method, then garbage collected and finalized.
* </ol>
*
* <p>In addition to the life-cycle methods, this interface
* provides the <code>getServletConfig</code> method, which the servlet
* can use to get any startup information, and the <code>getServletInfo</code>
* method, which allows the servlet to return basic information about itself,
* such as author, version, and copyright.
*
* @author Various
*
* @see GenericServlet
* @see javax.servlet.http.HttpServlet
*
*/ public interface Servlet {
1、Servlet是一个接口,定义的所有方法将被所有子类servlet实现;
2、一个Servlet是一个小java程序,必须与Web server一起运行;
3、Servlet从Web客户端获取请求并响应请求,通常通过Http(超文本传输协议);
4、通过实现这个Servlet接口,你可以写一个通用的servlet(继承于javax.servlet.GenericServlet),或者写一个支持HTTP协议的servlet(继承javax.servlet.http.HttpServlet);
5、这个接口定义一些方法去初始化一个servlet,去响应请求,并且可以从server中销毁servlet;
6、这些被称为生命周期的方法,以下面的顺序执行:
①:构造一个servlet实例,调用 init 方法进行初始化;
②:调用service方法处理客户端发过来的任何请求;
③:调用destroy方法销毁servlet,并由垃圾回收器终结这个servlet对象。
/**
* Called by the servlet container to indicate to a servlet that the
* servlet is being placed into service.
*
* <p>The servlet container calls the <code>init</code>
* method exactly once after instantiating the servlet.
* The <code>init</code> method must complete successfully
* before the servlet can receive any requests.
*
* <p>The servlet container cannot place the servlet into service
* if the <code>init</code> method
* <ol>
* <li>Throws a <code>ServletException</code>
* <li>Does not return within a time period defined by the Web server
* </ol>
*
*
* @param config a <code>ServletConfig</code> object
* containing the servlet's
* configuration and initialization parameters
*
* @exception ServletException if an exception has occurred that
* interferes with the servlet's normal
* operation
*
* @see UnavailableException
* @see #getServletConfig
*
*/ public void init(ServletConfig config) throws ServletException;
1、被servlet容器调用来表明是一个servlet,并且这个servlet会用来实现你的业务逻辑;
2、在servlet实例创建以后 init 方法被调用,并且只被调用一次;
3、在servlet处理各种请求之前,init方法必需成功运行,否则当抛出ServletException时,servlet将不会接收到任何请求;
4、可以在init方法中传入ServletConfig初始化参数
/**
*
* Returns a {@link ServletConfig} object, which contains
* initialization and startup parameters for this servlet.
* The <code>ServletConfig</code> object returned is the one
* passed to the <code>init</code> method.
*
* <p>Implementations of this interface are responsible for storing the
* <code>ServletConfig</code> object so that this
* method can return it. The {@link GenericServlet}
* class, which implements this interface, already does this.
*
* @return the <code>ServletConfig</code> object
* that initializes this servlet
*
* @see #init
*
*/ public ServletConfig getServletConfig();
1、这个方法返回一个ServletConfig(一个接口)类的对象,包含了初始化servlet时的参数;
2、ServletConfig这个接口的实现负责存储servlet的初始化参数。
/**
* Called by the servlet container to allow the servlet to respond to
* a request.
*
* <p>This method is only called after the servlet's <code>init()</code>
* method has completed successfully.
*
* <p> The status code of the response always should be set for a servlet
* that throws or sends an error.
*
*
* <p>Servlets typically run inside multithreaded servlet containers
* that can handle multiple requests concurrently. Developers must
* be aware to synchronize access to any shared resources such as files,
* network connections, and as well as the servlet's class and instance
* variables.
* More information on multithreaded programming in Java is available in
* <a href="http://java.sun.com/Series/Tutorial/java/threads/multithreaded.html">
* the Java tutorial on multi-threaded programming</a>.
*
*
* @param req the <code>ServletRequest</code> object that contains
* the client's request
*
* @param res the <code>ServletResponse</code> object that contains
* the servlet's response
*
* @exception ServletException if an exception occurs that interferes
* with the servlet's normal operation
*
* @exception IOException if an input or output exception occurs
*
*/ public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
1、这个方法在init()方法调用成功后调用,此方法是servlet容器负责调用来授予servlet对请求进行响应;
2、Servlets通常运行在多线程的servlet容器内部,可以同时处理多个请求。开发人员必须认识到同步访问任何共享资源,如文件、网络连接,以及servlet的类变量和实例变量。
/**
* Returns information about the servlet, such
* as author, version, and copyright.
*
* <p>The string that this method returns should
* be plain text and not markup of any kind (such as HTML, XML,
* etc.).
*
* @return a <code>String</code> containing servlet information
*
*/
public String getServletInfo();
1、此方法返回关于servlet的信息,如作者、版本、版权;
2、这个方法返回的必须是纯字符串文本,不能带有html、xml等标记
/**
*
* Called by the servlet container to indicate to a servlet that the
* servlet is being taken out of service. This method is
* only called once all threads within the servlet's
* <code>service</code> method have exited or after a timeout
* period has passed. After the servlet container calls this
* method, it will not call the <code>service</code> method again
* on this servlet.
*
* <p>This method gives the servlet an opportunity
* to clean up any resources that are being held (for example, memory,
* file handles, threads) and make sure that any persistent state is
* synchronized with the servlet's current state in memory.
*
*/ public void destroy();
}
1、此方法被servlet容器调用,指示了这个servlet已经完成业务实现,可以被销毁了;
2、此方法只被调用一次,在servlet的service方法中的线程都退出或者时间超时时调用;该servlet将不再调用service方法;
3、这个方法给servlet一个机会去清理所占用的资源(内存、文件、线程),确保与servlet的当前内存状态保持同步的持续状态。
读完源码,大脑立马清晰多了。釉木友同感?
Servlet类源码说明的更多相关文章
- Java集合---Array类源码解析
Java集合---Array类源码解析 ---转自:牛奶.不加糖 一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Prim ...
- Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析
上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. ...
- List 接口以及实现类和相关类源码分析
List 接口以及实现类和相关类源码分析 List接口分析 接口描述 用户可以对列表进行随机的读取(get),插入(add),删除(remove),修改(set),也可批量增加(addAll),删除( ...
- Thread类源码剖析
目录 1.引子 2.JVM线程状态 3.Thread常用方法 4.拓展点 一.引子 说来也有些汗颜,搞了几年java,忽然发现竟然没拜读过java.lang.Thread类源码,这次特地拿出来晒一晒. ...
- Java并发编程笔记之Unsafe类和LockSupport类源码分析
一.Unsafe类的源码分析 JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通过使用JNI的方式来访问本地C++实现库. rt.jar ...
- python附录-builtins.py模块str类源码(含str官方文档链接)
python附录-builtins.py模块str类源码 str官方文档链接:https://docs.python.org/3/library/stdtypes.html#text-sequence ...
- Long类源码浅析
1.Long类和Integer相类似,都是基本类型的包装类,类中的方法大部分都是类似的: 关于Integer类的浅析可以参看:Integer类源码浅析 2.这里主要介绍一下LongCache类,该缓存 ...
- java.lang.Void类源码解析_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 在一次源码查看ThreadGroup的时候,看到一段代码,为以下: /* * @throws NullPointerEx ...
- org.reflections 接口通过反射获取实现类源码研究
org.reflections 接口通过反射获取实现类源码研究 版本 org.reflections reflections 0.9.12 Reflections通过扫描classpath,索引元数据 ...
随机推荐
- 处理EXCEL11问题
程序原本是好使的,但是自从卸载OFFICE11而安装14后,程序无法启动. 重新安装2003 ,然后,删除原引用 Microsoft.Office.Interop.Excel,然后添加引用,浏览,找到 ...
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
- google搜索 site:pku.edu.cn inurl:aspx 即可查找所有动态网页 =====html(静态网页) asp(动态) jsp(动态) php(动态) cgi(网络程序) aspx(动态)
shodan shodan和我们国内的钟馗之眼是一种搜索引擎,他们区别于百度等引擎,他们只爬设备,只爬联网设备. 网址为: https://www.shodan.io/ Shodan,也有人把他叫撒旦 ...
- BootStrap--scroll
滚动侦测 滚动侦测基本使用方法为: <body data-spy="scroll"> <nav class="navbar navbar-default ...
- 『转』Kaspersky Internet Security for Android &KMS – 免费6个月
卡巴越南的活动,需要注册账户,完成小调查,24小时内发送激活码,激活码3个月内有效.建议用谷歌翻译下网站.KIS for Android 的激活码也通用于 Kaspersky Mobile Secur ...
- New Concept English Two 13 31
$课文29 出租汽车 294. Captain Ben Fawcett has bought an unusual taxi and has begun a new service. 本.弗西特机长买 ...
- 初始化一个static的Map变量
第一种方法:static块初始化 public class Demo{ private static final Map<String, String> myMap; static { m ...
- Kotlin Reference (九) Properties and Fields
most from reference 声明属性 Koltin的类都有属性,这些属性可以声明为可变的,使用var关键字或用val关键字生声明不可变属性. class Address { var nam ...
- 移动端rem布局雪碧图解决方案 以及分享腾讯团队的在线雪碧图工具
先分享一下地址:http://alloyteam.github.io/gopng/ 使用的方法也很简单,将需要的小图标拖进去,全部拖进去后再调位置(每拖一个进去都会帮你排列好,但是没有间隔,所以全部拖 ...
- IOS NSBundle的使用,注意mainBundle和Custom Bundle的区别
1.[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth ...