Não estou conseguindo redirecionar da pagina de login para a pagina de cadastro.
@WebServlet(urlPatterns = "/entrada")
public class EntradaServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String paramAcao = req.getParameter("acao");
HttpSession session = req.getSession();
Boolean usuarioNaoLogado = (session.getAttribute("usuarioLogado") == null);
Boolean acaoProtegida = !(paramAcao.equals("Login") || paramAcao.equals("LoginForm") );
if (acaoProtegida && usuarioNaoLogado) {
resp.sendRedirect("entrada?acao=LoginForm");
return;
}
String nomeDaClasse = "br.com.rh.model.acoes." + paramAcao;
String nome;
try {
Class<?> classe = Class.forName(nomeDaClasse);
@SuppressWarnings("deprecation")
Acao acao = (Acao) classe.newInstance();
nome = acao.executa(req, resp);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new ServletException(e);
}
String[] tipoEndereco = nome.split(":");
if(tipoEndereco[0].equals("forward")) {
RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/view/" + tipoEndereco[1]);
dispatcher.forward(req, resp);
} else {
resp.sendRedirect(tipoEndereco[1]);
}
}
}
public class LoginForm implements Acao{
@Override
public String executa(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
return "forward:login-form.jsp";
}
}
public class CadastroForm implements Acao {
@Override
public String executa(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
return "forward:cadastro-form.jsp";
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:url value="/entrada" var="linkServlet" />
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<body>
<div class="container">
<h2>Login</h2>
<form action="${linkServlet}" method="post">
<label for="username">Usuário</label> <input type="text"
id="username" name="username" required> <label
for="password">Senha</label> <input type="password" id="password"
name="password" required> <input type="hidden" value="Login"
name="acao">
<button type="submit">Entrar</button>
</form>
<div class="create-account">
<p>
Não tem uma conta? <a href="entrada?acao=CadastroForm">Crie uma
conta</a>
</p>
</div>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:url value="/entrada?acao=CadastrarEmpresa" var="linkServlet" />
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div class="container">
<h2>Cadastro</h2>
<form action="${linkServlet}" method="post">
<label for="nome">Nome</label>
<input type="text" id="nome" name="nome" required>
<label for="dataDeNascimento">Data de Nascimento</label>
<input type="date" id="dataDeNascimento" name="dataDeNascimento" required>
<label for="cpf">CPF</label>
<input type="text" id="cpf" name="cpf" required>
<label for="login">Login</label>
<input type="text" id="login" name="login" required>
<label for="senha">Senha</label>
<input type="password" id="senha" name="senha" required>
<label for="rua">Rua</label>
<input type="text" id="rua" name="rua" required>
<label for="numero">Número</label>
<input type="number" id="numero" name="numero" required>
<label for="bairro">Bairro</label>
<input type="text" id="bairro" name="bairro" required>
<label for="cidade">Cidade</label>
<input type="text" id="cidade" name="cidade" required>
<label for="estado">Estado</label>
<input type="text" id="estado" name="estado" required>
<input type="hidden" value="CadastrarFuncionario" name="acao">
<button type="submit">Cadastrar</button>
</form>
</div>
</body>
</html>