出现这个错误,一定是多次response导致的,例如下面这段代码就会出现此错误:
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("www.baidu.com");
}
出现本错误后,web前端能够接收到第一次response的内容,不会报错,只会在后台显示出错了
本文地址
分享到:
相关推荐
NULL 博文链接:https://m635674608.iteye.com/blog/1510545
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, ...
* 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
Section=ResponseHeader Detail=CR 后面必须是 LF The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF 主体意思是微软没有容忍不符合RFC 822...
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...
Everybody in the industry is committed to AJAX. Everybody understands the impact of it. Everybody recognizes the enormous power that can be derived from its employment in real-world solutions. Very ...
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=...
开源项目-golang-go.zip,"eliminate GOMAXPROCS limit" has just been committed to the master branch.
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 ...
Response already committed 删除websphere时,直接删除安装目录下的文件,重新安装websphere时会提示websphere已安装,是否安装副本。 WebSphere应用更新问题 配置oracle jdbc连接时,控制台测试连接成功,但应用...
Unfortunately , a crime was about to be committed but at that moment Lesley was unaware of the impending(即将发生的,迫近的) event , which would affect her life so drastically(极端地,彻底地) for the ...
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, ...