简述Servlet生命周期

Servlet生命周期的三个核心方法分别是 init() , service() 和 destroy()。每一个Servlet都会实现这些方法,而且在特定的运行时间调用它们。html

1)在Servlet生命周期的初始化阶段,web容器经过调用init()方法来初始化Servlet实例,而且能够传递一个实现 javax.servlet.ServletConfig 接口的对象给它。这个配置对象(configuration object)使Servlet可以读取在web应用的web.xml文件里定义的名值(name-value)初始参数。这个方法在Servlet实例的生命周期里只调用一次。java

2)初始化后,Servlet实例就能够处理客户端请求了。web容器调用Servlet的service()方法来处理每个请求。service() 方法定义了可以处理的请求类型而且调用适当方法来处理这些请求。编写Servlet的开发者必须为这些方法提供实现。若是发出一个Servlet没实现的请求,那么父类的方法就会被调用而且一般会给请求方(requester)返回一个错误信息。web

一般,咱们不须要重写(override)这个方法。网络

3)最后,web容器调用destroy()方法来终结Servlet。若是你想在Servlet的生命周期内关闭或者销毁一些文件系统或者网络资源,你能够调用这个方法来实现。destroy() 方法和init()方法同样,在Servlet的生命周期里只能调用一次。ide

 

以下例子post

package com.wxp.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;this

public class MyFirstServlet extends HttpServlet {
    /**
     * Constructor of the object.
     */
    public MyFirstServlet() {
        super();
    }.net

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }code

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {orm

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println("  <HEAD><TITLE>MyFirstServlet</TITLE></HEAD>");
            out.println("  <BODY>");
            out.print("Servlet MyFirstServlet at");
            out.print(request.getContextPath());
            out.println("  </BODY>");
            out.println("</HTML>");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            out.flush();
            out.close();
        }
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         this.doGet(request, response);
    }

    /**      * Initialization of the servlet. <br>      *      * @throws ServletException if an error occurs      */     public void init() throws ServletException {         // Put your code here     }     public String getServletInfo(){         return "MyFirstServlet";     } }  

相关文章
相关标签/搜索