Servlet详解

servlet 属于动态资源,作用是处理请求,服务器通常会把接受到的请求交给servlet处理;完成:

1.接受请求;
2.处理请求
3.完成响应;

实现Servlet 的三种方式:

1.实现javax.servlet.Servlet. ----interface

2.extends  javax.servlet.GenericServlet. ---Abstract class

3.extends javax.servlet.http.HttpServlet. ----->this is we use class ,is important

            ----Servlet 中大多数方法不由我们调用,而是由我们的服务器进行调用,Servlet Object 也是由服务器进行创建的----

 

我们需要学习HttpServlet ,but HttpServlet extends Abstract GenericServlet and implement Servlet .so  我们必须学习interface servlet 以及 abstract genericservlet我们看一看interface servlet 提供的方法:

我们看一下 servlet 的doc 文档:
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>.


servlet 是运行在web 服务器 中的小型Java 程序;servlet 用于接受和响应来自web 客户端的请求,通常用于http 超文本传输协议;

去实现这个接口,你可以通过继承 genericservlet 去写一般的servlet,或者继承httpservlet 写一个http servlet

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

这个接口定义了 初始化一个servlet ,服务器请求,以及从服务器移除一个servlet 的方法;

这三个是servlet 的生命周期方法,他们调用通过如下顺序被调用

 <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>

servlet 被构造时,通过init 方法进行初始化;

一些客户端的请求通过service 方法去处理负责

servlet 退出芙蕖,然后通过destroy 方法进行销毁,然后被垃圾回收回收;

* <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.

除了生命周期方法外,这个接口还提供了servlet getservletconfig 方法用来获取启动信息;

以及getservletinfo 方法 用来返回servlet 自己的基本信息像 author version copyright

Servlet have three lifecycle method:Servlet 一共有五种方法,其中有三种是生命周期方法,由我们编写,但由服务器自己调用;

   1.init(ServletConfig config):  ----> servlet 初始化操作,创建之后,马上执行,由服务器自己调用,只会执行一次;


 2.destory(): ---->销毁操作,由服务器调用,只会执行一次;

   3. service(ServletRequest req, ServletResponse res) ----> 由服务器调用,会被execute 很多次,客户端没请求一次就会调用一次;

   4.getServletConfig() ----> obtain ServletConfig Object, initialization and startup parameters for this servlet

  

   5.getServletInfo() -----> obtain getServletInfo Object .returns information about the Servlet such as author,version ,copyRight


在servlet 中维护着一个servletconfig对象,这个对象用于servlet 的初始化操作;

/**

 * A servlet configuration object used by a servlet container

 * to pass information to a servlet during initialization. 

 */

源码解释:servlet容器使用的servlet 配置对象,在初始化过程中传递给servlet 的信息;

ServletConfig :servlet 容器使用servlet 配置对象,在servlet 初始化过程中,将这个对象传递给servlet

1.getServletName(); ----->与 <servlet-name>SpringServlet</servlet-name> 对应,obtain servlet name
2.getServletContext(); -----> obtain servlet 上下文

3.getInitParameter(String name); -->与<init-param><param-name>123</param-name><param-value>456</param-value></init-param> 对应;用于
  获取初始化参数;

4.getInitParameterNames();---->obtain 初始化参数集合


  我们看一看Abstract  GenericServlet 提供的方法:

GenericServlet implements Servlet, ServletConfig

可以看出GenericServlet 实现了Servlet ServletConfig 接口 ,so GenericServlet 也可以获得ServletConfig 对象中的内容;
 
init(): --->在init(ServletConfig config) 初始化时候,会调用init() method ,一般init() 方法是我们自定义需要初始化的内容
     public void init(ServletConfig config) throws ServletException {
    this.config = config;
    this.init();
    }

HttpServlet 方法,实现了http 协议的servlet,我们所需要的;

1.对 service(ServletRequest req, ServletResponse res)方法进行了实现 ,对ServletRequest,ServletResponse 进行了强转成http相关的request 与   response,调用时候,由服务器帮我们实现了强转;

  here's no need to override this method. 我们不需要复写这个方法;

 public void service(ServletRequest req, ServletResponse res)
        throws ServletException, IOException
    {
        HttpServletRequest  request;
        HttpServletResponse response;
        
        if (!(req instanceof HttpServletRequest &&
                res instanceof HttpServletResponse)) {
            throw new ServletException("non-HTTP request or response");
        }

        request = (HttpServletRequest) req;
        response = (HttpServletResponse) res;

        service(request, response);
    }
}
2.调用了
service(HttpServletRequest req, HttpServletResponse resp) 根据不同的请求方法进行转发post get delete 等。。

3.我们需要去重写doGet doPost ....等方法,完成请求的接受处理响应;

总之:servlet 线程是不安全的;init(); service(); servletConfig object 需要去深刻理解的,对我们理解框架有很好的帮助;

默认情况下,servlet在我们第一次访问的时候进行创建的,那么如何在服务器启动时候就创建呢?

web.xml servlet 标签中插入:

<load-on-startup>1</load-on-startup>

ServletContext对象:

1.服务器会为每个应用创建一个servletcontext 对象,用于各个servlet 的通讯;
2.servletcontext 对象是在服务器启动时候完成的创建
3.servletcontext 在服务器关闭时候销毁

如何在web.xml 中配置servlet context 初始化的参数?供全部servlet 共享呢?

<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/application-context.xml</param-value>
  </context-param>

这样我们就可以在servlet 中获取初始化参数了;

ServletContext servletContext = getServletContext();

System.err.println(servletContext.getInitParameter("contextConfigLocation"));

值得注意的是

<context-param></context-param> ---->初始化servletcontext 的;
<init-param></init-param> ------->初始化servlet 的

理解完这些,我们可以去看看spring mvc 源码,就 可以知道它的执行流程了。。

  

原创打造,多多指教
原文地址:https://www.cnblogs.com/iscys/p/9749116.html