title: Spring MVC redirect user to login page once session is expired
body: <p>I have stored a user bean in session as <code>@SessionAttributes({"UserBean"})</code> in my controller.</p>
  
  <p>My aim is to redirect user to login/error page if session is expired. Following is my code snippet</p>
  
  <pre><code>@RequestMapping(value = "searchOpportunity.htm", method = RequestMethod.GET)
  public ModelAndView searchOpportunity(@ModelAttribute("UserBean") UserBean userBean) {
      functionName = "searchOpportunity";
      logger.info("HERE !!! In " + className + " - " + functionName + " ");
      System.out.println("HERE !!! In " + className + " - " + functionName + " ");
      try {
          if (userBean == null) {
              System.out.println(userBean);
              return new ModelAndView("userLogout", "command", null);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      return new ModelAndView("opportunity/opportunitySearch", "command", new SearchOpportunityBean());
  }
  </code></pre>
  
  <p>However when the session is expired i get the following error.</p>
  
  <pre><code>org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet mintDispatcher threw exception
  org.springframework.web.HttpSessionRequiredException: Session attribute 'UserBean' required - not found in session
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:761)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:758)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>Seems like the controller code is never executed if userbean is expired. What is the reason ?
  What can be a solution ?</p>