Servlet学习笔记(三)-Servlet类图分析(一)

在学习Servlet之前,让我先来看看它的“框架”吧。先摸清门路,下面才能走的顺当~

安装完JAVA EE 5的SDK后,在安装目录下的lib文件夹内找到javaee.jar文件(它就是servletAPI,包括jspAPI的"巢穴").

用JD-GUI打开javaee.jar后,在Servlet包中,可以看到一共有19个类名关于Servlet的类,6个其他类,以及HTTP包中5个类名关于Servlet的类。

让我们先对前19个类和HTTP包中的5个类进行下大致了解!

整体关系图

image

这里分成8块来讲

Servlet接口

涉及的关系图:

image

解释:

public abstract interface Servlet:

查看接口源码(Copy Code)

定义所有servlet都必须实现的方法。

这个接口定义了初始化servle的方法,为请求提供服务的方法和从服务器移除servlet的方法。这两个方法称为生命周期方法。它的调用顺序是:

1.构造servlet,然后使用 init 方法将其初始化。

2.处理来自客户端的对service方法的所有调用。

3.从服务中取出servlet,然后使用 destroy 方法销毁它,最后进行垃圾回收并终止它。

除了生命周期方法外,还提供了 getServletConfig 方法和 getServletInfo 方法。

servlet可以使用前一种方法获得在servlet被实例时的配置对象 ServletConfig,而后一种允许servlet返回有关其自身的基本信息,比如作者,版权,版本等。

这是中文API上的解释,有点含糊~我来细细的解释下。

首先在全局的XML配置文件中,有个参数叫 load-on-startup (可选)。

<load-on-startup>n</load-on-startup>

下面是来自于官网的一段说明(自己翻译了下,英语烂~凑合出一个大致意思):

The   load-on-startup   element   indicates   that   this   servlet   should   be   loaded   (instantiated   and   have   its   init()   called)   on   the   startup   of   the   web   application.   The   optional   contents   of   these   element  (可选) must   be   an   integer   indicating   the   order   in   which   the   servlet   should   be   loaded.(servlet加载顺序)   If   the   value   is   a   negative   integer(负数),   or   the   element   is   not   present(未指定),   the   container   is   free   to   load   the   servlet   whenever   it   chooses.(被请求时加载)     If   the   value   is   a   positive   integer   or   0(正数或为0),   the   container   must   load   and   initialize   the   servlet   as   the   application   is   deployed(当服务器启动时就加载servlet).   The   container   must   guarantee   that   servlets   marked   with   lower   integers(正数值小的)are   loaded   before   servlets   marked   with   higher   integers(正数值大的).   The   container   may   choose   the   order   of   loading   of   servlets   with   the   same   load-on-start-up   value(如果值相同,由容器自己选择启动顺序).

总结如下:

这个值用来标记Servlet的加载顺序(优先级)

未指定值或者为负数时:随该Servlet第一次被请求时才加载。

正数:随服务器启动而加载,正数值越小,加载优先级越高。

值相同:由服务器自己选择启动顺序,也就是随机吧。

这里我们假设n为1,也就是随服务器启动而加载。

当加载Servlet时,首先运行 init方法 初始化Servlet对象,并传入和Servle配置相关的ServletConfig对象

servlet容器仅在实例化servlet之后调用 init 方法一次。只有当init成功完成,才能继续处理任何请求。

查看init方法源代码(在抽象类GenericServlet中)(Copy Code)

然后调用Service方法,传入ServletRequest(请求)和ServletResponse(响应)对象,这个方法会使用getMethod方法,获得访问页面所用的方式(比如:get,post,head,put,delete等等),然后调用相应的doxxx方法(比如:doGet,doPust,doPost等等)。

看下Service方法的源代码(在HttpServlet抽象类中),加深印象,代码很简单哦~:

查看Service方法(Copy Code)

可以看到HttpServlet类实现了service方法(public),并且重载了一次,由这个重载方法(protected)判断并执行相应的doXXX方法。

访问修饰符为public的servic方法的参数是ServletRequestServletResponse

访问修饰符为protected(同包或者子类可访问)的service方法参数是HttpServletRequestHttpServletResponse。

如果您选择在您的Servlet代码中覆盖service方法,最好都要显式的调用doXXX方法,方法参数为HttpServletRequesHttpServletResponse对象。

最后处理完servic方法,进入相应的doXXX方法,处理客户端请求。

当服务器被关闭时,会先卸载所有的servlet,此时会执行destroy方法,同样,此方法仅在服务器被关闭时被调用一次。

查看destroy方法(Copy Code)

可见,其方法内容为空,那么我们可以利用此方法进行一些清除资源的操作(比如内存,文件句柄和线程)。

(未完待续…)

posted @ 2011-01-14 01:14  小 .xin  阅读(808)  评论(0)    收藏  举报
回到页首