¡¡¡¡¹³×Ó·½·¨µÄÃû×ÖÓ¦µ±ÒÔdo¿ªÊ¼£¬ÕâÊÇÊìϤÉè¼ÆÄ£Ê½µÄJava¿ª·¢ÈËÔ±µÄ±ê×¼×ö·¨¡£ÔÚÉÏÃæµÄÀý×ÓÖУ¬¹³×Ó·½·¨hookMethod()Ó¦µ±ÒÔdo¿ªÍ·£»ÔÚHttpServletÀàÖУ¬Ò²×ñ´ÓÕâÒ»ÃüÃû¹æÔò£¬ÈçdoGet()¡¢doPost()µÈ·½·¨¡£
ËÄ¡¢Ä£°å·½·¨Ä£Ê½ÔÚServletÖеÄÓ¦ÓÃ
¡¡¡¡Ê¹ÓùýServletµÄÈ˶¼Çå³þ£¬³ýÁËÒªÔÚweb.xml×öÏàÓ¦µÄÅäÖÃÍ⣬»¹Ðè¼Ì³ÐÒ»¸ö½ÐHttpServletµÄ³éÏóÀà¡£HttpServiceÀàÌṩÁËÒ»¸öservice()·½·¨£¬Õâ¸ö·½·¨µ÷ÓÃÆß¸ödo·½·¨ÖеÄÒ»¸ö»ò¼¸¸ö£¬Íê³É¶Ô¿Í»§¶Ëµ÷ÓõÄÏìÓ¦¡£ÕâЩdo·½·¨ÐèÒªÓÉHttpServletµÄ¾ßÌå×ÓÀàÌṩ£¬Òò´ËÕâÊǵäÐ͵ÄÄ£°å·½·¨Ä£Ê½¡£ÏÂÃæÊÇservice()·½·¨µÄÔ´´úÂ룺
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_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 {
//
// 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);
}
}µ±È»£¬Õâ¸öservice()·½·¨Ò²¿ÉÒÔ±»×ÓÀàÖû»µô¡£
ÏÂÃæ¸ø³öÒ»¸ö¼òµ¥µÄServletÀý×Ó£º
´ÓÉÏÃæµÄÀàͼ¿ÉÒÔ¿´³ö£¬TestServletÀàÊÇHttpServletÀàµÄ×ÓÀ࣬²¢ÇÒÖû»µôÁ˸¸ÀàµÄÁ½¸ö·½·¨£ºdoGet()ºÍdoPost()¡£
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("using the GET method");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("using the POST method");
}
}
´ÓÉÏÃæµÄÀý×Ó¿ÉÒÔ¿´³öÕâÊÇÒ»¸öµäÐ͵ÄÄ£°å·½·¨Ä£Ê½¡£
HttpServletµ£ÈγéÏóÄ£°å½ÇÉ«
Ä£°å·½·¨£ºÓÉservice()·½·¨µ£ÈΡ£
»ù±¾·½·¨£ºÓÉdoPost()¡¢doGet()µÈ·½·¨µ£ÈΡ£
TestServletµ£ÈξßÌåÄ£°å½ÇÉ«
TestServletÖû»µôÁ˸¸ÀàHttpServletÖÐÆß¸ö»ù±¾·½·¨ÖÐµÄÆäÖÐÁ½¸ö£¬·Ö±ðÊÇdoGet()ºÍdoPost()¡£
ת×Ô£ºhttp://www.cnblogs.com/java-my-life/archive/2012/05/14/2495235.html