Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Código final #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gerenciador/.classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 7 [1.7.0_09]">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.8.0_71">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
Expand Down
2 changes: 1 addition & 1 deletion gerenciador/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
eclipse.preferences.version=1
encoding//WebContent/index.html=UTF-8
encoding//WebContent/index.jsp=UTF-8
encoding/<project>=UTF-8
12 changes: 12 additions & 0 deletions gerenciador/WebContent/WEB-INF/paginas/buscaEmpresa.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
Resultado da busca:
<br />
<ul>
<c:forEach var="empresa" items="${empresas}">
<li>${empresa.id}: ${empresa.nome}</li>
</c:forEach>
</ul>
</body>
</html>
1 change: 1 addition & 0 deletions gerenciador/WebContent/WEB-INF/paginas/logout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>Logout efetuado</body></html>
1 change: 1 addition & 0 deletions gerenciador/WebContent/WEB-INF/paginas/novaEmpresa.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>Empresa ${nome} adicionada com sucesso!</body></html>
5 changes: 0 additions & 5 deletions gerenciador/WebContent/index.html

This file was deleted.

25 changes: 25 additions & 0 deletions gerenciador/WebContent/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
Bem vindo ao nosso gerenciador de empresas!
<br />
<c:if test="${not empty usuarioLogado}">
Voce esta logado como ${usuarioLogado.email}<br />
</c:if>
<br />
<form action="executar?tarefa=NovaEmpresa" method="post">
Nome: <input type="text" name="nome" /><br /> <input type="submit"
value="Enviar" />
</form>
<br />
<form action="login" method="post">
Email: <input type="text" name="email" /><br /> <br /> Senha: <input
type="password" name="senha" /><br /> <br /> <input type="submit"
value="Login" />
</form>
<br />
<form action="executar?tarefa=Logout" method="post">
<input type="submit" value="Logout" />
</form>
</body>
</html>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
32 changes: 32 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/BuscaEmpresa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package br.com.alura.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

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 br.com.alura.gerenciador.Empresa;
import br.com.alura.gerenciador.dao.EmpresaDAO;

public class BuscaEmpresa implements Tarefa{

public BuscaEmpresa() {
System.out.println("Instanciando uma Servlet do tipo BuscaEmpresa " + this);
}

@Override
public String executa(HttpServletRequest req, HttpServletResponse resp) {

String filtro = req.getParameter("filtro");
Collection<Empresa> empresas = new EmpresaDAO().buscaPorSimilaridade(filtro);
req.setAttribute("empresas", empresas);
return "/WEB-INF/paginas/buscaEmpresa.jsp";

}

}
34 changes: 34 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package br.com.alura.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/executar")
public class Controller extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String tarefa = req.getParameter("tarefa");

if (tarefa == null) {
throw new IllegalArgumentException("Você esqueceu de passar a tarefa");
}

try {
String nomeDaClasse = "br.com.alura.web." + tarefa;
Class<?> type = Class.forName(nomeDaClasse);
Tarefa instancia = (Tarefa) type.newInstance();
String pagina = instancia.executa(req, resp);
req.getRequestDispatcher(pagina).forward(req, resp);
} catch (Exception e) {
throw new ServletException(e);
}

}
}
26 changes: 26 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/Cookies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package br.com.alura.web;

import javax.servlet.http.Cookie;

public class Cookies {

Cookie[] cookies = null;

public Cookies (Cookie[] cookies){
this.cookies = cookies;
}

public Cookie getUsuarioLogado() {

if (cookies == null)
return null;

for (Cookie cookie : cookies) {
if (cookie.getName().equals("usuario.logado")) {
return cookie;
}
}

return null;
}
}
50 changes: 50 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/FiltroDeAuditoria.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package br.com.alura.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.alura.gerenciador.Usuario;

@WebFilter(urlPatterns = "/*")
public class FiltroDeAuditoria implements Filter {

@Override
public void destroy() {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

String nome = "<deslogado>";
HttpServletRequest req = (HttpServletRequest) request;

HttpSession session = req.getSession();
Usuario usuarioLogado = (Usuario) session.getAttribute("usuarioLogado");

if(usuarioLogado != null) {
nome = usuarioLogado.getEmail();
}

System.out.println("Usuario " + nome + " acessando a URI " + req.getRequestURI());
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
}

// user: [email protected]
// password: silveira
}
35 changes: 35 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/Login.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package br.com.alura.web;

import java.io.IOException;
import java.io.PrintWriter;

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.Usuario;
import br.com.alura.gerenciador.dao.UsuarioDAO;

@WebServlet(urlPatterns = "/login")
public class Login extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

HttpSession session = req.getSession();
PrintWriter writer = resp.getWriter();
String email = req.getParameter("email");
String senha = req.getParameter("senha");
Usuario usuario = new UsuarioDAO().buscaPorEmailESenha(email, senha);

if(usuario == null) {
writer.write("<html><body>Usuário ou senha inválida.</body></html>");
} else {
session.setAttribute("usuarioLogado", usuario);
writer.write("<html><body>Usuário " + email + " logado.</body></html>");
}
}
}
18 changes: 18 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/Logout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.alura.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Logout implements Tarefa {

@Override
public String executa(HttpServletRequest req, HttpServletResponse resp) {

HttpSession session = req.getSession();
session.removeAttribute("usuarioLogado");

return "/WEB-INF/paginas/logout.html";
}

}
24 changes: 24 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/NovaEmpresa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package br.com.alura.web;

import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.alura.gerenciador.Empresa;
import br.com.alura.gerenciador.dao.EmpresaDAO;

public class NovaEmpresa implements Tarefa {

@Override
public String executa(HttpServletRequest req, HttpServletResponse resp) {

String nome = req.getParameter("nome");
Empresa empresa = new Empresa(nome);
new EmpresaDAO().adiciona(empresa);

return "/WEB-INF/paginas/novaEmpresa.jsp";
}

}
8 changes: 8 additions & 0 deletions gerenciador/src/main/java/br/com/alura/web/Tarefa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package br.com.alura.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Tarefa {
String executa(HttpServletRequest request, HttpServletResponse response);
}