body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;}html, body{ }h1 { font-size:1.5em; font-weight:bold;}h2 { font-size:1.4em; font-weight:bold;}h3 { font-size:1.3em; font-weight:bold;}h4 { font-size:1.2em; font-weight:bold;}h5 { font-size:1.1em; font-weight:bold;}h6 { font-size:1.0em; font-weight:bold;}img { border:0; max-width: 100%;}blockquote { margin-top:0px; margin-bottom:0px;}table { border-collapse:collapse; border:1px solid #bbbbbb;}td { border-collapse:collapse; border:1px solid #bbbbbb;}Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET

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

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

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

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

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

  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.   
  8.             response.sendRedirect("http://blog.csdn.net/baiyanglu/article/details/8076104");  
  9.         }  
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的内容(就是说,挂电话之前说的话,我还是可以听到的,挂电话后讲的,当然听不到咯~),不会报错,只会在后台显示出错了

Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET的更多相关文章

  1. Cannot call sendError() after the response has been committed - baiyangliu

    当response提交后,不能调用sendError(),什么意思? 出现这个错误,一定是多次response导致的.可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你 ...

  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. Inno Setup入门(十五)——Inno Setup类参考(1)

    分类: Install Setup 2013-02-02 11:27 536人阅读 评论(0) 收藏 举报 nno setup脚本能够支持许多的类,这些类使得安装程序的功能得到很大的加强,通过对这些类 ...

  2. XHTML 与 HTML 之间的差异

    最主要的不同: XHTML 元素必须被正确地嵌套. XHTML 元素必须被关闭. 标签名必须用小写字母. XHTML 文档必须拥有根元素.

  3. MySql 加锁问题

    1.设置非自动提交 set autocommit=0;  这时候 for update才会起作用 2.一般用法 set autocommit=0;  for update(加锁)  ;  commit ...

  4. LightOJ 1282 Leading and Trailing 数论

    题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...

  5. IO流---字符流(FileWriter, FileReader ,BufferedWriter,BufferedReader)

    IO   Input  Output IO流用来处理设备之间的数据传输. java对数据的操作是通过流来实现的. 流按流向分:输入流,输出流     是相对内存而言的.把硬盘的数据读取到内存中就是输入 ...

  6. Power oj2470/DFS

    题目链接 2469: C 小Y的难题(1) Time Limit: 1000 MS Memory Limit: 65536 KB Total Submit: 9 Accepted: 7 Page Vi ...

  7. 10317 Fans of Footbal Teams(并查集)

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description ...

  8. 更换arm-linux-gcc 4.3.2编译器

    先创建一个临时目录:mcx@mcx-virtual-machine:/home/work/tools$ mkdir tmp 解压到根目录:mcx@mcx-virtual-machine:/home/w ...

  9. UVALive 2323 Modular Multiplication of Polynomials(模拟)

    这是一个相对简单的模拟,因为运算规则已经告诉了我们,并且比较简单,不要被吓到…… 思路:多项式除以另外一个多项式,如果能除,那么他的最高次一定被降低了,如果最高次不能被降低,那说明已经无法被除,就是题 ...

  10. js所有函数集合

    lick() 对象.click() 使对象被点击. closed 对象.closed 对象窗口是否已关闭true/false clearTimeout(对象) 清除已设置的setTimeout对象 c ...