If you do not use the Java EE support provided by the container, then it is common practice to keep the registered User login in the session area and use Filter on the desired url-pattern to check if User registered.
Here is a basic example to get an image:
Login:
User user = userDAO.find(username, password); if (user != null) { session.setAttribute("user", user); } else {
Filter (which is displayed on the url-sample, for example, /secured/* , /protected/* , etc., where you put pages with restricted JSPs on the login page):
User user = session.getAttribute("user"); if (user != null) { chain.doFilter(request, response); // Logged in, so continue with request. } else { response.sendRedirect("login"); // Not logged in, redirect to login page. }
Exit:
session.removeAttribute("user");
Of course, you can also take advantage of what Java EE out of the box provides with regard to security. A commonly used method is container-based declarative management, in which you can specify users and roles. You just need to declare <security-constraint> and <login-config> in web.xml and configure the user area on the application server. The details depend on the application server used, but if it is, for example, Tomcat 6.0, you can find some documentation about it here.
source share