doPost()详解
此方法中注释的意思为:
由服务器(通过<code>service</code>方法)调用,以允许servlet处理POST请求。
HTTP POST方法允许客户端一次向Web服务器发送无限长的数据,并且在发布诸如信用卡号码之类的信息时非常有用。
<p>当重写此方法时,读取请求数据,写入响应头,获取响应的写入器或输出流对象,最后写入响应数据。最好包括内容类型和编码。当使用<code>PrintWriter</code>对象返回响应时,在访问<code>PrintWriter</code>对象之前设置内容类型。
Servlet容器在提交响应之前必须写入报头,因为在HTTP中,报头必须在响应主体之前发送。如果整个响应都在响应缓冲区中,则自动设置内容长度。
如果可能,设置Content-Length头部(使用{@link javax.servlet.ServletResponse#setContentLength}方法),
允许servlet容器使用持久连接将响应返回给客户端,从而提高性能。如果整个响应都位于响应缓冲区中,则自动设置内容长度。
当使用HTTP 1.1分块编码(这意味着响应具有Transfer-Encoding报头)时,不要设置Content-Length报头。
此方法不需要是安全的或幂等的。通过POST请求的操作可能具有副作用,用户可以对此负责,例如,更新存储的数据或在线购买物品。
(
博主补充:这就是常说的post方法提交表单时,如果刷新当前页面会重复提交的问题,这句话想表达的就是这个意思;什么是幂等呢?
幂等(idempotent、idempotence)是一个数学与计算机学概念,常见于抽象代数中。
在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。---摘自百度百科
)
<p>如果HTTP POST请求的格式不正确,
<code>doPost</code>返回HTTP“Bad Request”消息。
/*** Called by the server (via the <code>service</code> method)* to allow a servlet to handle a POST request.** The HTTP POST method allows the client to send* data of unlimited length to the Web server a single time* and is useful when posting information such as* credit card numbers.** <p>When overriding this method, read the request data,* write the response headers, get the response's writer or output* stream object, and finally, write the response data. It's best* to include content type and encoding. When using a* <code>PrintWriter</code> object to return the response, set the* content type before accessing the <code>PrintWriter</code> object.** <p>The servlet container must write the headers before committing the* response, because in HTTP the headers must be sent before the* response body.** <p>Where possible, set the Content-Length header (with the* {@link javax.servlet.ServletResponse#setContentLength} method),* to allow the servlet container to use a persistent connection* to return its response to the client, improving performance.* The content length is automatically set if the entire response fits* inside the response buffer.** <p>When using HTTP 1.1 chunked encoding (which means that the response* has a Transfer-Encoding header), do not set the Content-Length header.** <p>This method does not need to be either safe or idempotent.* Operations requested through POST can have side effects for* which the user can be held accountable, for example,* updating stored data or buying items online.** <p>If the HTTP POST request is incorrectly formatted,* <code>doPost</code> returns an HTTP "Bad Request" message.*** @param req an {@link HttpServletRequest} object that* contains the request the client has made* of the servlet** @param resp an {@link HttpServletResponse} object that* contains the response the servlet sends* to the client** @exception IOException if an input or output error is* detected when the servlet handles* the request** @exception ServletException if the request for the POST* could not be handled** @see javax.servlet.ServletOutputStream* @see javax.servlet.ServletResponse#setContentType*/protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String protocol = req.getProtocol();String msg = lStrings.getString("http.method_post_not_supported");if (protocol.endsWith("1.1")) {resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);} else {resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);}}
一般记住这句话就行了:
由服务器(通过<code>service</code>方法)调用,以允许servlet处理POST请求。
doPost()详解相关推荐
- HttpServletResponse,HttpServletRequest详解
HttpServletResponse,HttpServletRequest详解 1.相关的接口 HttpServletRequest HttpServletRequest接口最常用的方法就是获得请求 ...
- [Spring mvc 深度解析(一)] 详解Servlet
详解Servlet Servlet是Server+Applet的缩写,表示一个服务器应用.通过上面的分析我们知道Servlet其实就是一套规范,我们按照这套规范写的代码就可以直接在Java的服务器 ...
- Stuts的Web.xml 详解
1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码. DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用 ...
- Java Servlet关键点详解
Java Servlet关键点详解 1.理解Servlet的生命周期 Servlet引擎控制着Servlet的生命周期 Servlet的生命周期由以下三个方法进行描述(五个生命周期阶段) 1)初始化 ...
- 通过源码详解 Servlet
Servlet 结构 1.Servlet Servlet 该接口定义了5个方法. init(),初始化 servlet 对象,完成一些初始化工作.它是由 servlet 容器控制的,该方法只能被调用一 ...
- JavaWeb 入门篇 (5) Cookie 和 Session 详解
Cookie 和 Session 详解 一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过 ...
- java 会话跟踪技术_JavaEE基础(04):会话跟踪技术,Session和Cookie详解
一.会话跟踪 1.场景描述 比如登录某个购物网站,身份识别成功后,在网站下单,支付 等操作,这些操作中当前登录用户信息必须是共享的,这样这些操作结果才能和登录用户做关联. 2.概念简介 可以把会话理解 ...
- ServletFileUpload API详解
ServletFileUpload1.ServletFileUpload upload=new ServletFileUpload(factory);创建一个上传工具,指定使用缓存区与临时文件存储位置 ...
- DiskFileItemFactory API详解
核心API介绍1.DiskFileItemFactory作用:可以设置缓存大小以及临时文件保存位置. 默认缓存大小是 10240(10k).临时文件默认存储在系统的临时文件目录下.(可以在环境变量中查 ...
- 利用jquery操作ajax,利用jquery对ajax操作,详解原理(附代码)
1.jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法: $(selector ...
最新文章
- 店宝宝电脑版_起名字 测名字神器 电脑安卓全有了
- TypeSprict -- 基础类型
- XML文件与实体类的互相转换
- 求职信计算机工程师英语作文,开发工程师英文求职信范文
- Java命令行界面(第26部分):CmdOption
- [Ajax] jQuery中的Ajax -- 03-搜索框提示效果
- 计算机证据的获取,计算机证据获取技术的研究与应用.pdf
- h5传奇 cqgmb.php,魔兽世界怀旧服狂暴战bwl最新WA字符串集合
- 第三章 网络体系结构作业
- linux 桥接stp原理,Linux 中的网桥技术
- 单片机控制10BitDA正弦信号发生器 PROTEUS 和51单片机教程(附仿真文件+源代码)
- NC单据模板公式(6大类)
- ASP.NET身份验证和授权,使用cookie和Claims认证
- uni-App 商品详情轮播图
- Multisim仿真—恒流源电路(二)
- Unity-两张图片叠加合成一张图片
- 技巧:彻底删除电脑弹窗广告,还你一个干净的桌面!
- Win10 安装编译器|调试器 TDM-GCC/Mingw64
- 使用Microsoft.Office.Interop.Excel批量编辑Excel文件
- 关于Linux的视频编程(v4l2编程)
热门文章
- 干货!浏览器提示“您与此网站之间建立的连接不安全”的解决方案
- 2022电大国家开放大学网上形考任务-会计学概论非免费(非答案)
- rpa机器人是什么意思?有什么用?
- java-php-python-springboot小区停车场管理系统计算机毕业设计
- 网络游戏防沉迷实名认证系统- 常见问题02
- 【Javscript Java】解决CSV中文乱码的问题
- SSM+layui实现学籍管理系统
- 认证失败是什么意思_BSCI是什么意思,BSCI验厂具体认证标准
- 从输入/输出的视角看TCP/IP(终端,shell以及X Window)
- LAMMPS—fix命令超详细解析