Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET
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连接已经关闭了,而你还试图发送数据给客户端,显然会出错。就好比我俩打电话,我都挂电话了,你还在“喂喂喂”。
例如下面这段代码就会出现此错误:
- 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;
- }
- }
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");
- }
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的更多相关文章
- Cannot call sendError() after the response has been committed - baiyangliu
当response提交后,不能调用sendError(),什么意思? 出现这个错误,一定是多次response导致的.可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你 ...
- 报错: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 ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读
源代码: @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Ob ...
- Cannot call sendError() after the response has been committed(filter问题)
就是因为执行了filter的dofilter方法中 chain.doFilter(request,response)了 执行了两遍 if(){}else{chain.doFilter(request, ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committe
1.问题描述 严重: Servlet.service() for servlet [default] in contextwith path [/OxygenCloud] threw exceptio ...
- [Java][Servlet] Cannot call sendRedirect() after the response has been committed
做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...
- Cannot call sendRedirect() after the response has been committed的解决办法
做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...
- Cannot forward after response has been committed
项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...
随机推荐
- inno setup 1
1.简单脚本 [setup] AppName=Test AppVerName=Test DefaultDirName="d:\setup\app" AppVersion=1.0 ...
- 使用HAXM加速Android虚拟机
Android虚拟机在支持Intel VT技术的CPU上,可以使用HAXM(Hardware Accelerated Execution Manager)得到硬件加速支持,使得虚拟机运行速度得到极大提 ...
- js中Object.__proto__===Function.prototype
参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...
- 关于web.xml的格式
先是filter 再是<filter-mapping> 然后<servlet> 再是<servlet-mapping> 这是一种规范基于j2ee 在开发的过程中一 ...
- 【素数】 poj 2739 一个数能有多少种连续素数相加方案
简单题 素数打表 根据数据量 用n2算法遍历 开一个save[k]素数存前k个素数和即可. #include <iostream> #include <cstdio> ...
- CodeForces 709B Checkpoints 模拟
题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. #include&l ...
- 在写一个iOS应用之前必须做的7件事
转载自:http://www.cocoachina.com/ios/20160316/15685.html 原文:https://medium.com/ios-os-x-development/7-t ...
- CodeForces 687A NP-Hard Problem(二分图判定)
这本来一个挺简单的题呢,结果让我给想复杂了,二分图就是把图分成了两部分,然后不同颜色各一边,肯定是满足题目中说的边和点的条件的,真是犯二了.. 代码如下: #include<iostream&g ...
- 在 Linux 系统中安装Load Generator ,并在windows 调用方法
在 Linux 系统中安装Load Generator ,并在windows 调用 由于公司需要测试系统的最大用户承受能力,所以需要学习使用loadrunner.在安装的时候碰到了不少问题,所以写下此 ...
- js 判断网页类型
<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>标题页</title ...