servlet详解及Tomcat中多线程下的Servlet(1)

servlet作为java web开发举足轻重的东西,在此总结一下对Servlet的学习(jsp也是servlet)



一。servlet生命周期


加载—>实例化—>服务—>销毁
加载由web服务器容器完成。

  • init():在Servlet的生命周期中,仅执行一次init()方法。它是在服务器装入Servlet时执行的,负责初始化Servlet对象。可以配置服务器,以在启动服务器或客户机首次访问Servlet时装入Servlet。无论有多少客户机访问Servlet,都不会重复执行init()。
  • service():它是Servlet的核心,负责响应客户的请求。每当一个客户请求一个HttpServlet对象,该对象的Service()方法就要调用,而且传递给这个方法一个“请求”(ServletRequest)对象和一个“响应”(ServletResponse)对象作为参数。在HttpServlet中已存在Service()方法。默认的服务功能是调用与HTTP请求的方法相应的do功能。
  • destroy(): 仅执行一次,在服务器端停止且卸载Servlet时执行该方法。当Servlet对象退出生命周期时,负责释放占用的资源。一个Servlet在运行service()方法时可能会产生其他的线程,因此需要确认在调用destroy()方法时,这些线程已经终止或完成。

service()源码如下(很简单,,几个if语句,,doGet比较特殊,需要判断传入内容容量大小):

    /**
     * Receives standard HTTP requests from the public
     * service method and dispatches
     * them to the doMethod methods defined in
     * this class. This method is an HTTP-specific version of the
     * {@link javax.servlet.Servlet#service} method. There's no
     * need to override this method.
     *
     * @param req   the {@link HttpServletRequest} object that
     *                  contains the request the client made of
     *                  the servlet
     *
     * @param resp  the {@link HttpServletResponse} object that
     *                  contains the response the servlet returns
     *                  to the client
     *
     * @exception IOException   if an input or output error occurs
     *                              while the servlet is handling the
     *                              HTTP request
     *
     * @exception ServletException  if the HTTP request
     *                                  cannot be handled
     *
     * @see javax.servlet.Servlet#service
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String method = req.getMethod();

        if (method.equals(METHOD_GET)) {
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } catch (IllegalArgumentException iae) {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }

        } else if (method.equals(METHOD_HEAD)) {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);

        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);

        } else if (method.equals(METHOD_PUT)) {
            doPut(req, resp);

        } else if (method.equals(METHOD_DELETE)) {
            doDelete(req, resp);

        } else if (method.equals(METHOD_OPTIONS)) {
            doOptions(req,resp);

        } else if (method.equals(METHOD_TRACE)) {
            doTrace(req,resp);

        } else {
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //

            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);

            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }



二。Tomcat中的Servlet


tomcat中servlet运行时的时序图:
servlet详解及Tomcat中多线程下的Servlet(1)_第1张图片



三。Servlet是单例的

在web访问中,每多一个用户,会在web容器中多一个用户线程。
多用户同时访问某个servlet的 doget方法时(在doget方法未使用字段,也就是该方法不依赖外界或者不更改外界变量),此时由于方法的执行在线程中是私有的,每个线程执行某个方法不影响。

于是乎,servlet是单例的,经过测试,init()方法只在servlet加载时执行,destroy()只在销毁时执行。

这样看来,servlet单例就够了。



四。Servlet测试,

测试servlet代码,TestThreadServlet.java:

package xatu.zsl.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zsl on 2017/8/7.
 */
@WebServlet("/TestThreadServlet")
public class TestThreadServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("执行了 init()");
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("doGet请求,线程名:" + Thread.currentThread().getName());
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行完毕!!   doGet请求,线程名:" + Thread.currentThread().getName());
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    public void destroy() {
        System.out.println("执行了 destory()");
        super.destroy();
    }
}

doGet中做耗时操作,,,并打印当前线程名,
结果如下:
servlet详解及Tomcat中多线程下的Servlet(1)_第2张图片

当然最后sotp,服务器关闭执行:
servlet详解及Tomcat中多线程下的Servlet(1)_第3张图片

注意:未做耗时操作时,结果如下图,:
servlet详解及Tomcat中多线程下的Servlet(1)_第4张图片



注意:只有十个线程,但是上面做耗时操作的却有多个线程。由此可见tomcat 自带线程池机制,默认创建了10个线程(当然可调)



不懂池机制??那怎么行,,, 池机制详解

你可能感兴趣的:(框架学习)