Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常

java.lang.IllegalStateException: Cannot create a session after the response has been committed

之所以会出现此类问题是因为我们在Response输出响应后才创建Session的。

(因为那时候服务器已经将数据发送到客户端了,即:就无法发送Session ID 了)

解决办法:

1.创建访问Session的语句【request.getSession()】提前至Response输出数据之前就好了。

例如改成下面的写法OK:

ServletOutputStream out = response.getOutputStream(); // 最好这样紧挨着 response.getOutputStream() HttpSession seesion = request.getSession(); seesion.setAttribute("xxx", rand); // 输出数据 out.print("<h1>hello</h1>"); out.close();

2.如果使用了Struts2可以在struts.xml中添加一个默认的拦截器:

<interceptor-ref name="createSession"/>             <interceptor-ref name="defaultStack"/>

时间: 2024-10-10 21:28:40

Cannot create a session after the response has been committed的相关文章

有时候在操作Session时,系统会抛出如下异常:java.lang.IllegalStateException: Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常 java.lang.IllegalStateException: Cannot create a session after the response has been committed 原因1: Session 的创建语句: HttpSession seesion = request.getSession(); 之前有Response的输出语句. 应该把HttpSession seesion = request.getSession(); 放

Cannot create a session after the response has been committed解决方案

场景: @RequestMapping("/demo")     public void doTmallGujia(String callbackUrl, HttpServletResponse response){         try {             if(StringUtils.isNotBlank(callbackUrl)){                 callbackUrl = URLDecoder.decode(callbackUrl, "UT

Cannot create a session after the response has been committed的处理

情景:在导出excel导出过程中前台提示"正在导出,请稍等...",导出结束后提示信息自动消失. 但导出结束后就报如上错误,不可能相应两次. 解决:主要是判断导出结束的时间点,确认导出结束时间.通过cookie解决,设置cookie if(fileName.contains("日志数据")){ Cookie status = new Cookie("exportStatus","success"); status.setMax

Cannot forward after response has been committed

项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDispatcher("admin.jsp").forward(request, response); 解决方法: 原代码:request.getRequestDispatcher("admin.jsp").forward(request, response); 修改成:删除

java.lang.IllegalStateException: Cannot forward after response has been committed

jjava.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispat

解决Cannot forward after response has been committed

在是用request.getRequestDispatcher("目标地址URL").forward(request, response);时报错Cannot forward after response has been committed 字面意识是response已经提交过了不能再次提交,多次试了以后发现我的代码中有用了几次request.getRequestDispatcher("目标地址URL").forward(request, response);语句

Laravel表单篇----request请求,session存储,response响应以及中间件

请求Request 取值(传入参数Request $request) $request -> input('name');(请求name值) $request -> input('sex', '未知');(也可以请求未被定义的参数) $request -> all();(得到所有请求值) 判断取值类型 $request -> method();(判断请求类型) $request -> isMethod('GET');(判断是否为get请求) $request -> aj

Cannot forward after response has been committed 问题

1.问题的出现:当用户进入主页的时候,我需要判断是来着移动端还是pc端,于是我用了拦截器,拦截所有请求: 2.操作:1)mvc配置文件<mvc:interceptor>                                  <mvc:mapping path="/**" />                                            <bean class="com.thinkgem.jeesite.mod

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

源代码: @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String uri = request.getRequestURI(); if(pathMatcher.match("/", uri)) { System.err.println("跳转"); resp