title: spring mvc login/logout using session
body: <p>In this login example i am trying to add session to the user.The basic login feature works fine.When i go back to the home page i cannot access the page that is in the log success page.I need to login again.I am new to spring, please tell me how i should get a session.</p>
  
  <p><strong>Logincontoller.java</strong></p>
  
  <pre><code>@Controller
  public class LoginController {
  
      @Autowired
      private LoginService loginService;
  
      @RequestMapping("login.html")
      public String toLogin(Model model) {
          Login login = new Login(); 
          model.addAttribute("login",login);
          return "login";
      }
  
      @RequestMapping(value ="login.html", method = RequestMethod.POST)
      public ModelAndView  doLogin(@Valid @ModelAttribute ("login") Login login, 
              BindingResult bindingresult,HttpSession session ) {
           ModelAndView view = new ModelAndView("login");
          if(!bindingresult.hasErrors()){
              if(!loginService.authenticateUser(login)){
                  bindingresult.addError(new ObjectError("invalid", "Invalid Credentials!!!"));
                  return new ModelAndView("error");
              }
              else{
                  session.setAttribute("login", login);
                  view.setViewName("success");
              }
          }
  
           return view;
  }
  
      @RequestMapping("/logout")
        public String logout(HttpSession session ) {
           session.invalidate();
           return "redirect:/login.html";
        }
  
  }
  </code></pre>