做一个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的解决办法的更多相关文章

  1. Cannot call sendRedirect() after the response has been committed

    AJAX+JSP时,out.write('content')之后,如果后面还有代码,无法被执行到,会报 错,java.lang.IllegalStateException: Cannot call s ...

  2. Java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

    在使用response重定向的时候,报以下错误:Java.lang.IllegalStateException: Cannot call sendRedirect() after the respon ...

  3. Cannot call sendRedirect() after the response has been committed错误;

    Cannot call sendRedirect() after the response has been committed提示信息其实很清楚,如果response已经提交过了,就无法再发送sen ...

  4. [Java][Servlet] Cannot call sendRedirect() after the response has been committed

    做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...

  5. Servlet中response.sendRedirect()跳转时不能设置target的解决办法

    一般使用Struts2的拦截器(或者是filter)验证是否登录的时候,如果用户没有登录则会跳转到登录的页面.这时候一般可以在拦截器或者filter中用response.sendRedirect(). ...

  6. jsp页面出错 Cannot call sendRedirect() after the response has been committed

    sendRedirect()不能多次调用,检查下代码

  7. swagger出现no response from server错误的解决办法

    解决办法:1.启用80端口2.如果不是使用的80端口,是用的nginx做了映射的其他端口的话可以用Springfox swagger-ui 覆盖默认request host,加上这个在spring的应 ...

  8. Cannot forward after response has been committed

    项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...

  9. java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

    http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...

随机推荐

  1. MAC下STF安装及踩坑

    [Mac OS X]brew: command not found ruby -e"$(curl -fsSL https://raw.githubusercontent.com/Homebr ...

  2. legend3---11、php前端模块化开发

    legend3---11.php前端模块化开发 一.总结 一句话总结: 把常用的前端块(比如课程列表,比如评论列表)放进模块列表里面,通过外部php变量给数据,可以很好的实现复用和修改 页面调用 @p ...

  3. Appnium安装

    Refer to https://blog.csdn.net/xgh1951/article/details/85124327

  4. JVM 内存溢出(转载~)

    对于JVM的内存写过的文章已经有点多了,而且有点烂了,不过说那么多大多数在解决OOM的情况,于此,本文就只阐述这个内容,携带一些分析和理解和部分扩展内容,也就是JVM宕机中的一些问题,OK,下面说下O ...

  5. Actuator Elasticsearch healthcheck error

    1. 相关环境 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  6. Python异步IO之协程(二):使用asyncio的不同方法实现协程

    引言:在上一章中我们介绍了从yield from的来源到async的使用,并在最后以asyncio.wait()方法实现协程,下面我们通过不同控制结构来实现协程,让我们一起来看看他们的不同作用吧- 在 ...

  7. 监控数据库DDL操作日志

    背景 为了监控好生产环境下各个数据库服务器上DDL操作日志,便于运维工程师管控好风险,我们有必要关注当前实例下的所有的DDL操作以及对应的IP和hostname. 测试环境 Microsoft SQL ...

  8. 专业写博一天------ArrayList 线程安全

    首先我们要了解什么是线程安全: 首先我们要明白线程的工作原理,jvm有一个main  memory ,而每个线程有自己的working memory,一个线程对一个variable  进行操作时,都要 ...

  9. httpd配置Rewrite 301 302

    在系统做一些大的.比较耗时的发布的时候,往往需要停服很长时间,这期间有用户访问的话,就需要展示一个升级说明的页面,这个页面放在反向代理服务器中:反向代理服务器如httpd有请求URL重写模块,通过它可 ...

  10. Spring Boot默认日志logback配置解析

    前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...