看了servlet源码后一点总结
这几天上班时间(
)没什么别的大事,就自己看了一下servlet的api,已经把源码放进项目里,边看api边看源码,自己写一点心得体会,也是对这个的一些小总结,希望让自己有点提升。废话不多说开始写。
servlet属于服务器端的程序,也就是说它的职责是充当客户请求和服务器响应的中间层。servlet的生命周期一般是首先装载servlet,也就是启动服务的时候,服务器端调用servlet的init()方法进行初始。当一个客户端请求到达服务器,服务器首先先生成一个请求对象(ServletRequest)和一个响应对象(ServletResponse),然后服务激活servlet的service()方法,将刚刚的请求对象和响应对象进行传递,通过请求对象,对对象的信息,请求的资源进行封装,再通过服务端将响应对象传递回客户端。一般servlet只需要创建一次就可以(也就是只调用一次init()方法),当不再需要servlet的时候就调用destory()方法销毁servlet对象。servlet里的接口Servlet正是各种Servlet的接口,也就是说,所有的Servlet都必须实现这个接口。
public abstract interface Servlet {public abstract void init(ServletConfig paramServletConfig)throws javax.servlet.ServletException;public abstract ServletConfig getServletConfig();public abstract void service(ServletRequest paramServletRequest,ServletResponse paramServletResponse)throws javax.servlet.ServletException, IOException;public abstract String getServletInfo();public abstract void destroy();}public abstract class GenericServlet implements Servlet, ServletConfig,Serializable {private transient ServletConfig config;public String getInitParameter(String name) {return getServletConfig().getInitParameter(name);}public Enumeration<String> getInitParameterNames() {return getServletConfig().getInitParameterNames();}public ServletConfig getServletConfig() {return this.config;}public ServletContext getServletContext() {return getServletConfig().getServletContext();}public void init(ServletConfig config)throws javax.servlet.ServletException {this.config = config;init();}public abstract void service(ServletRequest paramServletRequest,ServletResponse paramServletResponse)throws javax.servlet.ServletException, IOException;public String getServletName() {return this.config.getServletName();}}public abstract interface ServletConfig{ public abstract String getServletName(); public abstract ServletContext getServletContext(); public abstract String getInitParameter(String paramString); public abstract Enumeration<String> getInitParameterNames();}public abstract interface ServletContext {public abstract ServletContext getContext(String paramString);public abstract String getContextPath();public abstract String getMimeType(String paramString);public abstract Set<String> getResourcePaths(String paramString);public abstract URL getResource(String paramString)throws MalformedURLException;public abstract InputStream getResourceAsStream(String paramString);public abstract RequestDispatcher getRequestDispatcher(String paramString);public abstract RequestDispatcher getNamedDispatcher(String paramString);public abstract void log(String paramString);public abstract void addListener(String paramString);public abstract <T extends EventListener> T createListener(Class<T> paramClass) throws javax.servlet.ServletException;public abstract ClassLoader getClassLoader();public abstract JspConfigDescriptor getJspConfigDescriptor();}public abstract interface RequestDispatcher{ /**省略很多字符常量 **/ /** * 将Servlet中的一个请求转交给服务器上的另一个资源(例如 sevlet、JSP文件或HTML文件), 方法允许Servlet在生成响应之前对请求和被转发到的那个资源进行预处理. * @param paramServletRequest * @param paramServletResponse * @throws javax.servlet.ServletException * @throws IOException */ public abstract void forward(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws javax.servlet.ServletException, IOException; /** * 将资源(例如 servlet、JSP页面或HTML文件)内容包含到响应流中. 实质上方法默认启用了服务端按队列包含文件. * @param paramServletRequest * @param paramServletResponse * @throws javax.servlet.ServletException * @throws IOException */ public abstract void include(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws javax.servlet.ServletException, IOException;}public abstract class HttpServlet extends GenericServlet { /** * 继承HttpServlet的所有子类,必须重写一个方法,通常是以下四个之一 * @param req * @param resp * @throws ServletException * @throws IOException */ 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(405, msg);elseresp.sendError(400, msg);}protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {}protected void doPut(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {}protected void doDelete(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {}} 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);} protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {long lastModified;String method = req.getMethod();if (method.equals("GET")) {lastModified = getLastModified(req);if (lastModified == -1L) {doGet(req, resp);} else {long ifModifiedSince = req.getDateHeader("If-Modified-Since");if (ifModifiedSince < lastModified / 1000L * 1000L) {maybeSetLastModified(resp, lastModified);doGet(req, resp);} else {resp.setStatus(304);}}} else if (method.equals("HEAD")) {lastModified = getLastModified(req);maybeSetLastModified(resp, lastModified);doHead(req, resp);} else if (method.equals("POST")) {doPost(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 {String errMsg = lStrings.getString("http.method_not_implemented");Object[] errArgs = new Object[1];errArgs[0] = method;errMsg = MessageFormat.format(errMsg, errArgs);resp.sendError(501, errMsg);}}public abstract interface ServletRequest{ public abstract Object getAttribute(String paramString); public abstract Enumeration<String> getAttributeNames(); public abstract String getCharacterEncoding(); public abstract void setCharacterEncoding(String paramString) throws UnsupportedEncodingException; public abstract int getContentLength(); public abstract String getContentType(); public abstract ServletInputStream getInputStream() throws IOException; public abstract String getProtocol(); public abstract String getScheme(); public abstract String getServerName(); public abstract int getServerPort(); public abstract BufferedReader getReader() throws IOException; public abstract String getRemoteAddr(); public abstract String getRemoteHost(); .......public abstract interface ServletResponse{ public abstract String getCharacterEncoding(); public abstract String getContentType(); public abstract ServletOutputStream getOutputStream() throws IOException; public abstract PrintWriter getWriter() throws IOException; public abstract void setCharacterEncoding(String paramString); public abstract void setContentLength(int paramInt); public abstract void setContentType(String paramString); public abstract void setBufferSize(int paramInt); public abstract int getBufferSize(); public abstract void flushBuffer() throws IOException; public abstract void resetBuffer(); public abstract boolean isCommitted(); public abstract void reset(); public abstract void setLocale(Locale paramLocale); public abstract Locale getLocale();}public abstract interface Filter {public abstract void init(FilterConfig paramFilterConfig)throws javax.servlet.ServletException;public abstract void doFilter(ServletRequest paramServletRequest,ServletResponse paramServletResponse, FilterChain paramFilterChain)throws IOException, javax.servlet.ServletException;public abstract void destroy();}