Servlet接口
public interface Servlet {
//生命周期方法
public void init(ServletConfig config) throws ServletException;
public ServletConfig getServletConfig();
//生命周期方法
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
public String getServletInfo();
//生命周期方法
public void destroy();
}
ServletConfig 接口
public interface ServletConfig {
public ServletContext getServletContext();
public String getInitParameter(String name);
public Enumeration getInitParameterNames();
}
GenericServlet 抽象类实现了Servlet接口和ServletConfig接口
public abstract class GenericServlet
implements Servlet, ServletConfig, java.io.Serializable
{
private transient ServletConfig config;
public void destroy() {
}
//来自ServletConfig接口
public String getInitParameter(String name) {
return getServletConfig().getInitParameter(name);
}
//来自ServletConfig接口
public Enumeration getInitParameterNames() {
return getServletConfig().getInitParameterNames();
}
//来自Servlet接口
public ServletConfig getServletConfig() {
return config;
}
//来自ServletConfig接口
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
}
//servlet生命周期方法
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
//重载的init方法,不是servlet生命周期方法,我们要扩展HttpServlet的init方法时应该覆盖此方法而不是servlet生命周期方法
public void init() throws ServletException {
}
//核心方法,当servlet被调用时,执行此方法
public abstract void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
}
HttpServlet 是一个抽象类继承自GenericServlet
public abstract class HttpServlet extends GenericServlet
implements java.io.Serializable
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < (lastModified / 1000 * 1000)) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
}else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}
//加载时自动调用此方法
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
}
}
ServletRequest接口
public interface ServletRequest {
//根据属性名在request域中获取属性值
public Object getAttribute(String name);
//获取request域中所有的属性
public Enumeration getAttributeNames();
//获取请求时的编码方式
public String getCharacterEncoding();
//设置请求时的编码方式
public void setCharacterEncoding(String env) throws java.io.UnsupportedEncodingException;
//获取字节输入流
public ServletInputStream getInputStream() throws IOException;
//根据键获取参数中对应的值
public String getParameter(String name);
//获取请求参数的所有键(name)
public Enumeration getParameterNames();
//获取请求参数的所有值
public String[] getParameterValues(String name);
//获取请求参数的键-值 对应关系
public Map getParameterMap();
//获取请求所遵循的协议 一般为http
public String getProtocol();
//获取字符输入流
public BufferedReader getReader() throws IOException;
//向request域中添加属性
public void setAttribute(String name, Object o);
//在request域中移除属性
public void removeAttribute(String name);
//请求重定向
public RequestDispatcher getRequestDispatcher(String path);
//获取绝对路径
public String getRealPath(String path);
}
HttpServletRequest 接口
public interface HttpServletRequest extends ServletRequest {
//获取cookie数组
public Cookie[] getCookies();
//获取数据请求头
public long getDateHeader(String name);
//获取普通请求头
public String getHeader(String name);
//获取所有请求头
public Enumeration getHeaders(String name);
//获取所有请求头的名字
public Enumeration getHeaderNames();
//获取请求方法
public String getMethod();
//获取请求路径信息
public String getPathInfo();
//获取工程上下文路径
public String getContextPath();
//获取请求的URI
public String getRequestURI();
//获取请求的URL
public StringBuffer getRequestURL();
//获取配置文件
public String getServletPath();
//获取session 如果create为true,当session不存在时自动创建一个
public HttpSession getSession(boolean create);
//获取session
public HttpSession getSession();
}
ServletResponse 接口
public interface ServletResponse {
//获取响应编码
public String getCharacterEncoding();
//获取输出字节流
public ServletOutputStream getOutputStream() throws IOException;
//获取输出字符流
public PrintWriter getWriter() throws IOException;
//设置响应编码
public void setCharacterEncoding(String charset);
}
HttpServletResponse 接口
public interface HttpServletResponse extends ServletResponse {
//添加cookie
public void addCookie(Cookie cookie);
//判断是否含有这个响应头信息字段
public boolean containsHeader(String name);
public String encodeURL(String url);
public String encodeRedirectURL(String url);
public String encodeUrl(String url);
public String encodeRedirectUrl(String url);
//用来向客户端发送错误信息
public void sendError(int sc, String msg) throws IOException;
//用来向客户端发送错误信息
public void sendError(int sc) throws IOException;
//重定向
public void sendRedirect(String location) throws IOException;
//设置响应头(date)
public void setDateHeader(String name, long date);
//添加响应头(date)
public void addDateHeader(String name, long date);
//设置响应头
public void setHeader(String name, String value);
//添加响应头
public void addHeader(String name, String value);
//设置响应头(int)
public void setIntHeader(String name, int value);
//添加响应头(int)
public void addIntHeader(String name, int value);
//设置响应状态码
public void setStatus(int sc);
//设置响应状态码并设置响应的提示信息
public void setStatus(int sc, String sm);
//客户端无法连接
public static final int SC_CONTINUE = 100;
//服务器正向报头中注明的协议切换
public static final int SC_SWITCHING_PROTOCOLS = 101;
//请求被成功处理
public static final int SC_OK = 200;
//请求被成功处理,并在服务器方创建了一个新的资源
public static final int SC_CREATED = 201;
//请求正在被处理,但尚未完成
public static final int SC_ACCEPTED = 202;
//客户端所表达的mate信息并非来自服务器
public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
//请求被成功处理,但没有新的信息返回
public static final int SC_NO_CONTENT = 204;
//导致请求被发送的文档视图应该重置
public static final int SC_RESET_CONTENT = 205;
//服务器已经完成对资源的GET请求
public static final int SC_PARTIAL_CONTENT = 206;
//对应于一系列表述的被请求资源都有明确的位置
public static final int SC_MULTIPLE_CHOICES = 300;
//请求所申请的资源已经被移到一个新的地方,并且将来的参考点在请求中应当使用一个新的URL
public static final int SC_MOVED_PERMANENTLY = 301;
//请求所申请的资源已经被移到一个新的地方,并且将来的参考点在请求中仍使用原来的URL
public static final int SC_MOVED_TEMPORARILY = 302;
//请求所申请的资源已经被移到一个新的地方,并且将来的参考点在请求中仍使用原来的URL(HTTP1.1 推荐使用)
public static final int SC_FOUND = 302;
//请求的响应可以在一个不同的URL中找到
public static final int SC_SEE_OTHER = 303;
//一个有条件的GET操作发现资源可以利用,且没有被改变
public static final int SC_NOT_MODIFIED = 304;
//被请求的资源必须通过特定位置的代理来访问
public static final int SC_USE_PROXY = 305;
//访问的url临时跳转到了另外的一个url上
public static final int SC_TEMPORARY_REDIRECT = 307;
//客户发出的请求句法不正确
public static final int SC_BAD_REQUEST = 400;
//请求HTTP认证
public static final int SC_UNAUTHORIZED = 401;
//为以后的使用保留
public static final int SC_PAYMENT_REQUIRED = 402;
//服务器明白客户的请求,但拒绝响应
public static final int SC_FORBIDDEN = 403;
//所请求的资源不可用
public static final int SC_NOT_FOUND = 404;
//在请求行中标示的方法不允许对请求URL所标明的资源使用
public static final int SC_METHOD_NOT_ALLOWED = 405;
//被请求的资源只能响应实体,而且此符合请求所发送的可接受头部域的实体的确包含不可接受的内容
public static final int SC_NOT_ACCEPTABLE = 406;
//客户端必须先向代理验证
public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
//请求超时
public static final int SC_REQUEST_TIMEOUT = 408;
//通常与PUT请求一同使用常被用于试图上传版本不正确的文件时
public static final int SC_CONFLICT = 409;
//已经不存在
public static final int SC_GONE = 410;
//服务器不能处理请求(假设为带有附件的POST请求),除非客户端发送Content-Length头信息指出发送给服务器的数据的大小。该状态是新加入 HTTP 1.1的
public static final int SC_LENGTH_REQUIRED = 411;
//请求头信息中的某些先决条件是错误的。该状态是新加入 HTTP 1.1的
public static final int SC_PRECONDITION_FAILED = 412;
//现在所请求的文档比服务器现在想要处理的要大。如果服务器认为能够过一段时间处理,则会包含一个Retry-After的响应头信息。该状态是新加入 HTTP 1.1的
public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;
//请求URI过长
public static final int SC_REQUEST_URI_TOO_LONG = 414;
//请求所带的附件的格式类型服务器不知道如何处理。该状态是新加入 HTTP 1.1的
public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
//客户端包含了一个服务器无法满足的Range头信息的请求
public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
//如果服务器得到一个带有100-continue值的Expect请求头信息,这是指客户端正在询问是否可以在后面的请求中发送附件。在这种情况下,服务器也会用该状态(417)告诉浏览器服务器不接收该附件或用100 (SC_CONTINUE)状态告诉客户端可以继续发送附件
public static final int SC_EXPECTATION_FAILED = 417;
//内部服务器错误
public static final int SC_INTERNAL_SERVER_ERROR = 500;
//服务器不支持请求中要求的功能
public static final int SC_NOT_IMPLEMENTED = 501;
//错误的网关
public static final int SC_BAD_GATEWAY = 502;
//服务无法获得
public static final int SC_SERVICE_UNAVAILABLE = 503;
//网关超时
public static final int SC_GATEWAY_TIMEOUT = 504;
//服务器并不支持在请求中所标明 HTTP 版本
public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
}