Servlet的方法剖析:
1.service()方法里面做了什么?
2.doGet()與doPost()做了什么?應(yīng)該怎么寫?
回答
1.service()方法里面做了什么?

如果你的service方法中沒有寫super.service(req, resp); 那么后果是doget()和dopost()方法永遠(yuǎn)不會(huì)調(diào)用.這是為什么呢?
我們可以查看一下HttpServlet的源代碼:(Ps:已經(jīng)有刪除 , 補(bǔ)錄中有完整的代碼....)
------------------------Service()--------------------------------------------------
PRotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getMethod();
if (method.equals(METHOD_GET)) {//如果" 請(qǐng)求 "是get方法 , 那就調(diào)用doget , 否則看看是不是post long lastModified = getLastModified(req); if (lastModified == -1) { doGet(req, resp);//調(diào)用 }else if (method.equals(METHOD_POST)) { doPost(req, resp);
.......后面是諾干個(gè)if-else語句
從這個(gè)源代碼中就可以看出Service()主要是干什么了吧-----他就是用來區(qū)分是get方法還是post方法的
2.doGet里面做了什么?
下賣個(gè)關(guān)子 , 請(qǐng)看這句代碼對(duì)不對(duì),會(huì)不會(huì)出現(xiàn)異常.....

答案是:一定會(huì)出現(xiàn)異常,原因就是你寫了一個(gè)super.doGet();
這個(gè)會(huì)調(diào)用父類的doGet()方法 , 這樣做的結(jié)果就是: 會(huì)出現(xiàn)405(請(qǐng)求錯(cuò)誤)或者是illegalXXXException
Why? 為什么doget()不可以調(diào)用父類的方法?
原因就是:

這段源代碼就是在告訴我們:無論如何,他都會(huì)調(diào)用requset.sendError方法的 ,
如果你還想在super.doGet()下面獲取參數(shù)的話那就是錯(cuò)誤的 ,因?yàn)閟endError之后上下文已經(jīng)改變了 , 你就沒法獲取參數(shù)了;
例如:
這個(gè):

然后調(diào)用之后:

-------補(bǔ)錄------------------------------------------------/** * Receives standard HTTP requests from the public * <code>service</code> method and dispatches * them to the <code>do</code><i>Method</i> methods defined in * this class. This method is an HTTP-specific version of the * {@link javax.servlet.Servlet#service} method. There's no * need to override this method. * * @param req the {@link HttpServletRequest} object that * contains the request the client made of * the servlet * * @param resp the {@link HttpServletResponse} object that * contains the response the servlet returns * to the client * * @exception IOException if an input or output error occurs * while the servlet is handling the * HTTP request * * @exception ServletException if the HTTP request * cannot be handled * * @see javax.servlet.Servlet#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; try { ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); } catch (IllegalArgumentException iae) { // Invalid date header - proceed as if none was set ifModifiedSince = -1; } 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); }}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注