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 ...
随机推荐
- php设计模式总结-单件模式
一.单件模式 英文叫做sington.其他语言中有叫做单例模式,其实都是一样的道理.保证只会出现单个实例,所以是单例.翻译成单件,永远只会产生一件,呵呵. 还有翻译成单元素模式.其实关键是看这个英文比 ...
- Android——ViewPager滑动背景渐变(自定义view,ViewPager)
效果: ActivityBackgroundImage,java(自定义视图) package com.example.chenshuai.test322; import android.conten ...
- strace命令用法详解
Linux利器 strace strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必 ...
- ffmpeg 将jpg转为yuv
ffmpeg -i temp.jpg -s 1024x680 -pix_fmt yuvj420p 9.yuv 此前网上有条盛传的命令 -y -s出来uv是错的
- C#7.0新语法
一.out输出参数 在以前使用out输出参数的时候,必须先定义变量,然后才能使用,例如: 先定义一个方法,方法参数是out类型的输出参数: private void DoNoting(out int ...
- 三篇文章了解 TiDB 技术内幕——说存储
数据库.操作系统和编译器并称为三大系统,可以说是整个计算机软件的基石.其中数据库更靠近应用层,是很多业务的支撑.这一领域经过了几十年的发展,不断的有新的进展. 很多人用过数据库,但是很少有人实现过一个 ...
- MinGW下编译openssl, json-c
目的:在windows环境下,编译开源库openssl 环境:windows 10 ,Mingw及自带msys工具,openssl-1.0.2j 工具主要使用MinGW(含msys1.0), IDE选 ...
- vnc server on Ubuntu
Virtual Network Computing(VNC)是进行远程桌面控制的一个软件.客户端的键盘输入和鼠标操作通过网络传输到远程服务器,控制服务器的操作 (只有背景,没有菜单栏问题没有解决) ...
- 五、excel末尾补0和开头补0
1.末尾补0 假设数据在A列,则在B1输入以下公式 如果长度不足10,在后面加0,否则等于A1 (1)=IF(LEN(A1<17),LEFT(A1&"0000000000&qu ...
- windows下 删除指定文件夹里面一周前的所有文件和文件夹的bat
forfiles /p "指定文件夹路径" /m * /s /d -7 /c "cmd /c if @isdir==TRUE (rd /q @path) else del ...