下面说明在部署描述符中是如何进行Servlet声明和映射的,这个DTD的全部内容可以在下面这个地址获得: http://java.sun.com/dtd/web-app_2_3.dtd
在这个DTD中有关Servlet声明和映射和映射的部分如下:
The servlet element contains the declarative data of a
servlet. If a jsp-file is specified and the load-on-startup element
is present, then the JSP should be precompiled and loaded.
Used in: web-app
-->
(servlet-class|jsp-file), init-param*, load-on-startup?, runas?, security-role-ref*)>
The servlet-class element contains the fully qualified class name
of the servlet.
Used in: servlet
-->
The servlet-mapping element defines a mapping between a servlet
and a url pattern
Used in: web-app
-->
The servlet-name element contains the canonical name of the
servlet. Each servlet name is unique within the web application.
Used in: filter-mapping, servlet, servlet-mapping
-->
根据以上DTD,一个典型的Servlet的声明的格式如下:
catalog
com.mycorp.CatalogServlet
catalog
Spring
一个典型的Servlet映射如下:
catalog
/catalog/*
Servlet生存周期
在介绍Servlet的生存周期之前需要先介绍一下javax.servlet.Servlet接口。所有的Servlet必须实现或者间接实现这个借口,我们通常可以通过继承javax.servlet.GenericServlet或者javax.servlet.http.HttpServlet.类来实现这个接口。
这个接口中定义了下面5种方法:
public void init(ServletConfig config);
public ServletConfig getServletConfig();
public void service(ServletRequest req, ServletResponse res);
public String getServletInfo();
public void destroy() ;