開心一笑
啥叫代溝?
小學(xué)語文題關(guān)聯(lián)詞填空:
他___ 犧牲生命耘纱,____出賣組織敬肚。
60后填“寧可,也不”
70后填“害怕束析,所以”
80后填“與其艳馒,不如”
90后填“即使,也要”
00后填“白白员寇,忘了”
評論:這幾個回答分別體現(xiàn)了60后的無畏弄慰,70后的無奈,80后的自我蝶锋,90后的叛逆陆爽,00后的呆萌……
這不叫代溝,這叫沒法玩了扳缕!
提出問題
HttpServlet類簡單的學(xué)習(xí)和如何利用慌闭?别威??
解決問題
Servlet類的關(guān)系圖如下驴剔,方便理解:
這里寫圖片描述
HttpServlet類源碼的簡單學(xué)習(xí)
當(dāng)前端發(fā)送請求時省古,會首先調(diào)用HttpServlet中的service方法,假如沒有service方法丧失,才會調(diào)用父類方法豺妓。我說的是假如
Service方法的具體源碼為:
//請求會先訪問這個service
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
if(req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
//將req 和res強(qiáng)制轉(zhuǎn)換為帶Http的request 和response
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
//然后調(diào)用帶Http req和res的service方法
this.service((HttpServletRequest)request, (HttpServletResponse)response);
} else {
throw new ServletException("non-HTTP request or response");
}
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲得請求方法的類型
String method = req.getMethod();
long errMsg;
//如果是GET請求
if(method.equals("GET")) {
errMsg = this.getLastModified(req);
if(errMsg == -1L) {
//調(diào)用doGet方法
this.doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < errMsg) {
this.maybeSetLastModified(resp, errMsg);
this.doGet(req, resp);
} else {
resp.setStatus(304);
}
}
//如果是HEAD請求
} else if(method.equals("HEAD")) {
errMsg = this.getLastModified(req);
this.maybeSetLastModified(resp, errMsg);
this.doHead(req, resp);
//如果是POST請求
} else if(method.equals("POST")) {
this.doPost(req, resp);
//如果是PUT請求
} else if(method.equals("PUT")) {
this.doPut(req, resp);
//如果是DELETE請求
} else if(method.equals("DELETE")) {
this.doDelete(req, resp);
//如果是OPTIONS請求
} else if(method.equals("OPTIONS")) {
this.doOptions(req, resp);
} else if(method.equals("TRACE")) {
this.doTrace(req, resp);
} else {
String errMsg1 = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[]{method};
errMsg1 = MessageFormat.format(errMsg1, errArgs);
resp.sendError(501, errMsg1);
}
}
doGet方法源碼(doPost方法類似)
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//獲取請求協(xié)議
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
//如果是HTTP1.1
if(protocol.endsWith("1.1")) {
//設(shè)置響應(yīng)碼405
resp.sendError(405, msg);
} else {
//設(shè)置響應(yīng)碼400
resp.sendError(400, msg);
}
}
看到這里我們就知道,我們項目中的Servlet繼承HttpServlet布讹,一般要重寫doGet和doPost方法
然后再方法里實現(xiàn)我們要的邏輯琳拭,否則會出現(xiàn) 405等等異常。
例如下面簡單例子:
package com.evada.de.serialnum.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by Ay on 2016/5/1.
*/
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//實現(xiàn)自己的業(yè)務(wù)邏輯
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//實現(xiàn)自己的業(yè)務(wù)邏輯
}
}
讀書感悟
- 即便如此還是想要待在這里描验,因為這里是屬于 我的地方白嘁。
- 即使摔倒又如何?再站起來就行了挠乳。在摔倒的同時仰望天空权薯,廣闊無限的藍(lán)天今天也在對我微笑。
- 人不是活在過去睡扬,做好現(xiàn)在能做的事就夠了。