`

Cannot call sendError() after the response has been committed

阅读更多
当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://blog.csdn.net/baiyang_liu/article/details/8076104

我的问题已经解决
分享到:
评论

相关推荐

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

    在Java的Web开发中,`java.lang.IllegalStateException: Cannot call sendError() after the response has been committed` 是一个常见的错误,通常发生在尝试在HTTP响应已经发送到客户端之后调用`sendError()`方法...

    Cannot forward after response has been committed

    "Cannot forward after response has been committed" 是一个在Java Web开发中常见的错误,通常与Servlet、Filter或控制器逻辑有关。这个错误表明服务器已经完成了对HTTP响应的处理,并将其发送到客户端,然后试图...

    a project model for the FreeBSD Project.7z

    this document will outline the organisational structure (including role descriptions and communication lines), discuss the methodology model and after presenting the tools used for process control, ...

    springboot基本使用的一个文档。

    使用@SessionAttributes 和@ResponseBody 或者@RestController 注解配合使用时,可能会报 Cannot create a session after the response has been committed 错误。解决方法是在 return 之前自己创建 session。 本...

    Git-2.21.0-64-bit.zip

    * The "git fast-export/import" pair has been taught to handle commits with log messages in encoding other than UTF-8 better. * In recent versions of Git, per-worktree refs are exposed in refs/...

    servlet2.4doc

    Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...

    微软内部资料-SQL性能优化3

    For example, a second transaction traversing the doubly linked list mentioned above would see the list before or after the insert, but it will see only complete changes. Durability After a ...

    微软内部资料-SQL性能优化2

    If you attempt to access a reserved address that has not yet been committed (backed by memory or disk) you will cause an access violation. Committed Memory Committed pages are those pages that when ...

    curl for windows10 x64

    curl for windows10 x64 , version Version 7.56.1 (23 Oct 2017) Daniel Stenberg (23 Oct 2017) - RELEASE-NOTES: 7.56.1 ...- imap: if a FETCH response has no size, don't call write callback

    Django 1.1 Testing and Debugging.pdf

    What if a fix hasn't been committed yet? 366 What if a ticket has been closed without a fix? 367 Tracking down unreported problems 368 Where to ask questions 369 Tips on asking questions that will...

    HttpWebRequest出错.Section=ResponseHeader Detail=CR

     Section=ResponseHeader Detail=CR 后面必须是 LF The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF 主体意思是微软没有容忍不符合RFC 822...

    xfs_log_priv.rar_There There

    These dummy transactions get committed when everything is idle (after there has been some activity).

    中关村在线

    (Committed memory is the physical memory for which space has been reserved on the disk paging file). TOTAL_RESERVED_MEM_COUNTER_009_NAME=# Total reserved Bytes TOTAL_RESERVED_MEM_COUNTER_009_HELP=...

    Developer Express XtraGrid Suite All Sources Code

    released the first version of the XtraGrid Suite, our focus has been to deliver ground-breaking new capabilities for Visual Studio .NET®. With XtraGrid Suite v2, you have at your disposal a ...

    JSP三种跳转方式 超级好的范例

    java.lang.IllegalStateException: Can't sendRedirect() after data has been committed to the client. ``` 解决办法是在进行任何输出之前先检查是否需要重定向。 #### 二、`response.setHeader("Location", "")`...

    google repo工具

    To make edits to changes after they have been uploaded, you should use a tool like git rebase -i or git commit --amend to update your local commits. After your edits are complete: Make sure the ...

    [4] 学位英语:核心词汇(四).doc

    1. **cancel** - 取消,废除:用于取消计划或约定,如 "The flight has been cancelled due to bad weather." 2. **variable** - 易变的,可变的:形容词,表示事物可能发生变化,如 "Weather conditions are highly...

    Paul Ekman - Unmasking The Face. A guide to recognizing emotions from facial clues.part1.rar

    Ekman is a leading authority on the study of the facial expressions and their relation to emotion, and this book is a methodical and thorough (for the layperson, at least) introduction to the field, ...

    Paul Ekman - Unmasking The Face. A guide to recognizing emotions from facial clues.part2.rar

    Ekman is a leading authority on the study of the facial expressions and their relation to emotion, and this book is a methodical and thorough (for the layperson, at least) introduction to the field, ...

Global site tag (gtag.js) - Google Analytics