国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

雜談:Servlet(2)

2019-11-14 23:13:20
字體:
供稿:網(wǎng)友
雜談:Servlet(2)

Servlet的方法剖析:

1.service()方法里面做了什么?

2.doGet()與doPost()做了什么?應(yīng)該怎么寫?

回答

1.service()方法里面做了什么?

image

如果你的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)異常.....

image

答案是:一定會(huì)出現(xiàn)異常,原因就是你寫了一個(gè)super.doGet();

這個(gè)會(huì)調(diào)用父類的doGet()方法 , 這樣做的結(jié)果就是: 會(huì)出現(xiàn)405(請(qǐng)求錯(cuò)誤)或者是illegalXXXException

Why? 為什么doget()不可以調(diào)用父類的方法?

原因就是:

image

這段源代碼就是在告訴我們:無論如何,他都會(huì)調(diào)用requset.sendError方法的 ,

如果你還想在super.doGet()下面獲取參數(shù)的話那就是錯(cuò)誤的 ,因?yàn)閟endError之后上下文已經(jīng)改變了 , 你就沒法獲取參數(shù)了;

例如:

這個(gè):image

image

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

image

-------補(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); }}



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 上饶市| 莆田市| 河西区| 南漳县| 四会市| 容城县| 绥江县| 徐水县| 宁晋县| 巩留县| 芷江| 敦煌市| 得荣县| 修水县| 延吉市| 贵港市| 阳高县| 新化县| 汽车| 内乡县| 通州市| 晋州市| 察隅县| 双牌县| 靖边县| 山丹县| 赞皇县| 阿勒泰市| 新巴尔虎右旗| 行唐县| 陆良县| 新蔡县| 开鲁县| 区。| 定安县| 凌云县| 郁南县| 定安县| 尚志市| 沧源| 伊宁市|