开心一笑
啥叫代沟?
小学语文题关联词填空:
他___ 牺牲生命,____出卖组织。
60后填“宁可,也不”
70后填“害怕,所以”
80后填“与其,不如”
90后填“即使,也要”
00后填“白白,忘了”
评论:这几个回答分别体现了60后的无畏,70后的无奈,80后的自我,90后的叛逆,00后的呆萌……
这不叫代沟,这叫没法玩了!
提出问题
HttpServlet类简单的学习和如何利用???
解决问题
Servlet类的关系图如下,方便理解:
HttpServlet类源码的简单学习
当前端发送请求时,会首先调用HttpServlet中的service方法,假如没有service方法,才会调用父类方法。我说的是假如
Service方法的具体源码为:
//请求会先访问这个service
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
if(req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
//将req 和res强制转换为带Http的request 和response
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
//然后调用带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) {
//调用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 {
//获取请求协议
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
//如果是HTTP1.1
if(protocol.endsWith("1.1")) {
//设置响应码405
resp.sendError(405, msg);
} else {
//设置响应码400
resp.sendError(400, msg);
}
}
看到这里我们就知道,我们项目中的Servlet继承HttpServlet,一般要重写doGet和doPost方法
然后再方法里实现我们要的逻辑,否则会出现 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 {
//实现自己的业务逻辑
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//实现自己的业务逻辑
}
}
读书感悟
- 即便如此还是想要待在这里,因为这里是属于 我的地方。
- 即使摔倒又如何?再站起来就行了。在摔倒的同时仰望天空,广阔无限的蓝天今天也在对我微笑。
- 人不是活在过去,做好现在能做的事就够了。