HttpServlet中的service方法
生活随笔
收集整理的這篇文章主要介紹了
HttpServlet中的service方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HttpServlet中的service方法service方法是接口中的方法,servlet容器把所有請求發送到該方法,該方法默認行為是轉發http請求到doXXX方法中,如果你重載了該方法,默認操作被覆蓋,不再進行轉發操作!
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 lessmaybeSetLastModified(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);
}}
總結
以上是生活随笔為你收集整理的HttpServlet中的service方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java向MySQL数据库插入时间类型D
- 下一篇: [转载]关于request和sessio