当前位置: 首页 > news >正文

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的当前内存状态保持同步的持续状态

 

读完源码,大脑立马清晰多了。釉木友同感?

转载于:https://www.cnblogs.com/bossen/p/5943300.html

相关文章:

  • 连接 insance 到 vlan101 - 每天5分钟玩转 OpenStack(97)
  • 15、限定词
  • Automated Memory Analysis
  • 5.openstack之mitaka搭建计算节点
  • 改变Chrome浏览器主程序_缓存_个人信息路径
  • Xtreme9.0 - Car Spark 动态规划
  • java 计算距离现在几分,几个小时,几天
  • pragma
  • VC/MFC使用OLE操作 EXCEL
  • js定时器的使用(实例讲解)
  • 1 storm基本概念 + storm编程规范及demo编写
  • 清北学堂模拟day6 花
  • awk之shell快速修改文件名
  • ajax测试Demo以及json简单的转化
  • 《深入理解JavaScript》—— JSON
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 【个人向】《HTTP图解》阅后小结
  • css系列之关于字体的事
  • golang 发送GET和POST示例
  • JavaScript创建对象的四种方式
  • node学习系列之简单文件上传
  • quasar-framework cnodejs社区
  • ubuntu 下nginx安装 并支持https协议
  • ucore操作系统实验笔记 - 重新理解中断
  • 基于HAProxy的高性能缓存服务器nuster
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 入手阿里云新服务器的部署NODE
  • 物联网链路协议
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 06-01 点餐小程序前台界面搭建
  • ###STL(标准模板库)
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (八)Flask之app.route装饰器函数的参数
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (转)程序员疫苗:代码注入
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • (转)母版页和相对路径
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .cfg\.dat\.mak(持续补充)
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .Net 高效开发之不可错过的实用工具
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .net操作Excel出错解决
  • .net打印*三角形
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • ??eclipse的安装配置问题!??
  • @AliasFor注解
  • @property python知乎_Python3基础之:property
  • [ vulhub漏洞复现篇 ] ECShop 2.x / 3.x SQL注入/远程执行代码漏洞 xianzhi-2017-02-82239600
  • [AIGC] Nacos:一个简单 yet powerful 的配置中心和服务注册中心
  • [BUG]vscode插件live server无法自动打开浏览器