Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.12 KB | None | 0 0
  1. package br.sys.servlets;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10.  
  11. import br.sys.beans.Usuario;
  12. import br.sys.jdbc.UsuarioDAO;
  13.  
  14. public class Editar extends HttpServlet {
  15.    
  16.     private static final long serialVersionUID = 1L;
  17.  
  18.     @Override
  19.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  20.             throws ServletException, IOException {
  21.        
  22.         Usuario usuario = UsuarioDAO.getUsuario(req.getParameter("username"));
  23.        
  24.         HttpSession session = req.getSession();
  25.        
  26.         session.setAttribute("username",usuario.getUsername());
  27.         session.setAttribute("password",usuario.getPassword());
  28.         session.setAttribute("nome",usuario.getNome());
  29.         session.setAttribute("email",usuario.getEmail());
  30.        
  31.         resp.sendRedirect("editar.jsp");
  32.     }
  33. }
  34.  
  35. --------
  36.  
  37. package br.sys.servlets;
  38.  
  39. import java.io.IOException;
  40.  
  41. import javax.servlet.ServletException;
  42. import javax.servlet.http.HttpServlet;
  43. import javax.servlet.http.HttpServletRequest;
  44. import javax.servlet.http.HttpServletResponse;
  45.  
  46. import br.sys.jdbc.UsuarioDAO;
  47.  
  48. public class Excluir extends HttpServlet{
  49.    
  50.     private static final long serialVersionUID = 1L;
  51.  
  52.     @Override
  53.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  54.             throws ServletException, IOException {
  55.        
  56.         String[] usernames = req.getParameterValues("excluir");
  57.        
  58.         if (usernames!=null) {
  59.             for (String username : usernames) {
  60.                 UsuarioDAO.delete(username);
  61.             }
  62.         }
  63.         resp.sendRedirect("processagerenciar");
  64.     }
  65. }
  66.  
  67. --------
  68.  
  69. package br.sys.servlets;
  70.  
  71. import java.io.IOException;
  72. import java.util.ArrayList;
  73.  
  74. import javax.servlet.ServletException;
  75. import javax.servlet.http.HttpServlet;
  76. import javax.servlet.http.HttpServletRequest;
  77. import javax.servlet.http.HttpServletResponse;
  78.  
  79. import br.sys.beans.Usuario;
  80. import br.sys.jdbc.UsuarioDAO;
  81.  
  82. public class ProcessaCadastro extends HttpServlet{
  83.    
  84.     private static final long serialVersionUID = 1L;
  85.  
  86.     @Override
  87.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  88.             throws ServletException, IOException {
  89.        
  90.         ArrayList<String> erros = Validator.validaCadastro(req);
  91.        
  92.         if (erros.isEmpty()) {
  93.             Usuario usuario = new Usuario();
  94.            
  95.             usuario.setUsername(req.getParameter("username"));
  96.             usuario.setPassword(req.getParameter("password"));
  97.             usuario.setNome(req.getParameter("nome"));
  98.             usuario.setEmail(req.getParameter("email"));
  99.             UsuarioDAO.insert(usuario);
  100.             resp.sendRedirect("processagerenciar");
  101.         }
  102.         else{
  103.             req.setAttribute("errosCadastro",erros);
  104.             req.getRequestDispatcher("cadastro.jsp").forward(req, resp);
  105.         }
  106.     }
  107. }
  108.  
  109. --------
  110.  
  111. package br.sys.servlets;
  112.  
  113. import java.io.IOException;
  114.  
  115. import javax.servlet.ServletException;
  116. import javax.servlet.http.HttpServlet;
  117. import javax.servlet.http.HttpServletRequest;
  118. import javax.servlet.http.HttpServletResponse;
  119. import javax.servlet.http.HttpSession;
  120.  
  121. import br.sys.jdbc.UsuarioDAO;
  122.  
  123. public class ProcessaGerenciar extends HttpServlet {
  124.  
  125.     private static final long serialVersionUID = 1L;
  126.  
  127.     @Override
  128.     protected void service(HttpServletRequest req, HttpServletResponse resp)
  129.             throws ServletException, IOException {
  130.        
  131.         HttpSession session = req.getSession();
  132.         session.setAttribute("usuarios",UsuarioDAO.findAll());
  133.         resp.sendRedirect("gerenciar.jsp");
  134.     }
  135. }
  136.  
  137. --------
  138.  
  139. package br.sys.servlets;
  140.  
  141. import java.util.ArrayList;
  142.  
  143. import javax.servlet.http.HttpServlet;
  144. import javax.servlet.http.HttpServletRequest;
  145.  
  146. import br.sys.jdbc.UsuarioDAO;
  147.  
  148. public class Validator extends HttpServlet{
  149.  
  150.     private static final long serialVersionUID = 1L;
  151.    
  152.     /*
  153.     private static boolean getInteger(String sInt) {
  154.         try {
  155.             if (Integer.parseInt(sInt)<0) {
  156.                 return true;
  157.             }
  158.         } catch (NumberFormatException e) {
  159.             e.getStackTrace();
  160.             return true;
  161.         } catch (NullPointerException e) {
  162.             e.getStackTrace();
  163.             return true;
  164.         }
  165.         return false;
  166.     }
  167.     */
  168.    
  169.     private static boolean getString(String str) {
  170.         if(str.trim().isEmpty())
  171.             return true;
  172.         return false;
  173.     }  
  174.    
  175.     public static ArrayList<String> validaIndex(HttpServletRequest req) {
  176.         ArrayList<String> erros = new ArrayList<String>();
  177.  
  178.         if(getString(req.getParameter("username"))&& getString(req.getParameter("password"))){
  179.             erros.add("Digite um username valido.");
  180.             erros.add("Digite um password valido.");
  181.         }
  182.         else{
  183.             if(UsuarioDAO.getUsuario(req.getParameter("username"))==null)
  184.                 erros.add("Nao existe o usuario informado.");
  185.             else{
  186.                 if(!UsuarioDAO.isUsuarioAuth(req.getParameter("username"),req.getParameter("password")))
  187.                     erros.add("O usuario e senha nao conferem.");
  188.             }
  189.         }
  190.         return erros;
  191.     }
  192.        
  193.     public static ArrayList<String> validaCadastro(HttpServletRequest req) {
  194.         ArrayList<String> erros = new ArrayList<String>();
  195.        
  196.         if(getString(req.getParameter("username")))
  197.             erros.add("Digite um username valido.");
  198.  
  199.         if(UsuarioDAO.getUsuario(req.getParameter("username"))!=null)
  200.             erros.add("O username ja existe.");    
  201.        
  202.         if(getString(req.getParameter("password")))
  203.             erros.add("Digite um password valido.");       
  204.        
  205.         if(getString(req.getParameter("nome")))
  206.             erros.add("Digite um nome valido.");
  207.        
  208.         if((getString(req.getParameter("email"))) || !(req.getParameter("email").contains("@")))
  209.             erros.add("Digite um email valido ex. pat@example.com .");
  210.        
  211.         return erros;
  212.     }
  213.    
  214.     public static ArrayList<String> validaEditar(HttpServletRequest req) {
  215.         ArrayList<String> erros = new ArrayList<String>();
  216.    
  217.         if(getString(req.getParameter("password")))
  218.             erros.add("Digite um password valido.");       
  219.        
  220.         if(getString(req.getParameter("nome")))
  221.             erros.add("Digite um nome valido.");
  222.        
  223.         if((getString(req.getParameter("email"))) || !(req.getParameter("email").contains("@")))
  224.             erros.add("Digite um email valido ex. pat@example.com .");
  225.        
  226.         return erros;
  227.     }
  228. }
  229.  
  230. --------
  231.  
  232. package br.sys.servlets;
  233.  
  234. import java.io.IOException;
  235. import java.util.ArrayList;
  236.  
  237. import javax.servlet.ServletException;
  238. import javax.servlet.http.HttpServlet;
  239. import javax.servlet.http.HttpServletRequest;
  240. import javax.servlet.http.HttpServletResponse;
  241. import javax.servlet.http.HttpSession;
  242.  
  243. public class ProcessaIndex extends HttpServlet{
  244.  
  245.     private static final long serialVersionUID = 1L;
  246.    
  247.     @Override
  248.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  249.             throws ServletException, IOException {
  250.    
  251.         ArrayList<String> erros = Validator.validaIndex(req);
  252.        
  253.         if (erros.isEmpty()) {
  254.             HttpSession session = req.getSession();
  255.             session.setAttribute("login",req.getParameter("username"));
  256.             resp.sendRedirect("processagerenciar");
  257.         }
  258.         else{
  259.             req.setAttribute("errosIndex",erros);
  260.             req.getRequestDispatcher("index.jsp").forward(req,resp);
  261.         }
  262.     }
  263. }
  264.  
  265. --------
  266.  
  267. package br.sys.servlets;
  268.  
  269. import java.io.IOException;
  270. import java.util.ArrayList;
  271.  
  272. import javax.servlet.ServletException;
  273. import javax.servlet.http.HttpServlet;
  274. import javax.servlet.http.HttpServletRequest;
  275. import javax.servlet.http.HttpServletResponse;
  276. import javax.servlet.http.HttpSession;
  277.  
  278. import br.sys.beans.Usuario;
  279. import br.sys.jdbc.UsuarioDAO;
  280.  
  281. public class ProcessaEditar extends HttpServlet{
  282.  
  283.     private static final long serialVersionUID = 1L;
  284.  
  285.     @Override
  286.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  287.             throws ServletException, IOException {
  288.        
  289.         ArrayList<String> erros = Validator.validaEditar(req);
  290.         HttpSession session = req.getSession();
  291.        
  292.         if (erros.isEmpty()) {
  293.             Usuario usuario = new Usuario();
  294.             usuario.setUsername((String)session.getAttribute("username"));
  295.             usuario.setPassword(req.getParameter("password"));
  296.             usuario.setNome(req.getParameter("nome"));
  297.             usuario.setEmail(req.getParameter("email"));
  298.             UsuarioDAO.update(usuario);
  299.             resp.sendRedirect("processagerenciar");
  300.         }
  301.         else{
  302.             req.setAttribute("errosEditar",erros);
  303.             req.getRequestDispatcher("editar.jsp").forward(req, resp);
  304.         }
  305.     }
  306. }
  307.  
  308. --------
  309.  
  310. package br.sys.beans;
  311.  
  312. import java.io.Serializable;
  313.  
  314. public class Usuario extends Pessoa implements Serializable{
  315.  
  316.     private static final long serialVersionUID = 1L;
  317.    
  318. }
  319. package br.sys.beans;
  320.  
  321. public abstract class Pessoa {
  322.     private String username;
  323.     private String password;
  324.     private String nome;
  325.     private String email;
  326.    
  327.     public String getUsername() {
  328.         return username;
  329.     }
  330.     public void setUsername(String username) {
  331.         this.username = username;
  332.     }
  333.     public String getPassword() {
  334.         return password;
  335.     }
  336.     public void setPassword(String password) {
  337.         this.password = password;
  338.     }
  339.     public String getNome() {
  340.         return nome;
  341.     }
  342.     public void setNome(String nome) {
  343.         this.nome = nome;
  344.     }
  345.     public String getEmail() {
  346.         return email;
  347.     }
  348.     public void setEmail(String email) {
  349.         this.email = email;
  350.     }
  351. }
  352.  
  353. --------
  354.  
  355. package br.sys.jdbc;
  356.  
  357. import java.sql.Connection;
  358. import java.sql.PreparedStatement;
  359. import java.sql.ResultSet;
  360. import java.sql.SQLException;
  361. import java.util.ArrayList;
  362.  
  363. import br.sys.beans.Usuario;
  364.  
  365. public class UsuarioDAO {
  366.    
  367.     private static final String SQL_insert = "insert into Usuarios value(?,?,?,?)";
  368.     private static final String SQL_select = "select * from Usuarios where username=?";
  369.     private static final String SQL_select_all = "select * from Usuarios";
  370.     private static final String SQL_select_auth = "select * from Usuarios where username=? and password=?";
  371.    
  372.     private static final String SQL_delete = "delete from Usuarios where username=?";
  373.     private static final String SQL_update = "update Usuarios set password=?,nome=?,email=? where username=?";
  374.    
  375.     public static void insert(Usuario usuario) {
  376.         Connection con = ConnectionUtil.getConnection();
  377.        
  378.         try {
  379.             PreparedStatement stm = con.prepareStatement(SQL_insert);
  380.             stm.setString(1,usuario.getUsername());
  381.             stm.setString(2,usuario.getPassword());
  382.             stm.setString(3,usuario.getNome());
  383.             stm.setString(4,usuario.getEmail());
  384.             stm.execute();
  385.             ConnectionUtil.close(stm, con);
  386.         } catch (SQLException e) {
  387.             e.printStackTrace();
  388.         }
  389.     }
  390.    
  391.     public static Usuario getUsuario(String username) {
  392.         Connection con = ConnectionUtil.getConnection();
  393.         Usuario usuario = new Usuario();
  394.        
  395.         try {
  396.             PreparedStatement stm = con.prepareStatement(SQL_select);
  397.             stm.setString(1,username);
  398.             ResultSet result = stm.executeQuery();
  399.             if (result.next()) {
  400.                 usuario.setUsername(result.getString(1));
  401.                 usuario.setPassword(result.getString(2));
  402.                 usuario.setNome(result.getString(3));
  403.                 usuario.setEmail(result.getString(4));
  404.                 return usuario;
  405.             }
  406.             ConnectionUtil.close(result, stm, con);
  407.         } catch (SQLException e) {
  408.             e.printStackTrace();
  409.         }
  410.         return null;
  411.     }
  412.    
  413.     public static boolean isUsuarioAuth(String username,String password) {
  414.         Connection con = ConnectionUtil.getConnection();
  415.         try {
  416.             PreparedStatement stm = con.prepareStatement(SQL_select_auth);
  417.             stm.setString(1,username);
  418.             stm.setString(2,password);
  419.             ResultSet result = stm.executeQuery();
  420.             if (result.next()) {
  421.                 return true;
  422.             }
  423.             ConnectionUtil.close(result, stm, con);
  424.         } catch (SQLException e) {
  425.             e.printStackTrace();
  426.         }
  427.         return false;
  428.     }
  429.  
  430.     public static void delete(String username) {
  431.         Connection con = ConnectionUtil.getConnection();
  432.        
  433.         try {
  434.             PreparedStatement stm = con.prepareStatement(SQL_delete);
  435.             stm.setString(1,username);
  436.             stm.executeUpdate();
  437.             ConnectionUtil.close(stm, con);
  438.         } catch (SQLException e) {
  439.             e.printStackTrace();
  440.         }
  441.     }
  442.        
  443.     public static ArrayList<Usuario> findAll() {
  444.         ArrayList<Usuario> usuarios = new ArrayList<Usuario>();
  445.         Connection con = ConnectionUtil.getConnection();
  446.         try {
  447.             PreparedStatement stm = con.prepareStatement(SQL_select_all);
  448.             ResultSet result = stm.executeQuery();
  449.             while (result.next()) {
  450.                 Usuario usuario = new Usuario();
  451.                 usuario.setUsername(result.getString(1));
  452.                 usuario.setPassword(result.getString(2));
  453.                 usuario.setNome(result.getString(3));
  454.                 usuario.setEmail(result.getString(4));
  455.                 usuarios.add(usuario);
  456.             }
  457.             ConnectionUtil.close(result, stm, con);
  458.         } catch (SQLException e) {
  459.             e.printStackTrace();
  460.         }
  461.         return usuarios;
  462.     }
  463.  
  464.     public static void update(Usuario usuario) {
  465.         Connection con = ConnectionUtil.getConnection();
  466.    
  467.         try {          
  468.             PreparedStatement stm = con.prepareStatement(SQL_update);
  469.             stm.setString(1,usuario.getPassword());
  470.             stm.setString(2,usuario.getNome());
  471.             stm.setString(3,usuario.getEmail());
  472.             stm.setString(4,usuario.getUsername());
  473.             stm.executeUpdate();
  474.             ConnectionUtil.close(stm, con);
  475.         } catch (SQLException e) {
  476.             e.printStackTrace();
  477.         }      
  478.     }
  479. }
  480.  
  481. --------
  482.  
  483. package br.sys.jdbc;
  484.  
  485. import java.sql.Connection;
  486. import java.sql.DriverManager;
  487. import java.sql.ResultSet;
  488. import java.sql.SQLException;
  489. import java.sql.Statement;
  490.  
  491. public class ConnectionUtil {
  492.    
  493.     private static final String url = "jdbc:mysql://localhost:3306/dbsys";
  494.     private static final String user = "root";
  495.     private static final String pw = "root";
  496.     private static final String driver = "com.mysql.jdbc.Driver";
  497.    
  498.     public static Connection getConnection() {
  499.         Connection con = null;
  500.         try {
  501.             Class.forName(driver);
  502.             con = DriverManager.getConnection(url, user, pw);
  503.         } catch (ClassNotFoundException e) {
  504.             e.printStackTrace();
  505.             System.out.println("Erro no driver");
  506.         } catch (SQLException e) {
  507.             e.printStackTrace();
  508.             System.out.println("Erro na autenticacao");
  509.         }
  510.         if (con!=null) {
  511.             // System.out.println("Conexao estabelecida");
  512.         }
  513.         return con;
  514.     }
  515.    
  516.     public static void close(Statement stm, Connection con) {
  517.         if (stm!=null) {
  518.             try {
  519.                 stm.close();
  520.             } catch (SQLException e) {
  521.                 e.printStackTrace();
  522.             }
  523.         }
  524.         if (con!=null) {
  525.             try {
  526.                 con.close();
  527.             } catch (SQLException e) {
  528.                 e.printStackTrace();
  529.             }
  530.         }
  531.     }
  532.    
  533.     public static void close(ResultSet result, Statement stm, Connection con) {
  534.         close(stm, con);
  535.         if (result!=null) {
  536.             try {
  537.                 result.close();
  538.             } catch (SQLException e) {
  539.                 e.printStackTrace();
  540.             }
  541.         }
  542.  
  543.     }
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement