1

I am newbie in SpringMVC, trying to make a logout page in an employee details application. An employee can login into his/her details and can change it. But i am confused how to set the logout page.
Please tell me the possible alternatives through which a logout page can make without using spring-security.

thanks

dhS
  • 3,739
  • 5
  • 26
  • 55

1 Answers1

5

You can simply provide a link with url of logout controller method. Suppose you have controller named MiscController with method logout for handling logout, Then code would look like..

@Controller
public class MiscController {

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "index";
    }
}

Now in your jsp page you can have a link like..

<a href="${pageContext.servletContext.contextPath}/logout">Logout</a>

On Clicking the logout, You will be logged out and redirected to index.jsp page[Assuming ViewResolver is configured correctly].

Md Zahid Raza
  • 941
  • 1
  • 11
  • 28
  • do we need to set the session in the login page ... from where we are calling this request.getSession().invalidate() in logout method – dhS Aug 30 '16 at 18:28
  • 1
    When you login, you set user details is Session Object. So, when you logout, you need to invalidate all data saved in session object. – Md Zahid Raza Aug 30 '16 at 18:45
  • Each and every request [HttpServletRequest] object has HttpSession object associated with it where you save data related to a particular session. For details, You should read attribute scopes topic in java Servlet . – Md Zahid Raza Aug 30 '16 at 18:47
  • 3
    Also, it's better to use `POST` method for logout handler http://stackoverflow.com/questions/3521290/logout-get-or-post – Ali Dehghani Aug 30 '16 at 18:54