Cannot call sendError() after the response has been committed - baiyangliu
当response提交后,不能调用sendError(),什么意思?
出现这个错误,一定是多次response导致的。可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你还试图发送数据给客户端,显然会出错。就好比我俩打电话,我都挂电话了,你还在“喂喂喂”。
例如下面这段代码就会出现此错误:
- import java.io.Writer;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String userName;
- private String pwd;
- private String verifyCode;
- private String ajax;
- // 错误的写法
- @Override
- public String execute() throws Exception {
- // 通过ajax登录
- if (ajax != null) {
- HttpServletResponse response = ServletActionContext.getResponse();
- Writer writer = response.getWriter();
- writer.write("登录成功!");
- writer.flush();
- writer.close();
- }
- return SUCCESS;
- }
- // 正确写法
- public String login1() throws Exception {
- if (ajax != null) {
- HttpServletResponse response = ServletActionContext.getResponse();
- Writer writer = response.getWriter();
- writer.write("登录成功!");
- writer.flush();
- writer.close();
- return null;
- }
- return SUCCESS;
- }
- // 正确写法
- public String login2() throws Exception {
- if (ajax != null) {
- HttpServletResponse response = ServletActionContext.getResponse();
- Writer writer = response.getWriter();
- writer.write("登录成功!");
- writer.flush();
- writer.close();
- }
- return null;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPwd() {
- return pwd;
- }
- public void setPwd(String pwd) {
- this.pwd = pwd;
- }
- public String getVerifyCode() {
- return verifyCode;
- }
- public void setVerifyCode(String verifyCode) {
- this.verifyCode = verifyCode;
- }
- public String getAjax() {
- return ajax;
- }
- public void setAjax(String ajax) {
- this.ajax = ajax;
- }
- }
以上为登录测试代码(Struts2),在以上示例中,如果判断为ajax!=null成立,那么一定会报如题所示的错误,原因就是:if子句里已经做了一次response,在writer.close();的时候,本次response已经完成;但是紧接着在return SUCCESS;的时候,相当于又做了一次response,所以就出错了~
类似的错误也会出现于以下代码中:
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- Writer writer = response.getWriter();
- writer.write("Hello");
- writer.flush();
- writer.close();
- response.sendRedirect("http://blog.csdn.net/baiyanglu/article/details/8076104");
- }
出现本错误后,web前端能够接收到第一次response的内容(就是说,挂电话之前说的话,我还是可以听到的,挂电话后讲的,当然听不到咯~),不会报错,只会在后台显示出错了
以上,转自http://www.cnblogs.com/wang3680/p/2c5f28ecfb8c2e2772474f3fab4d771c.html
Cannot call sendError() after the response has been committed - baiyangliu的更多相关文章
- Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 报错:java.lang.IllegalStateException: Cannot call sendError() after the response has been committed(待解答)
严重: Servlet.service() for servlet [default] in context with path [/20161101-struts2-1] threw excepti ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读
源代码: @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Ob ...
- Cannot call sendError() after the response has been committed(filter问题)
就是因为执行了filter的dofilter方法中 chain.doFilter(request,response)了 执行了两遍 if(){}else{chain.doFilter(request, ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committe
1.问题描述 严重: Servlet.service() for servlet [default] in contextwith path [/OxygenCloud] threw exceptio ...
- [Java][Servlet] Cannot call sendRedirect() after the response has been committed
做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...
- Cannot call sendRedirect() after the response has been committed的解决办法
做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...
- Cannot forward after response has been committed
项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...
随机推荐
- [notes] some code tips
genericizing-codehtml, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin ...
- python 字符串和整数,浮点型互相转换
在编程当中,经常要用到字符串的互相转换, 现在记录 python 里面的字符串和整数是怎么转换的. int(str) 函数将 符合整数的规范的字符串 转换成 int 型. num2 = "1 ...
- net-snmp 的配置
http://www.cnblogs.com/oloroso/p/4595123.html
- Face Alignment by Coarse-to-Fine Shape Searching--解析
人脸关键点定位.Face Alignment by Coarse-to-Fine Shape Searching 算法源码详解(上) http://blog.csdn.net/shenxiaolu19 ...
- 微信小程序——时间戳的转换及调用
开发微信小程序网盘功能模块的时候,需要获取到网盘文件夹创建的时间.如下图: 但是请求返回的数据是一段时间戳,如下图: 所以我们只能通过js把时间戳转换成时间格式. 在小程序官网的Demo的utils. ...
- Linux 客户端bind函数的使用
无连接的socket的客户端和服务端以及面向连接socket的服务端通过调用bind函数来配置本地信息. 使用bind函数时,通过将my_addr.sin_port置为0,函数会自动为你选择一个未占用 ...
- 使用 pv 命令监控 linux 命令的执行进度
如果你是一个 linux 系统管理员,那么毫无疑问你必须花费大量的工作时间在命令行上:安装和卸载软件,监视系统状态,复制.移动.删除文件,查错,等等.很多时候都是你输入一个命令,然后等待很长时间直到执 ...
- tortoiseSVN 合并代码方法
http://blog.csdn.net/zhuyong0722/article/details/8965095 转自:http://tangzhifei.iteye.com/blog/1143553 ...
- Ogre 监听类与渲染流程
Ogre中有许多监听类,我们可以简单理解成C#中的事件,这些类作用都不小,说大点可能改变流程,说小点修改参数等,下面列举一些常用的监听类. FrameListener:由Ogre中的Root负责维护, ...
- e778. 在JList中加入和删除项
The default model for a list does not allow the addition and removal of items. The list must be crea ...