Java源码-servlet源码解析

本文详细解析了Servlet接口及其子接口HttpServlet的源码,包括初始化、服务方法、请求方法处理以及ServletRequest和HttpServletResponse接口的功能。重点介绍了如何处理HTTP请求和响应,以及如何设置响应信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Servlet是运行在Web服务器上的Java组件,用于处理客户端请求并生成响应。下面将介绍Servlet的源码解析。

  1. Servlet接口源码解析

Servlet接口是所有Servlet类必须实现的接口。该接口定义了Servlet生命周期方法和服务方法。

public interface Servlet {
    public void init(ServletConfig config) throws ServletException;
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
    public void destroy();
    public ServletConfig getServletConfig();
    public String getServletInfo();
}

init方法初始化Servlet,service方法处理请求并生成响应。destroy方法销毁Servlet。getServletConfig方法返回ServletConfig对象,该对象包含Servlet的配置信息。getServletInfo方法返回Servlet的描述信息。

  1. HttpServlet类源码解析

HttpServlet类是Servlet接口的子接口,专门用于处理HTTP请求和响应。HttpServletRequest和HttpServletResponse类分别代表HTTP请求和响应。HttpServlet类重写了service方法,根据请求方法(GET、POST等)调用对应的doXXX方法处理请求。以下是HttpServlet类的源码:

public abstract class HttpServlet extends GenericServlet {
    ...
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String method = req.getMethod();
        if (method.equals("GET")) {
            doGet(req, resp);
        } else if (method.equals("POST")) {
            doPost(req, resp);
        } else if (method.equals("HEAD")) {
            doHead(req, resp);
        } else if (method.equals("PUT")) {
            doPut(req, resp);
        } else if (method.equals("DELETE")) {
            doDelete(req, resp);
        } else if (method.equals("OPTIONS")) {
            doOptions(req, resp);
        } else if (method.equals("TRACE")) {
            doTrace(req, resp);
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "Not supported");
        }
    }
    ...
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "GET method not supported");
    }
    ...
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "POST method not supported");
    }
    ...
    protected void doHead(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "HEAD method not supported");
    }
    ...
    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "PUT method not supported");
    }
    ...
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "DELETE method not supported");
    }
    ...
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "OPTIONS method not supported");
    }
    ...
    protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "TRACE method not supported");
    }
}

  1. ServletRequest接口源码解析

ServletRequest接口代表HTTP请求,定义了获取请求信息的方法。以下是常用的ServletRequest方法:

public interface ServletRequest {
    ...
    public String getParameter(String name);
    public Map<String, String[]> getParameterMap();
    public String[] getParameterValues(String name);
    public Enumeration<String> getParameterNames();
    public String getHeader(String name);
    public Enumeration<String> getHeaderNames();
    public String getMethod();
    public HttpSession getSession();
    ...
}

getParameter方法获取请求参数的值,getParameterMap方法获取所有请求参数的映射,getParameterValues方法获取指定参数名的所有值,getParameterNames方法获取所有请求参数名的枚举,getHeader方法获取指定头部信息的值,getHeaderNames方法获取所有头部信息的枚举,getMethod方法获取请求方法(GET、POST等),getSession方法获取HttpSession对象。

  1. HttpServletResponse接口源码解析

HttpServletResponse接口代表HTTP响应,定义了设置响应信息的方法。以下是常用的HttpServletResponse方法:

public interface HttpServletResponse {
    ...
    public void setContentType(String type);
    public void addHeader(String name, String value);
    public void sendRedirect(String location) throws IOException;
    public void setStatus(int sc);
    public void sendError(int sc, String msg) throws IOException;
    public PrintWriter getWriter() throws IOException;
    public void setContentLength(int len);
    ...
}

setContentType方法设置响应内容类型,addHeader方法添加响应头部信息,sendRedirect方法重定向到指定URL,setStatus方法设置响应状态码,sendError方法发送错误响应,getWriter方法获取输出流,setContentLength方法设置响应内容长度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值