servlet学习笔记

2019-11-09  本文已影响0人  付振南

title: servlet学习笔记
date: 2019-11-07 21:01:05
tags:


servlet学习笔记

servlet介绍

A servlet is a small Java program that runs within a Web server.

入门:

The servlet is constructed, then initialized with the init method.

Any calls from clients to the service method are handled.

The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

servlet执行原理

servlet的五个抽象方法

void init(ServletConfig config)
throws ServletException
Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.

The servlet container cannot place the servlet into service if the init method

Throws a ServletException
Does not return within a time period defined by the Web server

ServletConfig getServletConfig()
Returns a ServletConfig object, which contains initialization and startup parameters for this servlet. The ServletConfig object returned is the one passed to the init method.
Implementations of this interface are responsible for storing the ServletConfig object so that this method can return it. The GenericServlet class, which implements this interface, already does this.

Called by the servlet container to allow the servlet to respond to a request.
This method is only called after the servlet's init() method has completed successfully.

The status code of the response always should be set for a servlet that throws or sends an error.

Servlets typically run inside multithreaded servlet containers that can handle multiple requests concurrently. Developers must be aware to synchronize access to any shared resources such as files, network connections, and as well as the servlet's class and instance variables. More information on multithreaded programming in Java is available in the Java tutorial on multi-threaded programming.

Returns information about the servlet, such as author, version, and copyright.
The string that this method returns should be plain text and not markup of any kind (such as HTML, XML, etc.).

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

servlet的生命周期

被创建

指定servlet的创建时机
1.默认情况下,第一次被访问时创建
即<load-on-startup>值为负整数,默认值为-1
2.在tomcat服务器启动时就创建
即<load-on-startup>值为>=0的整数,从0开始的整数中,越往后优先级越低,0的优先级最高

提供服务

被销毁

servlet3.0后的注解配置

servlet3.0之后,用户不需要手动配置web.xml文件,可以通过注解的方式去配置。

public @interface WebServlet {
    String name() default "";

    String[] value() default {};

    String[] urlPatterns() default {};

    int loadOnStartup() default -1;

    WebInitParam[] initParams() default {};

    boolean asyncSupported() default false;

    String smallIcon() default "";

    String largeIcon() default "";

    String description() default "";

    String displayName() default "";
}  

@WebServlet(urlPatterns="/demo1")

@WebServlet("/demo1")

servlet体系结构

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
    private static final long serialVersionUID = 1L;
    private transient ServletConfig config;

    public GenericServlet() {
    }

    public void destroy() {
    }

    public ServletConfig getServletConfig() {
        return this.config;
    }

    public String getServletInfo() {
        return "";
    }

    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }

    public void init() throws ServletException {
    }

    public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
}

HttpServlet是对http协议进行了封装,然后重写了service方法。

上一篇下一篇

猜你喜欢

热点阅读