[Java][Servlet] 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(..),程序正常运行。
[Java][Servlet] Cannot call sendRedirect() after the response has been committed的更多相关文章
- 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
AJAX+JSP时,out.write('content')之后,如果后面还有代码,无法被执行到,会报 错,java.lang.IllegalStateException: Cannot call s ...
- Cannot call sendRedirect() after the response has been committed错误;
Cannot call sendRedirect() after the response has been committed提示信息其实很清楚,如果response已经提交过了,就无法再发送sen ...
- Cannot call sendRedirect() after the response has been committed的解决办法
做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...
- jsp页面出错 Cannot call sendRedirect() after the response has been committed
sendRedirect()不能多次调用,检查下代码
- 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 ...
- Java Servlet规范
截自网址:http://blog.csdn.net/u010391029/article/details/46521051 JavaServlet Specification Version 2.3 ...
- java servlet 几种页面跳转的方法及传值
java servlet 几种页面跳转的方法及传值 java web 页面之间传值有一下这几种方式1.form 表单传递参数2.url地址栏传递参数3.session4.cookie5.appli ...
- Java Servlet完全教程
Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servle ...
随机推荐
- [51nod1188]最大公约数之和 V2(筛法)
题面 传送门 题解 口胡的整除分块单次询问\(O(\sqrt{n})\)的做法居然\(T\)了?那还是好好看正解吧-- 首先我们枚举\(j\),求对于每个\(j\)有所有\(i<j\)的\(\g ...
- asp.net core 简化模型验证 modelState.IsValid不用每一个写
第一种:直接在执行action之前验证模型 实现 IActionFilter public class ModelStateFilter : IActionFilter { public void O ...
- 7.Palindrome Linked List(回文链表)
Level: Easy 题目描述: Given a singly linked list, determine if it is a palindrome. Example 1: Input: ...
- python编程技巧
- ERROR (UnicodeEncodeError): 'ascii' codec can't encode character u'\uff08' in position 9: ordinal not in range(128)
环境win10+anaconda2 在安装labelme时遇到了这个问题,其实跟labelme没啥关系,主要是python2读取中文路径时报错,因为默认编码是ASCII,不认识中文,看到有一个一次性解 ...
- 解读 pytorch对resnet的官方实现
地址:https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py 贴代码 import torch.nn as ...
- js 遍历tree的一个例子
小例子蕴含着到力量,我希望自己有改变自己生活的能力,自己有主宰自己命运的能力... 备注:这个例子有问题. <!DOCTYPE html> <html lang="en&q ...
- Java 实现栈,队列
package base.structure; /** * @program: Algorithm4J * @description: 实现一个Stack * @author: Mr.Dai * @c ...
- 邮件服务器启动postfix时的问题:master 已死,但 pid 文件仍存
[root@linux115 ~]# service postfix restart // 重启postfix关闭 postfix:[失败] 启动 postfix: [确定][root@linux ...
- CodeForces-Zuhair and Strings(思维+枚举)
Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string ss has a level xx, if xx is l ...