当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. How Not to Crash #3: NSNotification通知引起的崩溃

    How Not to Crash #3: NSNotification通知引起的崩溃html, body {overflow-x: initial !important;}html { font-si ...

  2. CentOS6.3升级Python到2.7.3版本

    http://www.zhangchun.org/the-centos6-3-upgrade-python-to-2-7-3-version/ 查看python的版本 1 python  -V  2 ...

  3. Android keytool 生成keystore与给APK签名

    http://www.tttabc.com/android/keytool-keystore-jarsigner-apk.htm

  4. PHP——大话PHP设计模式——命名空间和类的自动载入

    开发工具:phpstorm phpstudy 命名空间:声明当前文件 类的自动载入

  5. mysql 慢查询时间

    只有当一个query执行时间(不包括锁等待的时间)>long_query_time的时候,才会判定会slow log: 但是判定为slow log之后,输出包括Query_time(执行时间+锁 ...

  6. sql 字符带下划线匹配问题

    SQL 中 _下划线 作用是 匹配一个任意字符. 如果我们要去掉下划线的作用 单纯只用作一个字符则需要转义成    like '%\_%' escape '\' 字段 1.order_qrsc 2.o ...

  7. websocket 和 socket.io 之间的区别是什么

    socket.io封装了websocket,同时包含了其它的连接方式,比如Ajax.原因在于不是所有的浏览器都支持websocket,通过socket.io的封装,你不用关心里面用了什么连接方式.你在 ...

  8. 在ASP.NET CORE下生成PDF文档

    原文链接:https://www.c-sharpcorner.com/article/creating-pdf-in-asp-net-core-mvc-using-rotativa-aspnetcor ...

  9. sparkr脚本

    test <- function(){ print(1) } test() 脚本内为单独函数时 调用脚本不执行 所以要在脚本内调取特定函数

  10. Self20171218_Assert断言使用

     一.Assert用法: assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制.在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行 ...