做一个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(..),程序正常运行。

[Java][Servlet] Cannot call sendRedirect() after the response has been committed的更多相关文章

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

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

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

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

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

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

  4. Cannot call sendRedirect() after the response has been committed的解决办法

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

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

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

  6. idea调试SpringMvc, 出现:”javax.servlet.ServletException: java.lang.IllegalStateException: Cannot create a session after the response has been committed"错误的解决方法

    调试拦截器出现以下错误: HTTP Status 500 - javax.servlet.ServletException: java.lang.IllegalStateException: Cann ...

  7. Java Servlet规范

    截自网址:http://blog.csdn.net/u010391029/article/details/46521051 JavaServlet Specification  Version 2.3 ...

  8. java servlet 几种页面跳转的方法及传值

    java servlet 几种页面跳转的方法及传值   java web 页面之间传值有一下这几种方式1.form 表单传递参数2.url地址栏传递参数3.session4.cookie5.appli ...

  9. Java Servlet完全教程

    Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servle ...

随机推荐

  1. 使用jenkins来跑docker image的惨痛经历

    记录一下我使用jenkins来跑docker container的艰辛路程吧,是照着jenkins官网的[文档](https://jenkins.io/doc/tutorials/build-a-ja ...

  2. day02.2-常用Linux命令整理

    Linux命令语法格式:   命令    [选项]    [参数] 1). 命令:告诉Linux操作系统执行什么: 2). 选项:说明命令的运行方式.选项部分是以字符‘-’开始的: 3). 参数:说明 ...

  3. 大佬写的js生成玫瑰(来源网络)

    <!DOCTYPE html> <html> <head> <title>js html5渲染的3D玫瑰花(程序员的情人节礼物)</title&g ...

  4. vuejs项目性能优化总结

    在使用elementUI构建公司管理系统时,发现首屏加载时间长,加载的网络资源比较多,对系统的体验性会差一点,而且用webpack打包的vuejs的vendor包会比较大.所以通过搜集网上所有对于vu ...

  5. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  6. sublime安装插件autoprefixer

    安装插件autoprefixer步骤: 1.确保Node.js已经安装,未安装请 点击 这里>> 2.下载autoprefixer插件 https://github.com/sindres ...

  7. phaser小游戏框架学习(二)

    今天继续学习phaser.js.上周写的学习教程主要内容是创建游戏场景,游戏中的显示对象,按钮对象的使用以及如何在不同屏幕大小中完美适配.这篇博客以介绍游戏榜单的渲染更新为主. 代码地址:https: ...

  8. Django 自定义模板标签 报错django.template.exceptions.TemplateSyntaxError: '####' is not a registered tag library. Must be one of:

    我写代码遇到这个错误,但是发现程序没有写错,好像是程序有缓存,重新运行几次就好了. 自定义模板标签,可以不用写views,url直接通过自定义函数把变量传给模板. 具体实现: 1.在app下新建Pyt ...

  9. jvm与tomcat启动优化配置

    JVM 优化 Java 的内存模型分为: Young,年轻代(易被 GC).Young 区被划分为三部分,Eden 区和两个大小严格相同的 Survivor 区,其中 Survivor 区间中,某一时 ...

  10. springcloud系列八 整合Hystrix

    feign本身是支持Hystrix的,所以不需要引入其他依赖: 我们可以看看feign这个项目的依赖,就是引入这个依赖的pom.xml 要想看这个很简单,点击那个依赖进去就可以了 点进去就可以看到 & ...