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 —————————————————————————————————————————— ...
随机推荐
- [BTS] BizTalk WCF-SQL Adapter 高级应用
9102年岁尾,41岁的我居然还在搞 BizTalk,感觉就是一种悲伤. 国内用户少之又少,能坚持一直在使用的“忠实”用户那就更少了. 不是它不好用,而是微软全线转向云服务,这个产品也已经快10年没有 ...
- pm2 常用配置项解析
1.解析 1. apps:json结构,apps是一个数组,每一个数组成员就是对应一个pm2中运行的应用 2. name:应用程序名称"app" 3. cwd:应用程序所在的目录& ...
- linux 下执行py问题提示cannot import name request
最近因为工作需要,需要在linux上去执行python代码,但是在执行的时候提示cannot import name request,我以为是导入有问题,然后我就把代码放到与包一个目录下,执行py问题 ...
- Django博客系统
零.创建项目及配置 一.编写 Model 层的代码 二.配置 admin 页面 三.根据需求定制 admin
- Hive的内部表和外部表
- Maven 官网 查找&下载 jar包& pom引用 完美方案
Maven 官网 查找&下载 jar包 & pom引用 问题描述 在我们在开发过程中,经常遇到程序中需要引用的某个版本jar包,但是在公司的私有仓库下载不到的情况. 遇到这种情况,该怎 ...
- RDD的cache 与 checkpoint 的区别
问题:cache 与 checkpoint 的区别? 关于这个问题,Tathagata Das 有一段回答: There is a significant difference between cac ...
- iOS模型输出和打印
在调试时,我们经常用到输出model,查看数据是否正确,还会在控制台"po 模型"操作,一般输出都是这样的格式的: person is <Person: 0x60800003 ...
- Unity Shader概述
一.概述 在Unity中需要配合使用材质和Unity Shader才能达到需要的效果.常见的流程:(1)创建一个材质:(2)创建一个Unity Shader,并把它赋给创建的材质:(3)把材质赋给要渲 ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...