Servlet是运行在Web服务器上的Java组件,用于处理客户端请求并生成响应。下面将介绍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的描述信息。
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");
}
}
ServletRequest接口代表HTTP请求,定义了获取请求信息的方法。以下是常用的ServletRequest方法:
public interface ServletRequest {
...
public String getParameter(String name);
public Map getParameterMap();
public String[] getParameterValues(String name);
public Enumeration getParameterNames();
public String getHeader(String name);
public Enumeration getHeaderNames();
public String getMethod();
public HttpSession getSession();
...
}
getParameter方法获取请求参数的值,getParameterMap方法获取所有请求参数的映射,getParameterValues方法获取指定参数名的所有值,getParameterNames方法获取所有请求参数名的枚举,getHeader方法获取指定头部信息的值,getHeaderNames方法获取所有头部信息的枚举,getMethod方法获取请求方法(GET、POST等),getSession方法获取HttpSession对象。
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方法设置响应内容长度。