关于 Cannot forward after response has been committed的错误
这个错误顾名思义:就是在给客户端做出提交响应完毕后,就不能再次使用request的转发。
代码原先是这样的:
package com.bdqn.jsp.study.web.filter; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebFilter(filterName = "checkLoginFilter", urlPatterns = "/*")
public class CheckLoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (request.getRequestURI().contains("/user")) {
Object value = request.getSession().getAttribute("user");
if (value == null) {
response.sendRedirect(request.getContextPath() + "/login.jsp");
//return;
}
}
filterChain.doFilter(request, response);
} @Override
public void destroy() { }
}
注意这里是个filter主要作用:用于判断用户是否登录后才能访问相关页面,地址栏里有“/user”的地址信息,就认为改地址是登录后才能进行访问
然而在某一个servlet里有如下相关代码:
if (returnValue != null) {
request.getRequestDispatcher("/" + returnValue.toString()).forward(request, response);
}
如果这么写就出问题了,因为在filter里response做出重定向后,还要执行doFilter方法,因此当执行servlet中的转发操作时就会报错了,因为在response.sendRedirect()会认为已经给response的commited属性设置成true了,因此不能再进行转发
关于 Cannot forward after response has been committed的错误的更多相关文章
- Cannot forward after response has been committed问题的解决
Cannot forward after response has been committed问题解决及分析 通过TOMCAT把系统启动,可以正常登陆门户,登陆进去选择子系统的时候点击登陆的时候,可 ...
- Cannot forward after response has been committed
项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...
- java.lang.IllegalStateException: Cannot forward after response has been committed的一个情况解决方法
java.lang.IllegalStateException: Cannot forward after response has been committed xxx.xxx.doPost(upd ...
- nested exception is java.lang.IllegalStateException: Cannot forward after response has been committed
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...
- java.lang.IllegalStateException: Cannot forward after response has been committed
jjava.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.cat ...
- Cannot forward after response has been committed 错误
出现该错误的原因是:页面的跳转控制不好,换句话就是说程序的逻辑控制不好,导致了程序顺序执行的时候多次跳转到同一页面,有的程序员建议用多次使用return语句来返回,但是个人认为最好的还是自己要先理清页 ...
- Cannot call sendRedirect() after the response has been committed错误;
Cannot call sendRedirect() after the response has been committed提示信息其实很清楚,如果response已经提交过了,就无法再发送sen ...
- 报错:java.lang.IllegalStateException: Cannot call sendError() after the response has been committed(待解答)
严重: Servlet.service() for servlet [default] in context with path [/20161101-struts2-1] threw excepti ...
- grails中报Cannot create a session after the response has been committed异常的解决办法
在grails中,我们在layouts\main.gsp中使用类似如下的代码来判断当前用户处于登录状态时显示相关的登录信息: <g:if test="${session.users}& ...
随机推荐
- windows 10下通过python3.6成功搭建jupyter 服务器
最近通过python学习爬虫技术,发现一个工具jupyter notebook很不错,该工具明显优势通过浏览器可以输入多行python代码,支持在线运行以及运行结果保存功能,在线验证python小模块 ...
- C 函数指针与回调函数
函数指针是指向函数的指针变量. 通常我们说的指针变量是指向一个整型.字符型或数组等变量,而函数指针是指向函数. 函数指针可以像一般函数一样,用于调用函数.传递参数. 函数指针变量的声明: #inclu ...
- 用Vue.js开发微信小程序:开源框架mpvue解析
前言 mpvue 是一款使用 Vue.js 开发微信小程序的前端框架.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为 H5 和小程序提供了代码复用的能力.如果想将 H5 项目改造为小程 ...
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like '%逸%'' at line 1
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- Mysql数据库mys和ora库的备份与恢复脚本
!/bin/bash Time=$(date +%Y%md%H%M%S) Back_dir="$HOME/mysqlback/${Time}" function Detect_u_ ...
- 使用 HttpClient3.1 和 HtmlParser2.1 开发Crawler
https://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/
- Linux CentOS7.0 (01)在Vmvare Workstation上 安装配置
一.新建虚拟机 1.创建新的虚拟机 -> 默认典型 ->选择安装介质 2.指定虚拟机名称.安装目录.磁盘容量大小 点击 "完成",创建虚拟机! 随后虚拟机将自动启动安装 ...
- SpringCloud的服务注册中心(一)
一.概念和定义 1.服务治理:服务注册与服务发现 服务注册中心,提供服务治理功能,用来实现各个微服务实例的自动注册与发现. 服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,维护人员就不需 ...
- python 类知识点总结
python 类知识点总结 面向对象思想: 1.设计的时候,一定要明确应用场景 2.由对象分析定义类的时候,找不到共同特征和技能不用强求 1.简述类.对象.实例化.实例这些名词的含义: 类:从一组对象 ...
- jacascript DOM变动事件
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! DOM变动事件 变动事件(MutationEvent)能在DOM中的某一部分发生变化时给出提示,这类事件非 ...