HttpServlet继承了GenericServlet抽象类,主是要让GenericServlet与具体的协议连接,而HttpServlet主是就处理Http协议请求,根据请求方法的不同调用相应的方法:

 

源码:HttpServlet.java

 

public abstract class HttpServlet extends GenericServlet
implements java.io.Serializable{
/*定义一些常量,用于与HTTP请求方法的匹配*/
private static fina
String METHOD_GET = "GET";
private static fina
String METHOD_POST = "POST";
...
  public HttpServlet() { }
/*重写GenericServlet抽象类的service方法,作用是根据HTTP请求的方法不同调用相应的方法,比如说HTTP请求是GET方法,则调用doGet()*/
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);
}
/*作用是根据HTTP请求的方法不同调用相应的方法,比如说HTTP请求是GET方法,则调用doGet()*/
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) {
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < (lastModified / 1000 * 1000)) {
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_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(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}
 /*这个方法体内容在其子类能用得上?*/
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String protoco
= 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 protoco
= 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 doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{…}

}

HttpServlet类并不复杂,其作用是也是很明显,HTTP请求什么方法,就调用相应的方法,比较有意义的就是HttpServletRequest这个类,它是怎样封装HTTP请求,怎样去解释HTTP请求报文。。。。

 

还有一点值得注意的就是,doGet,doPost的权限都是protected,意味着其类可以去继承,不过按我们所实现子类在都数都是去重写,并权限改为public,这里就有一个问题,HttpServlet的doGet,doPost等方法体内容有何用,子类只是会去调用重写的doGet,doPost方法,根本就不会去执行父关的doGet,doPost方法,那么

 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String protoco
= 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);
}
}

这一句就不会起作用,不知为何要这么设计 。。。

posted on 2010-05-26 09:56  ALVINZ  阅读(443)  评论(0)    收藏  举报