简体   繁体   中英

doFilter method on java - null pointer exception on filterchain when session is invalidate

this is my java filter (doFilter method):

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() {
}
}

I use this filter because i need to controll if the session is valid or not; this is useful in case of , after logout, if the back button of browser is pressed, automaticly it redirect the client on Login.jsp. The problem is on try catch module : infact without it, when i try to press back button the JVM give me the follow error:

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

i think that this happen because the filterchain is not present, when there are a not valid session, but how can manage this problem? i think that the "try catch" module is not a good solution. thanks

<?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>

this is logout 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM