[10]运行的流程
1、浏览器发送请求到HttpSevr类调用HttpServ的service(servletRequest, servletReponse)方法
2、由于没有找到这个方法,去调用父类(HttpServlet) 的同名方法。
3、父类的service方法将ServletRequest req请求转换成HttpServletRequest请求,再去调用service(request, response) 方法。
将ServletRequest req请求转换成HttpServletRequest请求再调用service(request, response) 方法源码如下:
[11]HttpServlet
怎样才能实现一个servlet,方法有3种:
1、实现servlet接口
2、继承GenericServlet(实现service 方法
)
3、继承HttpServlet类(重写doGet,doPost方法就行)
servlet 接口源代码
public abstract interface Servlet { public abstract void init(ServletConfig paramServletConfig) throws ServletException; public abstract ServletConfig getServletConfig(); public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws ServletException, IOException; public abstract String getServletInfo(); public abstract void destroy(); }
GenericServlet源代码
package javax.servlet; import java.io.IOException; import java.io.Serializable; import java.util.Enumeration; import java.util.ResourceBundle; 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 void destroy() { } public String getInitParameter(String name) { ServletConfig sc = getServletConfig(); if (sc == null) { throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized")); } return sc.getInitParameter(name); } public Enumeration getInitParameterNames() { ServletConfig sc = getServletConfig(); if (sc == null) { throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized")); } return sc.getInitParameterNames(); } public ServletConfig getServletConfig() { return this.config; } public ServletContext getServletContext() { ServletConfig sc = getServletConfig(); if (sc == null) { throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized")); } return sc.getServletContext(); } public String getServletInfo() { return ""; } public void init(ServletConfig config) throws ServletException { this.config = config; init(); } public void init() throws ServletException { } public void log(String msg) { getServletContext().log(getServletName() + ": " + msg); } public void log(String message, Throwable t) { getServletContext().log(getServletName() + ": " + message, t); } public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws ServletException, IOException; public String getServletName() { ServletConfig sc = getServletConfig(); if (sc == null) { throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized")); } return sc.getServletName(); } }
HttpServlet源代码
package javax.servlet.http; import java.io.IOException; import java.io.Serializable; import java.lang.reflect.Method; import java.text.MessageFormat; import java.util.Enumeration; import java.util.ResourceBundle; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public abstract class HttpServlet extends GenericServlet implements Serializable { private static final String METHOD_DELETE = "DELETE"; private static final String METHOD_HEAD = "HEAD"; private static final String METHOD_GET = "GET"; private static final String METHOD_OPTIONS = "OPTIONS"; private static final String METHOD_POST = "POST"; private static final String METHOD_PUT = "PUT"; private static final String METHOD_TRACE = "TRACE"; private static final String HEADER_IFMODSINCE = "If-Modified-Since"; private static final String HEADER_LASTMOD = "Last-Modified"; private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings"; private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings"); 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); else resp.sendError(400, msg); } protected long getLastModified(HttpServletRequest req) { return -1L; } protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { NoBodyResponse response = new NoBodyResponse(resp); doGet(req, response); response.setContentLength(); } 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(405, msg); else resp.sendError(400, msg); } protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_put_not_supported"); if (protocol.endsWith("1.1")) resp.sendError(405, msg); else resp.sendError(400, msg); } protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_delete_not_supported"); if (protocol.endsWith("1.1")) resp.sendError(405, msg); else resp.sendError(400, msg); } private Method[] getAllDeclaredMethods(Class c) { if (c.equals(HttpServlet.class)) { return null; } Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass()); Method[] thisMethods = c.getDeclaredMethods(); if ((parentMethods != null) && (parentMethods.length > 0)) { Method[] allMethods = new Method[parentMethods.length + thisMethods.length]; System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length); System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, thisMethods.length); thisMethods = allMethods; } return thisMethods; } protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Method[] methods = getAllDeclaredMethods(getClass()); boolean ALLOW_GET = false; boolean ALLOW_HEAD = false; boolean ALLOW_POST = false; boolean ALLOW_PUT = false; boolean ALLOW_DELETE = false; boolean ALLOW_TRACE = true; boolean ALLOW_OPTIONS = true; for (int i = 0; i < methods.length; ++i) { Method m = methods[i]; if (m.getName().equals("doGet")) { ALLOW_GET = true; ALLOW_HEAD = true; } if (m.getName().equals("doPost")) ALLOW_POST = true; if (m.getName().equals("doPut")) ALLOW_PUT = true; if (m.getName().equals("doDelete")) ALLOW_DELETE = true; } String allow = null; if ((ALLOW_GET) && (allow == null)) allow = "GET"; if (ALLOW_HEAD) if (allow == null) allow = "HEAD"; else allow = allow + ", HEAD"; if (ALLOW_POST) if (allow == null) allow = "POST"; else allow = allow + ", POST"; if (ALLOW_PUT) if (allow == null) allow = "PUT"; else allow = allow + ", PUT"; if (ALLOW_DELETE) if (allow == null) allow = "DELETE"; else allow = allow + ", DELETE"; if (ALLOW_TRACE) if (allow == null) allow = "TRACE"; else allow = allow + ", TRACE"; if (ALLOW_OPTIONS) if (allow == null) allow = "OPTIONS"; else allow = allow + ", OPTIONS"; resp.setHeader("Allow", allow); } protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String CRLF = "/r/n"; String responseString = "TRACE " + req.getRequestURI() + " " + req.getProtocol(); Enumeration reqHeaderEnum = req.getHeaderNames(); while (reqHeaderEnum.hasMoreElements()) { String headerName = (String)reqHeaderEnum.nextElement(); responseString = responseString + CRLF + headerName + ": " + req.getHeader(headerName); } responseString = responseString + CRLF; int responseLength = responseString.length(); resp.setContentType("message/http"); resp.setContentLength(responseLength); ServletOutputStream out = resp.getOutputStream(); out.print(responseString); out.close(); } 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); } } private void maybeSetLastModified(HttpServletResponse resp, long lastModified) { if (resp.containsHeader("Last-Modified")) return; if (lastModified >= 0L) resp.setDateHeader("Last-Modified", lastModified); } 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); } }
GenericServlet实现了接口:Servlet , ServletConfig
HttpServlet :继承了GenericServlet
咱先看下访问页面是怎样的一个流程:
当你访问一个servlet的时候(也可以说是访问一个jsp页面),访问的方式有(get,post,delete),调用service()方法处理你的请求,你可以在里面写你想要干嘛。
在GenericServlet中,service是一个抽象方法,也就是当你以继承GenericServlet的方式创建一个servlet时,必须实现service方法,在里面处理前端发过来的request,和response。
在HttpServlet中,大家可以看它的原代码,有2个service方法,一个是保护的(主要看里面的实现),一个是公有的,公有的service接收到请求时,会抛给保护的那个service,它在这只是起一个中转的作用。当前端有请求过来时,会默认找到这2个service方法,具体先找哪个我不清楚,反正最后请求都会在保护的那个service那里,在那个service中,会根据前端是哪种方式的请求,再抛给相应的处理方法,如:get则交给doGet处理。所以,在以继承GenericServlet的方式创建一个servlet时,你只要重写相应的doGet或者doPost等处理方法就行了,然后再在里面写你的代码就行了。
--------------------
还有一个就是关于servlet的生命周期,它开始于init,而且容器中一个servlet只有一个实例,多个请求过来时,是共享一个servlet的,每次请求都是调用service,它销毁与容器关闭的时候,还有人说可以调用destory来销毁一个实例,我做了实验,不行。可能是不同容器不同的处理吧,你们可以看下源代码,destory在GenericServlet中是空实现,不知道为什么。求高手解答。
测试销毁servlet的代码
public class LoginServlet extends HttpServlet { private Integer a = 0; // 一个servlet将会一直在容器中存在,除非它重启服务器或者强制 /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter pw = resp.getWriter(); pw.write("这是一个servlet"); System.out.println(a++); if (a > 5) { destroy(); //如果销毁的话,下次输出a就又从0开始了,否则就是继续递增 } } }
http://www.cnblogs.com/libingbin/