doGet或doPost请求
一、HTTP实现doGet或doPost请求原理介绍
1、浏览器发送请求到HttpSevr类调用HttpServ的service(servletRequest, servletReponse)方法
2、由于没有找到这个方法,去调用父类(HttpServlet) 的同名方法。
3、父类的service方法将ServletRequest req请求转换成HttpServletRequest请求,再去调用service(request, response) 方法。
将ServletRequest req请求转换成HttpServletRequest请求再调用service(request, response) 方法源码如下:
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);}
4、 调用的service(request, response) 方法功能是判断用户发出是什么请求,如果是get则调用子类(HttpSevr)的doGet方法,如果是post则调用子类(HttpSevr)的doPost方法。
service(request, response) 方法源码如下:
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 logicdoGet(req, resp);} else {long ifModifiedSince;try {ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);} catch (IllegalArgumentException iae) {// Invalid date header - proceed as if none was setifModifiedSince = -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 lessmaybeSetLastModified(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);}}
5、调用关系图
二、注意事项:
1、如果在HttpServ中覆盖了service(ServletRequest,SerlvetResonse)方法则这个类的所实现的doGet/doPost都不会再执行了。
因为service(ServletRequest,SerlvetResonse)是最高接口Servlet定义规范。在tomcat调用时,一定会在最终的子类中去找这个方法且调用它。
如果最终的子类没有则会调用父的service(ServletRequest,SerlvetResonse)。
2、如果覆盖了serivce(HttpServletRequest,HtpServletResponse)则会执行httpServlet中的service(ServletRequest,SerlvetResonse),但是由于子类中已经覆盖了serivce(HttpServletRequest,HtpServletResponset)所以,httpServlet中的serivce(HttpServletRequest,HtpServletResponset)就不再执行了,而是直接执行子类中同名同参数方法,且doXxxx也不会执行了,因为子类的serivce(HttpServletRequest,HtpServletResponset)没有调用doXxxx.
3、如果继承了HttpServlet没有实现任何的doXxx方法则会抛出一个异常
三、客户端请求和返回数据用到的常用方法:
javaweb学习总结(十)——HttpServletRequest对象(一) - 孤傲苍狼 - 博客园 (cnblogs.com)
doGet或doPost请求相关推荐
- okHttp拦截器放在doGet和doPost请求中
public class OkHttp3Util { /** * 懒汉 安全 加同步 * 私有的静态成员变量 只声明不创建 * 私有的构造方法 * 提供返回实例 ...
- 执行Http请求时doGet和doPost的区别
应用方式\提交方式 doGet doPost 请求方式 通过URL附带参数发送请求,从服务器上获取数据.在url中可以看到传递的参数. 通过url发送请求,从服务器上提交数据,但不通过url传递数据, ...
- android doGet和doPost
doGet和doPost的区别 get和post是http协议的两种方法,另外还有head, delete等 这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能是字 ...
- java中的doget_java servlet中doGet()和doPost()方法的用法和区别
一般来说我们是用不到doGet方法的,doGet方法提交表单的时候会在url后边显示提交的内容,所以不安全.而且doGet方法只能提交256个字符(1024字节),而doPost没有限制,因为get方 ...
- Service 和 doGet 和 doPost 方法的区别
Service 和 doGet 和 doPost 方法的区别 新建一个JSP文件: get方式 post方式 service方法.doGet方法和doPost方法的区别: 在上面的步骤中,把下图中的m ...
- HttpServlet的doGet()和doPost()方法
由于,大多数客户端的请求方式都是GET和POST 因此,HttpServlet中提供了doGet()和doPost()方法 示例程序 在目录D:\cn\itcast\firstapp\servlet中 ...
- JavaWeb第二讲 重定向与转发 doGet()与doPost()
(一)重定向与转发 转发**(服务器行为)** request.getRequestDispatcher("url").forward(request, response); 重定 ...
- Myeclipse中快速插入HttpServlet子类中doGet和doPost方法
Myeclipse中快速插入HttpServlet子类中doGet和doPost方法 鼠标右键-source-Override/Implment Methods 参考自:https://blog.cs ...
- 对doget和dopost区别的理解
doget与dopost的区别 GET方法用于获取固定资源,一般并不用于传参数,但是实际开发过程中很多都是在用GET方法来传递参数. get直接访问:http://write.blog.csdn.ne ...
最新文章
- 基于用户的协同过滤和皮尔逊相关系数
- Mysql数据中文乱码问题处理
- 2.3线性表的链式表示和实现
- mongodb 日志,(一个看日志解决新问题的方法) -- clwu
- 苹果屏幕旋转怎么设置_iPhone12屏幕供应商是谁 苹果12屏幕怎么查看是哪家
- python运维开发_python运维开发
- QT GUI界面中集成VISIO绘图区详细说明
- App Inventer制作蓝牙通讯软件
- java get请求参数乱码_java web项目get,post请求参数中文乱码解决
- IP城域网遇到的总结
- 2017机器人与自动化领域SCI期刊最新影响因子
- 史上最全echarts可视化图表详解
- 001 第一季:SpringBoot2核心技术
- 书论29 颜真卿《怀素上人草书歌序》
- 计算机无法打开eventlog,电脑系统日志不能查看怎么办
- Python玩转数据分析——双因素方差分析
- fbx模型导入unity,绑了骨骼加蒙皮法线就反
- 推荐几个浪尖收藏的大数据学习平台
- html侧边工具栏,侧边栏工具条
- 在线词典助手安装教程