做一个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. java基础之变量和常量、类型转换

    一.     变量 变量是可改变的量,每赋个值便会开辟一个新内存地址. 1.首先,变量需要一个声明,例如:int a,这个a也可以当作是一个标签,它指向了一个内存地址,这个地址是属于int类型的套餐, ...

  2. uoj#420. 【集训队作业2018】矩形(组合数学)

    题面 传送门 题解 这辣鸡题目做了咱整整三天--咱果然还是太菜了--好珂怕的推倒啊-- 首先把它变成 \[\left( \sum_{i = 1}^{n} \sum_{j = 1}^{m} F(i, j ...

  3. js window.open()打开的页面关闭后刷新父页面

    function test(){ var winObj = window.open(URL); var loop = setInterval(function(){ if(winObj.closed) ...

  4. Mysql 别名

    一.列别名 1.要给列添加别名,可以使用AS关键词后跟别名,例:SELECT [column_1 | expression] AS descriptive_name FROM table_name; ...

  5. P1979 华容道

    题意:$n*m$棋盘上$n*m-1$颗棋子,有且只有一个格子为空白格子,每个棋子大小$1*1$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...

  6. Gym - 101845D 计算几何

    Ignacio is a famous mathematician, some time ago he was married with Dolly, a famous scientific, at ...

  7. ubuntu下vnstat监控网卡流量

    vnstat使用 vnstat 是另一个可以用来监视带宽使用量的程序.它比ipac-ng更简单易用. vnstat的一个优点是它不是一个运行的守护程序,所以它几乎不占用内存.它由 cron 任务创建, ...

  8. Java 标准IO高可用类

    输出: 使用: * <pre> * public class TestStdOut { * public static void main(String[] args) { * int a ...

  9. Mysql tips 功能...

    1. mysql  GROUP_CONCAT() 使用 排序... SELECT shop.id, shop.name, shop.user_id, shop.address, shop.map_lo ...

  10. Java高级工程师应该掌握的东东

    今天偶然看了膜拜单车官网对java程序员的招聘要求,如下,可以对照发现自己的不足 职责 负责APP SERVER中间层等模块开发 完成各类需求开发任务,同时保证服务稳定性.茁壮性 要求 精通Java语 ...