Servlet源码学习笔记

标题 Servlet 接口源码

public interface Servlet {

	//Servlet第一次被请求时,初始化一个Servlet对象
	void init(ServletConfig var1) throws ServletException;
	
	//Servlet的配置对象
	ServletConfig getServletConfig();
 
	//每当请求Servlet时,Servlet容器就会调用这个方法
	void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
 
	//获取Servlet的一些信息,版本,作者等等...
	String getServletInfo();
	
	//当要销毁Servlet时,Servlet容器就会调用这个方法
	void destroy();
}

ServletRequset接口源码

public interface ServletRequest {
 
	//返回请求主体的字节数
	int getContentLength();
 
	//返回主体的MIME类型
	String getContentType();
 
	//返回请求参数的值
	String getParameter(String var1);
 
}

ServletResponse接口源码

public interface ServletResponse {

	String getCharacterEncoding();
 
	String getContentType();
	
	//向浏览器发送数据,用来发送二进制数据的
	ServletOutputStream getOutputStream() throws IOException;
	
	/*
	  返回了一个可以向客户端发送文本的的Java.io.PrintWriter对象,默认情况下,PrintWriter对象使用ISO-8859-1编码
	  默认情况下,PrintWriter对象使用ISO-8859-1编码(该编码在输入中文时会发生乱码)
	  在向客户端发送响应时,大多数都是使用该对象向客户端发送HTML。
	*/
	PrintWriter getWriter() throws IOException;
 
	void setCharacterEncoding(String var1);
 
	void setContentLength(int var1);
	
	/*
	  在发送任何HTML之前,应该先调用该方法。
	  设置响应的内容类型,并将“text/html”作为一个参数传入
	*/
	void setContentType(String var1);
 
	void setBufferSize(int var1);
 
	int getBufferSize();
 
	void flushBuffer() throws IOException;
 
	void resetBuffer();
 
	boolean isCommitted();
 
	void reset();
 
	void setLocale(Locale var1);
 
	Locale getLocale();
}

ServletConfig接口源码

public interface ServletConfig {

	//获得Servlet在web.xml中配置的name的值
	String getServletName();

	ServletContext getServletContext();
	
	//获得Servlet的初始化参数的
	String getInitParameter(java.lang.String s);
	
	//获得所有Servlet的初始化参数的名称
	Enumeration<java.lang.String> getInitParameterNames();
}

ServletContext对象源码

ServletContext中的下列方法负责处理属性:

	Object getAttribute(String var1);
	 
	Enumeration<String> getAttributeNames();
	 
	void setAttribute(String var1, Object var2);
	 
	void removeAttribute(String var1);

GenericServlet抽象类源码

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
	private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
	private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");
	private transient ServletConfig config;
 
	public GenericServlet() {
	}
 
	public void destroy() {
	}
 
	public String getInitParameter(String name) {
		ServletConfig sc = this.getServletConfig();
		if (sc == null) {
			throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
		} else {
			return sc.getInitParameter(name);
		}
	}
 
	public Enumeration<String> getInitParameterNames() {
		ServletConfig sc = this.getServletConfig();
		if (sc == null) {
			throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
		} else {
			return sc.getInitParameterNames();
		}
	}
 
	public ServletConfig getServletConfig() {
		return this.config;
	}
 
	public ServletContext getServletContext() {
		ServletConfig sc = this.getServletConfig();
		if (sc == null) {
			throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
		} else {
			return sc.getServletContext();
		}
	}
 
	public String getServletInfo() {
		return "";
	}
 
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
		this.init();
	}
	
	//当程序员如果需要覆盖这个GenericServlet的初始化方法,则只需要覆盖不带参数的init( )方法就好了
	public void init() throws ServletException {
	}
 
	public void log(String msg) {
		this.getServletContext().log(this.getServletName() + ": " + msg);
	}
 
	public void log(String message, Throwable t) {
		this.getServletContext().log(this.getServletName() + ": " + message, t);
	}
 
	public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
 
	public String getServletName() {
		ServletConfig sc = this.getServletConfig();
		if (sc == null) {
			throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
		} else {
			return sc.getServletName();
		}
	}
}

HttpServlet抽象类源码

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
	HttpServletRequest request;
	HttpServletResponse response;
	try {
		request = (HttpServletRequest)req;
		response = (HttpServletResponse)res;
	} catch (ClassCastException var6) {
		throw new ServletException("non-HTTP request or response");
	}
 
	this.service(request, response);
}

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	String method = req.getMethod();
	long lastModified;
	if (method.equals("GET")) {
		lastModified = this.getLastModified(req);
		if (lastModified == -1L) {
			this.doGet(req, resp);
		} else {
			long ifModifiedSince = req.getDateHeader("If-Modified-Since");
			if (ifModifiedSince < lastModified) {
				this.maybeSetLastModified(resp, lastModified);
				this.doGet(req, resp);
			} else {
				resp.setStatus(304);
			}
		}
	} else if (method.equals("HEAD")) {
		lastModified = this.getLastModified(req);
		this.maybeSetLastModified(resp, lastModified);
		this.doHead(req, resp);
	} else if (method.equals("POST")) {
		this.doPost(req, resp);
	} else if (method.equals("PUT")) {
		this.doPut(req, resp);
	} else if (method.equals("DELETE")) {
		this.doDelete(req, resp);
	} else if (method.equals("OPTIONS")) {
		this.doOptions(req, resp);
	} else if (method.equals("TRACE")) {
		this.doTrace(req, resp);
	} else {
		String errMsg = lStrings.getString("http.method_not_implemented");
		Object[] errArgs = new Object[]{method};
		errMsg = MessageFormat.format(errMsg, errArgs);
		resp.sendError(501, errMsg);
	}
}

HttpServletRequest接口源码

HttpServletRequest表示Http环境中的Servlet请求,它扩展于javax.servlet.ServletRequest接口,并添加了几个方法。

public interface HttpServletRequest extends ServletRequest {
	String BASIC_AUTH = "BASIC";
	String FORM_AUTH = "FORM";
	String CLIENT_CERT_AUTH = "CLIENT_CERT";
	String DIGEST_AUTH = "DIGEST";

	String getAuthType();
	
	//返回一个cookie对象数组
	Cookie[] getCookies();

	long getDateHeader(String s);
	
	//返回指定HTTP标题的值
	String getHeader(String s);

	Enumeration<String> getHeaders(String s);

	Enumeration<String> getHeaderNames();

	int getIntHeader(String s);
	
	//返回生成这个请求HTTP的方法名称
	String getMethod();

	String getPathInfo();

	String getPathTranslated();
	
	//返回请求上下文的请求URI部分,web应用的名称
	String getContextPath();

	//返回请求URL中的查询字符串,get提交url地址后的参数字符串
	String getQueryString();

	String getRemoteUser();

	boolean isUserInRole(String s);

	Principal getUserPrincipal();

	String getRequestedSessionId();

	String getRequestURI();

	StringBuffer getRequestURL();

	String getServletPath();

	HttpSession getSession(boolean b);

	//返回与这个请求相关的会话对象
	HttpSession getSession();

	String changeSessionId();

	boolean isRequestedSessionIdValid();

	boolean isRequestedSessionIdFromCookie();

	boolean isRequestedSessionIdFromURL();

	/**
	 * @deprecated
	 */
	boolean isRequestedSessionIdFromUrl();

	boolean authenticate(HttpServletResponse httpServletResponse) throws IOException, ServletException;

	void login(String s, String s1) throws ServletException;

	void logout() throws ServletException;

	Collection<Part> getParts() throws IOException, ServletException;

	Part getPart(String s) throws IOException, ServletException;

	<T extends HttpUpgradeHandler> T upgrade(Class<T> aClass) throws IOException, ServletException;
}

HttpServletResponse接口源码

public interface HttpServletResponse extends ServletResponse {
	int SC_CONTINUE = 100;
	int SC_SWITCHING_PROTOCOLS = 101;
	int SC_OK = 200;
	int SC_CREATED = 201;
	int SC_ACCEPTED = 202;
	int SC_NON_AUTHORITATIVE_INFORMATION = 203;
	int SC_NO_CONTENT = 204;
	int SC_RESET_CONTENT = 205;
	int SC_PARTIAL_CONTENT = 206;
	int SC_MULTIPLE_CHOICES = 300;
	int SC_MOVED_PERMANENTLY = 301;
	int SC_MOVED_TEMPORARILY = 302;
	int SC_FOUND = 302;
	int SC_SEE_OTHER = 303;
	int SC_NOT_MODIFIED = 304;
	int SC_USE_PROXY = 305;
	int SC_TEMPORARY_REDIRECT = 307;
	int SC_BAD_REQUEST = 400;
	int SC_UNAUTHORIZED = 401;
	int SC_PAYMENT_REQUIRED = 402;
	int SC_FORBIDDEN = 403;
	int SC_NOT_FOUND = 404;
	int SC_METHOD_NOT_ALLOWED = 405;
	int SC_NOT_ACCEPTABLE = 406;
	int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
	int SC_REQUEST_TIMEOUT = 408;
	int SC_CONFLICT = 409;
	int SC_GONE = 410;
	int SC_LENGTH_REQUIRED = 411;
	int SC_PRECONDITION_FAILED = 412;
	int SC_REQUEST_ENTITY_TOO_LARGE = 413;
	int SC_REQUEST_URI_TOO_LONG = 414;
	int SC_UNSUPPORTED_MEDIA_TYPE = 415;
	int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
	int SC_EXPECTATION_FAILED = 417;
	int SC_INTERNAL_SERVER_ERROR = 500;
	int SC_NOT_IMPLEMENTED = 501;
	int SC_BAD_GATEWAY = 502;
	int SC_SERVICE_UNAVAILABLE = 503;
	int SC_GATEWAY_TIMEOUT = 504;
	int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
	
	//给这个响应添加一个cookie
	void addCookie(Cookie cookie);

	boolean containsHeader(String s);

	String encodeURL(String s);

	String encodeRedirectURL(String s);

	/**
	 * @deprecated
	 */
	String encodeUrl(String s);

	/**
	 * @deprecated
	 */
	String encodeRedirectUrl(String s);

	void sendError(int i, String s) throws IOException;

	void sendError(int i) throws IOException;

	//发送一条响应码,讲浏览器跳转到指定的位置
	void sendRedirect(String s) throws IOException;

	void setDateHeader(String s, long l);

	void addDateHeader(String s, long l);

	void setHeader(String s, String s1);
	
	//给这个请求添加一个响应头
	void addHeader(String s, .String s1);

	void setIntHeader(String s, int i);

	void addIntHeader(String s, int i);
	
	//设置响应行的状态码
	void setStatus(int i);

	/**
	 * @deprecated
	 */
	void setStatus(int i, String s);

	int getStatus();

	String getHeader(String s);

	Collection<String> getHeaders(String s);

	Collection<String> getHeaderNames();
}

你可能感兴趣的:(Servlet源码学习笔记)