Cannot call sendRedirect() after the response has been committed的解决办法
做一个Login Demo的时候,写了如下代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
String userName = request.getParameter("userName");
String password = request.getParameter("password");
if (userName.equals("test")) {
response.sendRedirect(request.getContextPath() + "/success/success.jsp");
} else {
response.sendRedirect(request.getContextPath() + "/error/login-failed.jsp");
}
}
运行程序时报错了Cannot call sendRedirect() after the response has been committed,意思已经很明确了,就是说不能在一个response做了提交之后再重定向,也就是说再重定向之前不要去改变response,包括如下部分:
- 修改response中参数的值,cookie,session 等
- 重定向
请注意在做doPost(..)的时候我这里调用了super.doPost(..),而super.doPost(..)做了什么呢?
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
response被修改了,我们注释掉super.doPost(..),程序正常运行。
Cannot call sendRedirect() after the response has been committed的解决办法的更多相关文章
- Cannot call sendRedirect() after the response has been committed
AJAX+JSP时,out.write('content')之后,如果后面还有代码,无法被执行到,会报 错,java.lang.IllegalStateException: Cannot call s ...
- Java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
在使用response重定向的时候,报以下错误:Java.lang.IllegalStateException: Cannot call sendRedirect() after the respon ...
- Cannot call sendRedirect() after the response has been committed错误;
Cannot call sendRedirect() after the response has been committed提示信息其实很清楚,如果response已经提交过了,就无法再发送sen ...
- [Java][Servlet] Cannot call sendRedirect() after the response has been committed
做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...
- Servlet中response.sendRedirect()跳转时不能设置target的解决办法
一般使用Struts2的拦截器(或者是filter)验证是否登录的时候,如果用户没有登录则会跳转到登录的页面.这时候一般可以在拦截器或者filter中用response.sendRedirect(). ...
- jsp页面出错 Cannot call sendRedirect() after the response has been committed
sendRedirect()不能多次调用,检查下代码
- swagger出现no response from server错误的解决办法
解决办法:1.启用80端口2.如果不是使用的80端口,是用的nginx做了映射的其他端口的话可以用Springfox swagger-ui 覆盖默认request host,加上这个在spring的应 ...
- Cannot forward after response has been committed
项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...
随机推荐
- 解惑spring事务传播特性之嵌套事务
/** * Support a current transaction, create a new one if none exists. * Analogous to EJB transaction ...
- R语言:载入rjava(xlsx)包报错
先安装JRE,在电脑中添加环境变量: 电脑-右键-属性-高级系统设置-环境变量-用户变量下新建:变量名:JAVA-HOME,变量值:JRE安装路径(到jre1.8***这个文件夹就行了) 系统变量下找 ...
- HearthBuddy 突袭 rush
https://hearthstone.gamepedia.com/Rush Rush is an ability allowing a minion to attack other minions ...
- nginx安装(windows下)及配置
1. 正向代理和反向代理 正向代理是一个位于客户端[用户A]和原始服务器[服务器B]之间的服务器[代理服务器Z],为了从原始服务器取得内容,用户A向代理服务器Z发送一个请求并指定目标(服务器B),然后 ...
- P1115 最大子段和&P1719 最大加权矩形
上接:DP&图论 DAY 1 上午 这两个题本质是一个亚子,所以放一起啦 DPDPDPDPDPDPDPDP P1115 最大子段和 题解 因为题目要求的是一段连续的区间,所以前缀和搞暴力??? ...
- ios-runtime拦截touch事件,展示用户触摸效果
[展示效果说明] 对 app 操作录屏时,展示出手指在 app 上的触摸效果可以看到具体点击情况,感觉这样比较直观方便,也不用做视频后期了. 这里简单用 runtime 实现了一个这样的效果,不需要修 ...
- Android:JNA实践(附Demo)
一.JNA和JNI的对比 1.JNI的调用流程 Android应用开发中要实现Java和C,C++层交互时,想必首先想到的是JNI,但是JNI的使用过程十分繁琐,需要自己再封装一层JNI接口进行转 ...
- [Ubuntu]18安装百度网盘
1.下载客户端 下载地址: 选择linux版本,我选择的是deb格式,下载就可以了. 2.安装 进入下载目录,点击右键,选择在终端打开. 之后输入 以下代码愉快的安装就好了 注意:dpkg后面跟的文 ...
- python 生成词云
1.知识点 """ WordCloud参数讲解: font_path表示用到字体的路径 width和height表示画布的宽和高 prefer_horizontal可以调 ...
- Maven exclusions(排除依赖)
在写pom的时候,我们写的一个依赖往往会依赖于其他的包,而这些包可能是过时的不安全的,因此需要排除并重新引用安全的版本,先在依赖这个项目的pom中去除想排除的依赖,再添加指定版本的依赖. pom的依赖 ...