为了账号安全,请及时绑定邮箱和手机立即绑定

执行错误?

代码:

package FirstFilter;


import java.io.IOException;


import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/*

 *  导入的jar包是javax.servlet.*;

 *  step:1-----

 *  在web.xml文件中配置,实例化一个Filter实例

*/


public class firstFilter implements Filter{

   //step:4

public void destroy() {

// TODO Auto-generated method stub

System.out.println("服务器关闭,销毁Filter  执行destory方法");

}

    //step:3

public void doFilter(ServletRequest arg0, ServletResponse arg1,

FilterChain arg2) throws IOException, ServletException {

System.out.println("开始运行doFilter--");

//重定向

HttpServletResponse response=(HttpServletResponse) arg1;

response.sendRedirect("index.jsp"); 

// HttpServletRequest request = (HttpServletRequest) arg0;  

//    request.getRequestDispatcher("index.jsp").forward(arg0, arg1);  

arg2.doFilter(arg0, arg1);

System.out.println("结束运行doFilter--");

}

   // step:2

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

System.out.println("Filter初始化方法:init()");

}

}

配置文件:

<filter>

      <filter-name>Filter</filter-name>

       <filter-class>FirstFilter.firstFilter</filter-class>

      

         </filter>

  

  <filter-mapping>

   <filter-name>Filter</filter-name>

   <url-pattern>/index.jsp</url-pattern>

    <dispatcher>REQUEST</dispatcher>  

  </filter-mapping>

</web-app>

错误提示:

type Exception report

message javax.servlet.ServletException: java.lang.IllegalStateException: Cannot create a session after the response has been committed

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.IllegalStateException: Cannot create a session after the response has been committed
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
FirstFilter.firstFilter.doFilter(firstFilter.java:35)


root cause

javax.servlet.ServletException: java.lang.IllegalStateException: Cannot create a session after the response has been committed
org.apache.jsp.index_jsp._jspService(index_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
FirstFilter.firstFilter.doFilter(firstFilter.java:35)


root cause

java.lang.IllegalStateException: Cannot create a session after the response has been committed
org.apache.catalina.connector.Request.doGetSession(Request.java:2886)
org.apache.catalina.connector.Request.getSession(Request.java:2316)
org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:898)
org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:910)
org.apache.jasper.runtime.PageContextImpl._initialize(PageContextImpl.java:146)
org.apache.jasper.runtime.PageContextImpl.initialize(PageContextImpl.java:125)
org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:112)
org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl.java:65)
org.apache.jsp.index_jsp._jspService(index_jsp.java:54)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
FirstFilter.firstFilter.doFilter(firstFilter.java:35)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.


正在回答

1 回答

不知道,你解决了么。我解释下,自己理解的

Cannot create a session after the response has been committed

这个错误解释的很清楚,就是response被二次提交了,

罪魁祸首就是

FirstFilter.firstFilter.doFilter(firstFilter.java:35)

也就是这句:arg2.doFilter(arg0, arg1);

这句的意思是放行,给浏览器发送消息(response)

你把这句注释了,记得改下这句,

response.sendRedirect("index.jsp"); -----》》response.sendRedirect("main.jsp"); 

你的这是个无限循环的

0 回复 有任何疑惑可以回复我~
#1

丶子非鱼 提问者

我知道是无限循环的 我照着博客来的,别人的是无限循环的 但是我的就有错 尴尬
2017-05-08 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

执行错误?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信