Bom dia!
Abaixo minha solução para a dinâmica de só acessar as páginas protegidas quem já fez login: Esse código atenderia bem o objetivo?
` package br.com.alura.gerenciador.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import br.com.alura.gerenciador.action.Acao;
@WebServlet("/entrada") public class UnicaEntradaServlet extends HttpServlet { private static final long serialVersionUID = 1L;
@SuppressWarnings({ "deprecation", "rawtypes" })
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String paramAcao = request.getParameter("acao");
String nomeDaClasse = "br.com.alura.gerenciador.action." + paramAcao;
String nome;
try {
Class classe = Class.forName(nomeDaClasse);
Acao acao = (Acao) classe.newInstance();
nome = acao.executa(request, response);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | ServletException
| IOException e) {
throw new ServletException(e);
}
HttpSession sessao = request.getSession();
if (sessao.getAttribute("usuarioLogado") != null) {
String[] tipoEEndereco = nome.split(":");
if (tipoEEndereco[0].equals("forward")) {
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/view/" + tipoEEndereco[1]);
rd.forward(request, response);
} else {
response.sendRedirect(tipoEEndereco[1]);
}
} else {
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/view/formLogin.jsp");
rd.forward(request, response);
}
}
} `