代码:
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.
不知道,你解决了么。我解释下,自己理解的
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");
你的这是个无限循环的