Bootstrap

javaweb之Servlet

Servlet,javaweb的基础之一。

主要作用是处理浏览器的请求。

1.接收请求 ServletRequest

2.处理请求 编码

3.响应请求 ServetResponse

Servlet接口:

public interface Servlet {
    //生命周期方法,初始化,在Servlet对象创建后执行,仅执行一次
    void init(ServletConfig var1) throws ServletException;
    //获取Servlet配置信息
    ServletConfig getServletConfig();
    //生命周期方法,请求,处理,响应
    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
    //获取Servlet信息
    String getServletInfo();
    //生命周期方法,对象销毁前执行,仅执行一次
    void destroy();
}

GenericServlet抽象类:

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
    //Servlet配置
    private transient ServletConfig config;
    //获取指定name初始参数value
    public String getInitParameter(String name) {
        return this.getServletConfig().getInitParameter(name);
    }
    //获取Servlet配置
    public ServletConfig getServletConfig() {
        return this.config;
    }
    //获取Servlet上下文
    public ServletContext getServletContext() {
        return this.getServletConfig().getServletContext();
    }
    //初始化,由Tomcat注入Servlet配置信息
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        //调用本类方法
        this.init();
    }
    //重点方法,自定义Servlet类写此方法,而不是上一方法
    public void init() throws ServletException {
    } 
    //获取Servlet名称
    public String getServletName() {
        return this.config.getServletName();
    }
}

HttpServlet对象

public abstract class HttpServlet extends GenericServlet {
    public void service(ServletRequest req, ServletResponse res) throws ServletException, 
    IOException {
        HttpServletRequest request;
        HttpServletResponse response;
        try {
            //将请求和响应参数强制转为Http类型的请求和响应参数
            request = (HttpServletRequest)req;
            response = (HttpServletResponse)res;
        } catch (ClassCastE
;