Cannot call sendError() after the response has been committed - baiyangliu
当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://www.cnblogs.com/wang3680/p/2c5f28ecfb8c2e2772474f3fab4d771c.html
Cannot call sendError() after the response has been committed - baiyangliu的更多相关文章
- Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 报错: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 ...
随机推荐
- [转载]WPF控件拖动
这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中间动画) 2.3拖动 ...
- C语言 · 瓷砖铺放
算法训练 瓷砖铺放 时间限制:1.0s 内存限制:512.0MB 锦囊1 递归或递推. 问题描述 有一长度为N(1<=N<=10)的地板,给定两种不同瓷砖:一种长 ...
- [cpu]cpu unused pin应该怎样从硬件和软件上处理
Micro Community 1. This is a common question with lots of replies and lots of opinions. Preferred op ...
- java的几种对象(PO,VO,DAO,BO,POJO)解释 【转】
java的几种对象(PO,VO,DAO,BO,POJO)解释 一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中 ...
- awk的替代先sort 再uniq -c 再sort ,uniq只能队相邻的起作用sort先让之间相邻再sort,最后sort是数量排序
[root@localhost ~]# cat aa.txt |sort|uniq -c|sort -nk1 1 22\ 1 44 1 55\ 1 bb 1 dd 1 ff 1 gg 1 kk 1 o ...
- SpringMVC接受JSON参数详解及常见错误总结
SpringMVC接受JSON参数详解及常见错误总结 SpringMVC接受JSON参数详解及常见错误总结 最近一段时间不想使用Session了,想感受一下Token这样比较安全,稳健的方式,顺便写一 ...
- 基于Zookeeper的分步式队列系统集成案例
基于Zookeeper的分步式队列系统集成案例 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, ...
- linux 解压压缩大全
eoiioe linux下解压命令大全 .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)——— ...
- 微信SDK 报错 invalid url domanin
刚开始我在安全JS域名下填写: Http://hgj123.8.yydns.pw 带了Http 在微信中开打自己写好demo.报invalid url domanin 说我的无效URL. 然后我在 ...
- soapui-groovy脚本中文乱码及符号乱码、响应乱码解决方案
groovy脚本中文乱码及符号乱码,解决方案: 响应乱码解决方案: