Session Filtering Code
@WebFilter(urlPatterns = { "/*" })
public class FiltroSessaoUsuario implements Filter {
public void init(FilterConfig fConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
if (httpRequest.getAttribute("usuario") != null) {
chain.doFilter(request, response);
} else {
request.getRequestDispatcher("erro-
login.jsp").forward(httpRequest, httpResponse);
}
} catch (Exception e) {
throw new RuntimeException("Ocorreu um erro no filtro de sessao do
usuario.", e);
}
}
public void destroy() {
}
}
Login logic code
public class LoginUsuarioLogica implements Logica {
public void executa(HttpServletRequest request, HttpServletResponse
response) throws Exception {
Usuario usuario = new Usuario();
usuario.setEmail(request.getParameter("email"));
usuario.setSenha(request.getParameter("senha"));
HttpSession sessao = request.getSession();
if (new UsuarioDAO().validaLogin(usuario)) {
sessao.setAttribute("usuario", usuario.getEmail());
request.getRequestDispatcher("index.jsp").forward(request,
response);
} else {
sessao.invalidate();
request.getRequestDispatcher("erro-login.jsp").forward(request,
response);
}
}
}
Logout logic code
public class LogoutUsuarioLogica implements Logica {
public void executa(HttpServletRequest request, HttpServletResponse
response) throws Exception {
if (request.getParameter("parametro").equalsIgnoreCase("logout")) {
HttpSession sessao = request.getSession();
sessao.invalidate();
response.sendRedirect("login.jsp");
}
}
}