java上的doFilter方法 - 当session无效时,filterchain上的空指针异常

时间:2014-04-13 17:37:02

标签: java servlet-filters

这是我的java过滤器(doFilter方法):

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    String uri = req.getRequestURI();
    // context.log("Requested Resource::"+uri);

    if ((!req.isRequestedSessionIdValid()
            && (uri.endsWith("Login.jsp") || uri.endsWith("LoginServlet")) || 
            req.isRequestedSessionIdValid())) {


        // pass the request along the filter chain
         try {
        filterChain.doFilter(req, res);

         }catch (Exception e) {
            // TODO: handle exception
             e.printStackTrace();
            res.sendRedirect("Logout");
        }

    } else {

        res.sendRedirect("Logout");

        // context.log("Unauthorized access request");
    }

}

public void destroy() {
}
}

我使用此过滤器,因为我需要控制会话是否有效; 如果在注销后,如果按下浏览器的后退按钮,则会自动将其重定向到Login.jsp上的客户端。 问题出在try catch模块:没有它的情况下,当我尝试按下后退按钮时,JVM会给我以下错误:

org.apache.jasper.JasperException: java.lang.NullPointerException
at  org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.connection.filter.ServletFilter.doFilter(ServletFilter.java:41)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at org.apache.jsp.Home_jsp._jspService(Home_jsp.java:155)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
... 22 more

我认为发生这种情况是因为当存在无效会话时,过滤链不存在,但如何管理此问题?我认为"尝试赶上"模块不是一个好的解决方案。 感谢

1 个答案:

答案 0 :(得分:3)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>LoginServlet</display-name>
 <servlet>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.connection.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<display-name>Logout</display-name>
<servlet>
<display-name>Logout</display-name>
<servlet-name>Logout</servlet-name>
<servlet-class>com.connection.servlet.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Logout</servlet-name>
<url-pattern>/Logout</url-pattern>
</servlet-mapping>

<filter>
<filter-name>ServletFilter</filter-name>
<filter-class>
    com.connection.filter.ServletFilter
</filter-class>
<init-param>
    <param-name>avoid-urls</param-name>
    <param-value>Login.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ServletFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

这是注销servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setHeader("Cache-Control", "no-cache, no-store");
    response.setHeader("Pragma", "no-cache");
    request.getSession().invalidate();
    response.sendRedirect("Login.jsp");
}