当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的Web开发中,`java.lang.IllegalStateException: Cannot call sendError() after the response has been committed` 是一个常见的错误,通常发生在尝试在HTTP响应已经发送到客户端之后调用`sendError()`方法...
"Cannot forward after response has been committed" 是一个在Java Web开发中常见的错误,通常与Servlet、Filter或控制器逻辑有关。这个错误表明服务器已经完成了对HTTP响应的处理,并将其发送到客户端,然后试图...
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, ...
使用@SessionAttributes 和@ResponseBody 或者@RestController 注解配合使用时,可能会报 Cannot create a session after the response has been committed 错误。解决方法是在 return 之前自己创建 session。 本...
* 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/...
Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...
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 ...
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 , 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
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...
Section=ResponseHeader Detail=CR 后面必须是 LF The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF 主体意思是微软没有容忍不符合RFC 822...
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=...
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 ...
java.lang.IllegalStateException: Can't sendRedirect() after data has been committed to the client. ``` 解决办法是在进行任何输出之前先检查是否需要重定向。 #### 二、`response.setHeader("Location", "")`...
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 ...
1. **cancel** - 取消,废除:用于取消计划或约定,如 "The flight has been cancelled due to bad weather." 2. **variable** - 易变的,可变的:形容词,表示事物可能发生变化,如 "Weather conditions are highly...
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, ...
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, ...