Servlet -doGet() doPost()原理
一、自定义类只需要重写doGet(HttpServletRequest request, HttpServletResponse response) 和doPost(HttpServletRequest request, HttpServletResponse, response)的原因:
1、在HttpServlet接口中重写了父类的service(ServletRequest request, ServletResponse response),在这里面主要完成了将父类的request和response对象转化成专门用于处理HTTP请求响应的HttpServletRequest和HttpServletResponse对象,然后再调用一个重载的service方法,该方法接收HttpServletRequest和HttpServletResponse对象:
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response; try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
}
2、在重载的service方法中,调用了doGet(HttpServletRequest request, HttpServletResponse)和doPost(HttpServletRequest request, HttpServletResponse),用于处理Get和Post请求和响应:
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 less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
} }
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
由上可知,Servlet引擎在处理用户请求时所调用的service(ServletRequest req, ServletResponse rep)最终会执行doGet和doPost方法中的代码完成。这就是在自定义的Servlet类中,只要继承了HttpServlet就只需要重写doGet和doPost即可。
二、Servlet的生命周期:
1、加载和实例化Servlet对象:由Servlet窗口负责加载Servlet,当Servlet窗口启动或是检测到需要这个Servlet响应第一个请求时,将加载这个Servlet。加载的方式是通过反射的API去调用Servelet的无参构造方法完成,所以在自定义的Servlet中不应该出现带参数的构造方法,如果需要定义有参的构造方法,则一定要把无参的也写出来。
2、初始化Servlet对象:调用init()方法完成相应的初始化,如读取初始化参数、连接DB等。
3、处理请求:调用service()方法处理请求。之前一这是init()方法执行成功。
4、销毁Servlet对象:调用detroy()方法。
Servlet -doGet() doPost()原理的更多相关文章
- [转]servlet中的service, doGet, doPost方法的区别和联系
原文地址:http://m.blog.csdn.net/blog/ghyg525/22928567 大家都知道在javax.servlet.Servlet接口中只有init, service, des ...
- servlet 中 service ,doGet , doPost 关系
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...
- Servlet的工作原理和生命周期
Servlet的工作原理 . Web服务器加载Servlet:Web服务器启动后,它会根据每个工程的web.xml文件去查找该工程的Servlet,并且找到这些Servlet的Class文件所在的地址 ...
- java servlet的工作原理
servlet本质上就是java类嘛.不过是有特殊规范的java类而已.下面就说一说为什么servlet要有特殊规范. 首先,考虑一下什么地方用servlet,WEB应用,而且是需要servlet容器 ...
- doget,doPost在底层走的是service
doget,doPost在底层走的是service 因为在源码上 先执行service方法 然后再调用doget,doPost方法
- Servlet 小试牛刀(doGet,doPost)
实验说明: 通过javax.servlet.http下的HttpServlet,HttpServletRequest,HttpServletResponse来完成一些常用Servlet实例 java代 ...
- servlet的doPost 和doGet和web文件结构
doPost和doGet分别由 tomcat自己来决定调用post 还是get 方式查询 get:url有少量的参数信息,一般用到查询那里 (像百度.. post一般用来提交大文件数据(二进制数据 d ...
- 关于HttpServlet和Servlet以及doPost和doGet关系
这两天在看Servlet和Jsp,spring太难了,还是先看看基础,只怪自己太弱了. Servlet是一个接口,本身定义的是一种网络服务,HttpServlet是已经实现了Servlet接口,也就是 ...
- servlet中doPost()和doGet()
转载至 http://blog.163.com/grandry_it_bird/blog/static/1751633362010102615553610/ 一般来说我们是用不到doGet方法的,do ...
随机推荐
- ubuntu下cmake自动化编译的一个例子
一个CMakeLists.txt的例子参考:https://www.hahack.com/codes/cmake/https://blog.csdn.net/afei__/article/detail ...
- HTTP状态码及说明
- [Webpack] Detect Unused Code with Webpack and unused-files-webpack-plugin
As you refactor and modify applications, it's difficult to manage and keep track of files as they be ...
- 一条长为L的绳子,一面靠墙,另外三边组成矩形,问此矩形最大面积能是多少?
靠墙的两边设为x,墙的对边设为y,有2x+y=L; 则y=L-2x, 矩形面积函数为xy=x(L-2x)=-2x2+xL,即f(x)=-2x2+xL 这时就是求二次函数的极值问题了. 按二次函数y=a ...
- RTSP - RTP over TCP
RTP over RTSP(TCP)(一) RTP over RTSP包混合发送的解决办法 RTSP - RTP over TCP To use TCP communication, ...
- 【转】C++ 虚函数&纯虚函数&抽象类&接口&虚基类
1. 动态多态 在面向对象语言中,接口的多种不同实现方式即为多态.多态是指,用父类的指针指向子类的实例(对象),然后通过父类的指针调用实际子类的成员函数. 多态性就是允许将子类类型的指针赋值给父类类型 ...
- XML-RPC.NET
XML-RPC.NET 是一个 .NET 的客户端服务器的基于 XML-RPC 远程过程调用的框架. 示例代码: [XmlRpcUrl("http://betty.userland.com/ ...
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- CVTE电话面试
Cvte电话面试 1. SVM和逻辑回归的相同不同点 2. 特征值和奇异值的区别 3. 如何找到全局最优解,梯度下降和牛顿法区别 4. 防止过拟合的方法 5. 随机森林和ADBOOST方差和偏置 6. ...
- 补习知识:Entity Framework Code First属性映射约定
Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...