Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.sys.servlets;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import br.sys.beans.Usuario;
- import br.sys.jdbc.UsuarioDAO;
- public class Editar extends HttpServlet {
- private static final long serialVersionUID = 1L;
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- Usuario usuario = UsuarioDAO.getUsuario(req.getParameter("username"));
- HttpSession session = req.getSession();
- session.setAttribute("username",usuario.getUsername());
- session.setAttribute("password",usuario.getPassword());
- session.setAttribute("nome",usuario.getNome());
- session.setAttribute("email",usuario.getEmail());
- resp.sendRedirect("editar.jsp");
- }
- }
- --------
- package br.sys.servlets;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import br.sys.jdbc.UsuarioDAO;
- public class Excluir extends HttpServlet{
- private static final long serialVersionUID = 1L;
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String[] usernames = req.getParameterValues("excluir");
- if (usernames!=null) {
- for (String username : usernames) {
- UsuarioDAO.delete(username);
- }
- }
- resp.sendRedirect("processagerenciar");
- }
- }
- --------
- package br.sys.servlets;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import br.sys.beans.Usuario;
- import br.sys.jdbc.UsuarioDAO;
- public class ProcessaCadastro extends HttpServlet{
- private static final long serialVersionUID = 1L;
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- ArrayList<String> erros = Validator.validaCadastro(req);
- if (erros.isEmpty()) {
- Usuario usuario = new Usuario();
- usuario.setUsername(req.getParameter("username"));
- usuario.setPassword(req.getParameter("password"));
- usuario.setNome(req.getParameter("nome"));
- usuario.setEmail(req.getParameter("email"));
- UsuarioDAO.insert(usuario);
- resp.sendRedirect("processagerenciar");
- }
- else{
- req.setAttribute("errosCadastro",erros);
- req.getRequestDispatcher("cadastro.jsp").forward(req, resp);
- }
- }
- }
- --------
- package br.sys.servlets;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import br.sys.jdbc.UsuarioDAO;
- public class ProcessaGerenciar extends HttpServlet {
- private static final long serialVersionUID = 1L;
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- HttpSession session = req.getSession();
- session.setAttribute("usuarios",UsuarioDAO.findAll());
- resp.sendRedirect("gerenciar.jsp");
- }
- }
- --------
- package br.sys.servlets;
- import java.util.ArrayList;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import br.sys.jdbc.UsuarioDAO;
- public class Validator extends HttpServlet{
- private static final long serialVersionUID = 1L;
- /*
- private static boolean getInteger(String sInt) {
- try {
- if (Integer.parseInt(sInt)<0) {
- return true;
- }
- } catch (NumberFormatException e) {
- e.getStackTrace();
- return true;
- } catch (NullPointerException e) {
- e.getStackTrace();
- return true;
- }
- return false;
- }
- */
- private static boolean getString(String str) {
- if(str.trim().isEmpty())
- return true;
- return false;
- }
- public static ArrayList<String> validaIndex(HttpServletRequest req) {
- ArrayList<String> erros = new ArrayList<String>();
- if(getString(req.getParameter("username"))&& getString(req.getParameter("password"))){
- erros.add("Digite um username valido.");
- erros.add("Digite um password valido.");
- }
- else{
- if(UsuarioDAO.getUsuario(req.getParameter("username"))==null)
- erros.add("Nao existe o usuario informado.");
- else{
- if(!UsuarioDAO.isUsuarioAuth(req.getParameter("username"),req.getParameter("password")))
- erros.add("O usuario e senha nao conferem.");
- }
- }
- return erros;
- }
- public static ArrayList<String> validaCadastro(HttpServletRequest req) {
- ArrayList<String> erros = new ArrayList<String>();
- if(getString(req.getParameter("username")))
- erros.add("Digite um username valido.");
- if(UsuarioDAO.getUsuario(req.getParameter("username"))!=null)
- erros.add("O username ja existe.");
- if(getString(req.getParameter("password")))
- erros.add("Digite um password valido.");
- if(getString(req.getParameter("nome")))
- erros.add("Digite um nome valido.");
- if((getString(req.getParameter("email"))) || !(req.getParameter("email").contains("@")))
- erros.add("Digite um email valido ex. pat@example.com .");
- return erros;
- }
- public static ArrayList<String> validaEditar(HttpServletRequest req) {
- ArrayList<String> erros = new ArrayList<String>();
- if(getString(req.getParameter("password")))
- erros.add("Digite um password valido.");
- if(getString(req.getParameter("nome")))
- erros.add("Digite um nome valido.");
- if((getString(req.getParameter("email"))) || !(req.getParameter("email").contains("@")))
- erros.add("Digite um email valido ex. pat@example.com .");
- return erros;
- }
- }
- --------
- package br.sys.servlets;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- public class ProcessaIndex extends HttpServlet{
- private static final long serialVersionUID = 1L;
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- ArrayList<String> erros = Validator.validaIndex(req);
- if (erros.isEmpty()) {
- HttpSession session = req.getSession();
- session.setAttribute("login",req.getParameter("username"));
- resp.sendRedirect("processagerenciar");
- }
- else{
- req.setAttribute("errosIndex",erros);
- req.getRequestDispatcher("index.jsp").forward(req,resp);
- }
- }
- }
- --------
- package br.sys.servlets;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import br.sys.beans.Usuario;
- import br.sys.jdbc.UsuarioDAO;
- public class ProcessaEditar extends HttpServlet{
- private static final long serialVersionUID = 1L;
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- ArrayList<String> erros = Validator.validaEditar(req);
- HttpSession session = req.getSession();
- if (erros.isEmpty()) {
- Usuario usuario = new Usuario();
- usuario.setUsername((String)session.getAttribute("username"));
- usuario.setPassword(req.getParameter("password"));
- usuario.setNome(req.getParameter("nome"));
- usuario.setEmail(req.getParameter("email"));
- UsuarioDAO.update(usuario);
- resp.sendRedirect("processagerenciar");
- }
- else{
- req.setAttribute("errosEditar",erros);
- req.getRequestDispatcher("editar.jsp").forward(req, resp);
- }
- }
- }
- --------
- package br.sys.beans;
- import java.io.Serializable;
- public class Usuario extends Pessoa implements Serializable{
- private static final long serialVersionUID = 1L;
- }
- package br.sys.beans;
- public abstract class Pessoa {
- private String username;
- private String password;
- private String nome;
- private String email;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }
- --------
- package br.sys.jdbc;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import br.sys.beans.Usuario;
- public class UsuarioDAO {
- private static final String SQL_insert = "insert into Usuarios value(?,?,?,?)";
- private static final String SQL_select = "select * from Usuarios where username=?";
- private static final String SQL_select_all = "select * from Usuarios";
- private static final String SQL_select_auth = "select * from Usuarios where username=? and password=?";
- private static final String SQL_delete = "delete from Usuarios where username=?";
- private static final String SQL_update = "update Usuarios set password=?,nome=?,email=? where username=?";
- public static void insert(Usuario usuario) {
- Connection con = ConnectionUtil.getConnection();
- try {
- PreparedStatement stm = con.prepareStatement(SQL_insert);
- stm.setString(1,usuario.getUsername());
- stm.setString(2,usuario.getPassword());
- stm.setString(3,usuario.getNome());
- stm.setString(4,usuario.getEmail());
- stm.execute();
- ConnectionUtil.close(stm, con);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static Usuario getUsuario(String username) {
- Connection con = ConnectionUtil.getConnection();
- Usuario usuario = new Usuario();
- try {
- PreparedStatement stm = con.prepareStatement(SQL_select);
- stm.setString(1,username);
- ResultSet result = stm.executeQuery();
- if (result.next()) {
- usuario.setUsername(result.getString(1));
- usuario.setPassword(result.getString(2));
- usuario.setNome(result.getString(3));
- usuario.setEmail(result.getString(4));
- return usuario;
- }
- ConnectionUtil.close(result, stm, con);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static boolean isUsuarioAuth(String username,String password) {
- Connection con = ConnectionUtil.getConnection();
- try {
- PreparedStatement stm = con.prepareStatement(SQL_select_auth);
- stm.setString(1,username);
- stm.setString(2,password);
- ResultSet result = stm.executeQuery();
- if (result.next()) {
- return true;
- }
- ConnectionUtil.close(result, stm, con);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- public static void delete(String username) {
- Connection con = ConnectionUtil.getConnection();
- try {
- PreparedStatement stm = con.prepareStatement(SQL_delete);
- stm.setString(1,username);
- stm.executeUpdate();
- ConnectionUtil.close(stm, con);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static ArrayList<Usuario> findAll() {
- ArrayList<Usuario> usuarios = new ArrayList<Usuario>();
- Connection con = ConnectionUtil.getConnection();
- try {
- PreparedStatement stm = con.prepareStatement(SQL_select_all);
- ResultSet result = stm.executeQuery();
- while (result.next()) {
- Usuario usuario = new Usuario();
- usuario.setUsername(result.getString(1));
- usuario.setPassword(result.getString(2));
- usuario.setNome(result.getString(3));
- usuario.setEmail(result.getString(4));
- usuarios.add(usuario);
- }
- ConnectionUtil.close(result, stm, con);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return usuarios;
- }
- public static void update(Usuario usuario) {
- Connection con = ConnectionUtil.getConnection();
- try {
- PreparedStatement stm = con.prepareStatement(SQL_update);
- stm.setString(1,usuario.getPassword());
- stm.setString(2,usuario.getNome());
- stm.setString(3,usuario.getEmail());
- stm.setString(4,usuario.getUsername());
- stm.executeUpdate();
- ConnectionUtil.close(stm, con);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- --------
- package br.sys.jdbc;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ConnectionUtil {
- private static final String url = "jdbc:mysql://localhost:3306/dbsys";
- private static final String user = "root";
- private static final String pw = "root";
- private static final String driver = "com.mysql.jdbc.Driver";
- public static Connection getConnection() {
- Connection con = null;
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(url, user, pw);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.out.println("Erro no driver");
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.println("Erro na autenticacao");
- }
- if (con!=null) {
- // System.out.println("Conexao estabelecida");
- }
- return con;
- }
- public static void close(Statement stm, Connection con) {
- if (stm!=null) {
- try {
- stm.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (con!=null) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void close(ResultSet result, Statement stm, Connection con) {
- close(stm, con);
- if (result!=null) {
- try {
- result.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement