当response提交后,不能调用sendError(),什么意思?

出现这个错误,一定是多次response导致的。可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你还试图发送数据给客户端,显然会出错。就好比我俩打电话,我都挂电话了,你还在“喂喂喂”。

例如下面这段代码就会出现此错误:

  1. import java.io.Writer;
  2. import javax.servlet.http.HttpServletResponse;
  3. import org.apache.struts2.ServletActionContext;
  4. import com.opensymphony.xwork2.ActionSupport;
  5. public class LoginAction extends ActionSupport {
  6. /**
  7. *
  8. */
  9. private static final long serialVersionUID = 1L;
  10. private String userName;
  11. private String pwd;
  12. private String verifyCode;
  13. private String ajax;
  14. // 错误的写法
  15. @Override
  16. public String execute() throws Exception {
  17. // 通过ajax登录
  18. if (ajax != null) {
  19. HttpServletResponse response = ServletActionContext.getResponse();
  20. Writer writer = response.getWriter();
  21. writer.write("登录成功!");
  22. writer.flush();
  23. writer.close();
  24. }
  25. return SUCCESS;
  26. }
  27. // 正确写法
  28. public String login1() throws Exception {
  29. if (ajax != null) {
  30. HttpServletResponse response = ServletActionContext.getResponse();
  31. Writer writer = response.getWriter();
  32. writer.write("登录成功!");
  33. writer.flush();
  34. writer.close();
  35. return null;
  36. }
  37. return SUCCESS;
  38. }
  39. // 正确写法
  40. public String login2() throws Exception {
  41. if (ajax != null) {
  42. HttpServletResponse response = ServletActionContext.getResponse();
  43. Writer writer = response.getWriter();
  44. writer.write("登录成功!");
  45. writer.flush();
  46. writer.close();
  47. }
  48. return null;
  49. }
  50. public String getUserName() {
  51. return userName;
  52. }
  53. public void setUserName(String userName) {
  54. this.userName = userName;
  55. }
  56. public String getPwd() {
  57. return pwd;
  58. }
  59. public void setPwd(String pwd) {
  60. this.pwd = pwd;
  61. }
  62. public String getVerifyCode() {
  63. return verifyCode;
  64. }
  65. public void setVerifyCode(String verifyCode) {
  66. this.verifyCode = verifyCode;
  67. }
  68. public String getAjax() {
  69. return ajax;
  70. }
  71. public void setAjax(String ajax) {
  72. this.ajax = ajax;
  73. }
  74. }

以上为登录测试代码(Struts2),在以上示例中,如果判断为ajax!=null成立,那么一定会报如题所示的错误,原因就是:if子句里已经做了一次response,在writer.close();的时候,本次response已经完成;但是紧接着在return SUCCESS;的时候,相当于又做了一次response,所以就出错了~

类似的错误也会出现于以下代码中:

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. Writer writer = response.getWriter();
  4. writer.write("Hello");
  5. writer.flush();
  6. writer.close();
  7. response.sendRedirect("http://blog.csdn.net/baiyanglu/article/details/8076104");
  8. }

出现本错误后,web前端能够接收到第一次response的内容(就是说,挂电话之前说的话,我还是可以听到的,挂电话后讲的,当然听不到咯~),不会报错,只会在后台显示出错了

以上,转自http://www.cnblogs.com/wang3680/p/2c5f28ecfb8c2e2772474f3fab4d771c.html

Cannot call sendError() after the response has been committed - baiyangliu的更多相关文章

  1. Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  2. 报错: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 ...

  3. java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

    http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...

  4. java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读

    源代码: @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Ob ...

  5. Cannot call sendError() after the response has been committed(filter问题)

    就是因为执行了filter的dofilter方法中 chain.doFilter(request,response)了 执行了两遍 if(){}else{chain.doFilter(request, ...

  6. java.lang.IllegalStateException: Cannot call sendError() after the response has been committe

    1.问题描述 严重: Servlet.service() for servlet [default] in contextwith path [/OxygenCloud] threw exceptio ...

  7. [Java][Servlet] Cannot call sendRedirect() after the response has been committed

    做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...

  8. Cannot call sendRedirect() after the response has been committed的解决办法

    做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...

  9. Cannot forward after response has been committed

    项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...

随机推荐

  1. Go Revel - main函数分析

    运行revel命令时,首先会编译整个项目,在编译时,会根据`app.conf`配置文件生成两个源码文件`tmp/main.go`.`routes/routes.go`,其中`main.go`是整个项目 ...

  2. windows 下 MySql5.6主从复制

    说明: 1.MySql 版本5.6 2.本例中使用的主从服务器ip分别为:192.168.120.211:192.168.120.209 一.配置master服务器 1.配置 在my.ini中[mys ...

  3. 115道Java经典面试题(面中率最高、最全)

    115道Java经典面试题(面中率最高.最全) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可 ...

  4. log4写完日志不会自动释放

    今天想做个日志记录功能.网上查了下.决定使用log4net 来做. 但是最发现在写日志的时候.会一直占用当前的日志文件.当你想查看的时候会提示另一个进程占用了该文件. 只有到他创建下一个日志文件.然后 ...

  5. SQL Server创建远程链接服务器

    --使用sp_addlinkedserver增加链接 EXEC sys.sp_addlinkedserver @server='127.0.0.1', --被访问的服务器别名(习惯上直接使用目标服务器 ...

  6. 微信小程序——倒计时功能

    做小程序项目中,需要做一个倒计时功能,如下图: 记录一下实现步骤: 1.考虑到这个功能可能会有多处用到,所以把倒计时的函数写在utils.js里面: const formatNumber = n =& ...

  7. C#中二进制和流之间的各种相互转换

    一. 二进制转换成图片间的转换 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = Im ...

  8. C#里的SubString和Convert.ToDateTime

    1.C#里的SubString String.SubString(int   index,int   length) index:开始位置,从0开始 length:你要取的子字符串的长度 2.C#语言 ...

  9. Python 匿名参数

    #-*- coding:utf-8 -*- #匿名函数 #匿名函数语法格式 ''' 变量 = lambda 参数列表:表达式 ''' func = lambda x,y:x+y a = func(2, ...

  10. USB2.0学习笔记连载(十七):keil实现寄存器的配置及相关函数讲解(一)

    首先要实现对寄存器的配置,可以参考手册<Development kit  User Guide>,如下图所示: 此文件包含在 文件中.上述的应用文档详细介绍了如何利用KEIL实现对固件程序 ...