package br.com.alura.gerenciador.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
* Servlet implementation class NovaEmpresa
*/
@WebServlet("/novaEmpresa")
public class NovaEmpresa extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Cadastrando nova empresa");
PrintWriter out = response.getWriter();
String nome = request.getParameter("nome");
String email = request.getParameter("mail");
System.out.println(email);
Empresa empresa = new Empresa();
empresa.setNome(nome);
BancoDeDados banco = new BancoDeDados();
banco.adiciona(empresa);
out.println("<html><body> Nova Empresa: " + email+" </body></html>");
RequestDispatcher rq = request.getRequestDispatcher("/novaEmpresaCriada.jsp");
request.setAttribute("nome", empresa.getNome());
rq.forward(request, response);
System.out.println(banco.getEmpresas());
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:if test="${not empty empresa.nome}">
Empresa ${ empresa } cadastrada com sucesso!
</c:if>
<c:if test="${empty empresa}">
Nenhuma empresa criada!
</c:if>
</body>
</html>