SMOKEN 发布的文章

JDK自带的HttpURLConnection,默认启用keepAlive,支持HTTP / 1.1和HTTP / 1.0持久连接, 使用后的HttpURLConnection会放入缓存中供以后的同host:port的请求重用,底层的socket在keepAlive超时之前不会关闭。

HttpURLConnection受以下system properties控制:
http.keepAlive=<boolean>(默认值:true),是否启用keepAlive,如果设置为false,则HttpURLConnection不会缓存,使用完后会关闭socket连接。
http.maxConnections=<int>(默认值:5),每个目标host缓存socket连接的最大数。

- 阅读剩余部分 -

servlet、filter、listener分别定义2个,代码如下:

public class GlobalServlet extends HttpServlet {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        logger.info("init");
    }
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("service");
        PrintWriter pw = resp.getWriter();
        pw.write("This is "+this.getClass().getSimpleName());
        pw.close();
    }
    
    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class LoginServlet extends HttpServlet {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        logger.info("init");
    }
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("service");
        PrintWriter pw = resp.getWriter();
        pw.write("This is "+this.getClass().getSimpleName());
        pw.close();
    }
    
    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class GlobalFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        logger.info("start doFilter");
        chain.doFilter(request, response);
        logger.info("end doFilter");
    }

    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class LoginFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        logger.info("start doFilter");
        chain.doFilter(request, response);
        logger.info("end doFilter");
    }

    @Override
    public void destroy() {
        logger.info("destroy");
    }
}

public class ContextListener1 implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("contextDestroyed");
    }
}

public class ContextListener2 implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("contextDestroyed");
    }
}

- 阅读剩余部分 -