Skip to content

Commit

Permalink
Adicionada a restrição "UNIQUE" no arquivo "agendadb.sql" em "Usuario…
Browse files Browse the repository at this point in the history
… => nome", por isso o método "usuarioExiste(String nome)" da classe UsuarioDAO foi removido e no Catch (SQLException) do método cadastrar adicionada uma condicional, no qual se o código do erro gerado pelo mysql for o código impedindo a adição de nomes iguais, lançar a exceção;

Adicionadas as interfaces UsuarioDAO e ContatoDAO, as classes antes UsuarioDAO e ContatoDAO foram renomoeadas para UsuarioDAOImpl e ContatoDAOImpl;
Contato e Usuario agora implementam Serializable.
  • Loading branch information
iagocolodetti committed Apr 6, 2019
1 parent 3b208a5 commit fa59518
Show file tree
Hide file tree
Showing 28 changed files with 476 additions and 359 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Também é possível efetuar o logout e um filtro é usado para verificar a cada
Aplicação criada com o padrão MVC, utilizando classe Java como modelo, página JSP como view (com Bootstrap 3 e JSTL) e Servlet como controller.

* Downloads: https://github.com/iagocolodetti/Agenda/releases
* [Arquivo de Implantação](https://github.com/iagocolodetti/Agenda/releases/download/v2.2/Agenda.war "Agenda.war")
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/Agenda/releases/download/v2.2/agendadb.sql "agendadb.sql")
* [Bibliotecas Necessárias](https://github.com/iagocolodetti/Agenda/releases/download/v2.2/bibliotecas.zip "bibliotecas.zip")
* [Código-Fonte](https://github.com/iagocolodetti/Agenda/archive/v2.2.zip "v2.2.zip")
* [Arquivo de Implantação](https://github.com/iagocolodetti/Agenda/releases/download/v2.3/Agenda.war "Agenda.war")
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/Agenda/releases/download/v2.3/agendadb.sql "agendadb.sql")
* [Bibliotecas Necessárias](https://github.com/iagocolodetti/Agenda/releases/download/v2.3/bibliotecas.zip "bibliotecas.zip")
* [Código-Fonte](https://github.com/iagocolodetti/Agenda/archive/v2.3.zip "v2.3.zip")
20 changes: 10 additions & 10 deletions src/java/br/com/iagocolodetti/controle/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;

/**
*
* @author iagocolodetti
*/
public class ConnectionFactory {

private static final String DRIVER = "com.mysql.jdbc.Driver";
Expand All @@ -14,25 +18,23 @@ public class ConnectionFactory {
private static final String PASS = "root";

public static Connection getConnection() {

try {
Class.forName(DRIVER);
return DriverManager.getConnection(URL, USER, PASS);
}
catch (ClassNotFoundException | SQLException e) {
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return null;
}

public static void closeConnection(Connection con) {

try {
if (con != null) {
con.close();
}
}
catch (SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
Expand All @@ -44,8 +46,7 @@ public static void closeConnection(Connection con, PreparedStatement ps) {
if (ps != null) {
ps.close();
}
}
catch (SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
Expand All @@ -57,8 +58,7 @@ public static void closeConnection(Connection con, PreparedStatement ps, ResultS
if (rs != null) {
rs.close();
}
}
catch (SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/java/br/com/iagocolodetti/controle/FiltroAutenticacao.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
*
* @author iagocolodetti
*/
public class FiltroAutenticacao implements Filter {

@Override
public void init(FilterConfig filterConfig) {

}

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

HttpServletRequest rq = (HttpServletRequest) request;
if (rq.getSession().isNew() || rq.getSession().getAttribute("usuario") == null) {
rq.setAttribute("erro", "Sessão expirada, faça o login novamente.");
rq.getSession().invalidate();
Expand All @@ -30,9 +34,9 @@ public void doFilter(ServletRequest request, ServletResponse response,

chain.doFilter(request, response);
}

@Override
public void destroy() {
public void destroy() {

}
}
21 changes: 14 additions & 7 deletions src/java/br/com/iagocolodetti/controle/ServletAgenda.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.com.iagocolodetti.controle;

import br.com.iagocolodetti.modelo.Contato;
import br.com.iagocolodetti.modelo.ContatoDAO;
import br.com.iagocolodetti.modelo.ContatoDAOImpl;
import br.com.iagocolodetti.modelo.Usuario;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -10,20 +10,27 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author iagocolodetti
*/
public class ServletAgenda extends HttpServlet {

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Usuario usuario = (Usuario) request.getSession().getAttribute("usuario");

if (!request.getSession().isNew() && usuario != null) {
ArrayList<Contato> contatos = new ContatoDAO().carregar(usuario);
if (!contatos.isEmpty()) request.setAttribute("contatos", contatos);
else request.setAttribute("agendavazia", "A agenda ainda não possui contatos.");
ArrayList<Contato> contatos = new ContatoDAOImpl().carregar(usuario);
if (!contatos.isEmpty()) {
request.setAttribute("contatos", contatos);
} else {
request.setAttribute("agendavazia", "A agenda ainda não possui contatos.");
}
}

request.getRequestDispatcher("agenda.jsp").forward(request, response);
}
}
29 changes: 16 additions & 13 deletions src/java/br/com/iagocolodetti/controle/ServletAlterar.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.com.iagocolodetti.controle;

import br.com.iagocolodetti.modelo.Contato;
import br.com.iagocolodetti.modelo.ContatoDAO;
import br.com.iagocolodetti.modelo.ContatoDAOImpl;
import br.com.iagocolodetti.modelo.ContatoExisteException;
import br.com.iagocolodetti.modelo.ContatoNaoExisteException;
import br.com.iagocolodetti.modelo.Usuario;
Expand All @@ -12,6 +12,10 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author iagocolodetti
*/
public class ServletAlterar extends HttpServlet {

private void erro(HttpServletRequest request, String erro) {
Expand All @@ -23,46 +27,45 @@ private void erro(HttpServletRequest request, String erro) {
request.setAttribute("novoemail", Util.decodificar(request.getParameter("novoemail")));
request.setAttribute("novotelefone", Util.decodificar(request.getParameter("novotelefone")));
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setAttribute("id", Util.decodificar(request.getParameter("id")));
request.setAttribute("nomeatual", Util.decodificar(request.getParameter("nome")));
request.setAttribute("emailatual", Util.decodificar(request.getParameter("email")));
request.setAttribute("telefoneatual", Util.decodificar(request.getParameter("telefone")));

request.getRequestDispatcher("alterar.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

RequestDispatcher rd = null;

Usuario usuario = (Usuario) request.getSession().getAttribute("usuario");

if (!request.getSession().isNew() && usuario != null) {
try {
Contato contato = new Contato(
Integer.parseInt(request.getParameter("id")),
Util.decodificar(request.getParameter("novonome")),
Util.decodificar(request.getParameter("novoemail")),
Util.decodificar(request.getParameter("novotelefone")));
new ContatoDAO().alterar(usuario.getId(), contato);

new ContatoDAOImpl().alterar(usuario.getId(), contato);

request.setAttribute("sucesso", "Contato alterado com sucesso.");
rd = request.getRequestDispatcher("agenda");
}
catch (IndexOutOfBoundsException | IllegalArgumentException | ContatoExisteException | ContatoNaoExisteException e) {
} catch (IndexOutOfBoundsException | IllegalArgumentException | ContatoExisteException | ContatoNaoExisteException e) {
erro(request, e.getMessage());
rd = request.getRequestDispatcher("alterar.jsp");
}
}

rd.forward(request, response);
}
}
25 changes: 13 additions & 12 deletions src/java/br/com/iagocolodetti/controle/ServletCadastrar.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
package br.com.iagocolodetti.controle;

import br.com.iagocolodetti.modelo.Usuario;
import br.com.iagocolodetti.modelo.UsuarioDAO;
import br.com.iagocolodetti.modelo.UsuarioDAOImpl;
import br.com.iagocolodetti.modelo.UsuarioExisteException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author iagocolodetti
*/
public class ServletCadastrar extends HttpServlet {

private void erro(HttpServletRequest request, String erro) {
request.setAttribute("erro", "Erro: " + erro);
request.setAttribute("nome", Util.decodificar(request.getParameter("nome")));
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

if (!request.getSession().isNew() && request.getSession().getAttribute("usuario") != null) {
response.sendRedirect("agenda");
}
else {
} else {
response.sendRedirect("cadastrar.jsp");
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

if (!request.getSession().isNew() && request.getSession().getAttribute("usuario") != null) {
response.sendRedirect("agenda");
}
else {
} else {
try {
Usuario usuario = new Usuario(Util.decodificar(request.getParameter("nome")), Util.decodificar(request.getParameter("senha")));
new UsuarioDAO().cadastrar(usuario);
new UsuarioDAOImpl().cadastrar(usuario);
request.setAttribute("sucesso", "Usuário \"" + usuario.getNome() + "\" cadastrado com sucesso.");
}
catch (IllegalArgumentException | IndexOutOfBoundsException | UsuarioExisteException e) {
} catch (IllegalArgumentException | IndexOutOfBoundsException | UsuarioExisteException e) {
erro(request, e.getMessage());
}
request.getRequestDispatcher("cadastrar.jsp").forward(request, response);
Expand Down
9 changes: 6 additions & 3 deletions src/java/br/com/iagocolodetti/controle/ServletIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author iagocolodetti
*/
public class ServletIndex extends HttpServlet {

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

if (!request.getSession().isNew() && request.getSession().getAttribute("usuario") != null) {
response.sendRedirect("agenda");
}
else {
} else {
response.sendRedirect("index.jsp");
}
}
Expand Down
21 changes: 12 additions & 9 deletions src/java/br/com/iagocolodetti/controle/ServletLogin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.com.iagocolodetti.controle;

import br.com.iagocolodetti.modelo.Usuario;
import br.com.iagocolodetti.modelo.UsuarioDAO;
import br.com.iagocolodetti.modelo.UsuarioDAOImpl;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
Expand All @@ -10,31 +10,34 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author iagocolodetti
*/
public class ServletLogin extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

RequestDispatcher rd;
Usuario usuario = new UsuarioDAO().buscar(new Usuario(request.getParameter("nome"), request.getParameter("senha")));

Usuario usuario = new UsuarioDAOImpl().buscar(new Usuario(request.getParameter("nome"), request.getParameter("senha")));

if (usuario.getId() != -1) {
HttpSession objSessao = request.getSession();
if (objSessao == null) {
objSessao = request.getSession(true);
}
objSessao.setAttribute("usuario", usuario);

rd = request.getRequestDispatcher("agenda");
}
else {
} else {
request.setAttribute("nome", Util.decodificar(usuario.getNome()));
request.setAttribute("erro", "Erro: Usuário e/ou senha inválido(s)");
rd = request.getRequestDispatcher("index.jsp");
}

rd.forward(request, response);
}
}
6 changes: 5 additions & 1 deletion src/java/br/com/iagocolodetti/controle/ServletLogout.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author iagocolodetti
*/
public class ServletLogout extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.getSession().invalidate();
response.sendRedirect("index.jsp");
}
Expand Down
Loading

0 comments on commit fa59518

Please sign in to comment.