Advertisement
Guest User

AKJDSHDA

a guest
Dec 8th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.41 KB | None | 0 0
  1. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  2. <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
  3.  
  4. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Registro</title>
  10. </head>
  11. <body>
  12. <form method="post" action="ServletUsuario">
  13. <table>
  14. <tr> <td>Registro</td></tr>
  15. <tr>
  16. <td>Rut</td>
  17. <td>
  18. <input type="text" name="txtRut"
  19. required/>
  20. </td>
  21. </tr>
  22. <tr>
  23. <td>Clave</td>
  24. <td>
  25. <input type="password" name="txtClave"
  26. required/>
  27. </td>
  28. </tr>
  29. <tr>
  30. <td>Confirmar Clave</td>
  31. <td>
  32. <input type="password" name="txtConfirmarClave"
  33. required/>
  34. </td>
  35. </tr>
  36. <tr>
  37. <td>Nombre</td>
  38. <td>
  39. <input type="text" name="txtNombre"
  40. required/>
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>Apellido Paterno</td>
  45. <td>
  46. <input type="text" name="txtApPaterno"
  47. required/>
  48. </td>
  49. </tr>
  50. <tr>
  51. <td>Apellido Materno</td>
  52. <td>
  53. <input type="text" name="txtApMaterno"
  54. required/>
  55. </td>
  56. </tr>
  57. <tr>
  58. <td>Direccion</td>
  59. <td>
  60. <input type="text" name="txtDireccion"
  61. required/>
  62. </td>
  63. </tr>
  64. <tr>
  65. <td>Numeración</td>
  66. <td>
  67. <input type="text" name="txtNumeracion"
  68. required/>
  69. </td>
  70. </tr>
  71. <tr>
  72. <td>Comuna</td>
  73. <td>
  74. <sql:setDataSource driver="com.mysql.jdbc.Driver"
  75. password="" user="root"
  76. url="jdbc:mysql://localhost/examen"
  77. var="bd"/>
  78. <sql:query var="registro" dataSource="${bd}">
  79. SELECT * FROM COMUNA;
  80. </sql:query>
  81. <select name="cbo_opcion">
  82. <option value="0">Seleccione</option>
  83. <c:forEach var="reg" items="${registro.rows}">
  84. <option value="${reg.id_comuna}">
  85. <c:out value="${reg.NOMBRE}"/>
  86. </option>
  87. </c:forEach>
  88. </select>
  89. </td>
  90. </tr>
  91. <tr>
  92. <td>Teléfono</td>
  93. <td>
  94. <input type="text" name="txtTelefono"
  95. required/>
  96. </td>
  97. </tr>
  98. <tr>
  99. <td>
  100. <input type="submit" value="Registrar" name="btn_accion">
  101. </td>
  102. </tr>
  103. </table>
  104. </form>
  105. <div>
  106. Mensaje: ${mensaje}
  107. </div>
  108. </body>
  109. </html>
  110.  
  111.  
  112.  
  113.  
  114. ////////////////////////////////////////////////////////////////
  115. DAO
  116. package controladores;
  117.  
  118. import conexion.Cl_Conexion;
  119. import pojos.Cl_Cliente;
  120. import java.sql.Connection;
  121. import java.sql.PreparedStatement;
  122. import java.sql.ResultSet;
  123.  
  124.  
  125. public class DaoCliente {
  126. private Connection conexion;
  127. private String rut="";
  128.  
  129. public DaoCliente() {
  130. conexion=new Cl_Conexion().getConexion();
  131. }
  132.  
  133. public String getRut() {
  134. return rut;
  135. }
  136.  
  137.  
  138. //metodo validar
  139. public boolean validar(Cl_Cliente cliente){
  140. try {
  141. PreparedStatement pstm=conexion.prepareCall("SELECT * FROM cliente WHERE rut=? AND clave=?");
  142. pstm.setString(1, cliente.getRut());
  143. pstm.setInt(2, cliente.getClave());
  144. ResultSet rs=pstm.executeQuery();
  145. if (rs.next()) {
  146. rut=rs.getString("rut");
  147. return true;
  148. }
  149. return false;
  150. } catch (Exception e) {
  151. return false;
  152. }
  153. }
  154. public String recuperarNombre(Cl_Cliente c){
  155. try {
  156. PreparedStatement pstm=conexion.prepareCall("SELECT * FROM cliente WHERE rut=? AND clave=?");
  157. pstm.setString(1, c.getRut());
  158. pstm.setInt(2, c.getClave());
  159. ResultSet rs=pstm.executeQuery();
  160. if (rs.next()) {
  161. return rs.getString("nombre");
  162. }
  163. return null;
  164. } catch (Exception e) {
  165. return null;
  166. }
  167. }
  168.  
  169. public boolean agregarCliente(Cl_Cliente nc) {
  170. try {
  171. String sql = "INSERT INTO CLIENTE VALUES(?,?,?,?,?,?,?,?,?)";
  172. PreparedStatement pstm = conexion.prepareCall(sql);
  173. pstm.setInt(1, nc.getClave());
  174. pstm.setString(2, nc.getRut());
  175. pstm.setString(3, nc.getNombre());
  176. pstm.setString(4, nc.getApellido_p());
  177. pstm.setString(5, nc.getApellido_m());
  178. pstm.setString(6, nc.getDireccion());
  179. pstm.setInt(7, nc.getNumeracion_d());
  180. pstm.setInt(8, nc.getId_comuna());
  181. pstm.setInt(9, nc.getTelefono());
  182. int cantidad = pstm.executeUpdate();
  183. return cantidad > 0;
  184. } catch (Exception e) {
  185. System.out.println("Error mnmnmnm" + e.getMessage());
  186. return false;
  187. }
  188. }
  189.  
  190.  
  191.  
  192. }
  193.  
  194.  
  195. ///////////////////////////////////////////
  196. SERVLET
  197. package servlet;
  198.  
  199. import controladores.DaoCliente;
  200. import java.io.IOException;
  201. import java.io.PrintWriter;
  202. import javax.servlet.ServletException;
  203. import javax.servlet.annotation.WebServlet;
  204. import javax.servlet.http.HttpServlet;
  205. import javax.servlet.http.HttpServletRequest;
  206. import javax.servlet.http.HttpServletResponse;
  207. import javax.servlet.http.HttpSession;
  208. import pojos.Cl_Cliente;
  209.  
  210. /**
  211. *
  212. * @author bACS
  213. */
  214. @WebServlet(name = "ServletUsuario", urlPatterns = {"/ServletUsuario"})
  215. public class ServletUsuario extends HttpServlet {
  216.  
  217. /**
  218. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  219. * methods.
  220. *
  221. * @param request servlet request
  222. * @param response servlet response
  223. * @throws ServletException if a servlet-specific error occurs
  224. * @throws IOException if an I/O error occurs
  225. */
  226. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  227. throws ServletException, IOException {
  228. //response.setContentType("text/html;charset=UTF-8");
  229.  
  230. String btn = request.getParameter("btn_accion");
  231. if (btn.equals("Enviar")) {
  232. enviar(request, response);
  233. }
  234. if (btn.equals("Registrar")) {
  235. registrar(request, response);
  236. }
  237. }
  238.  
  239. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  240. /**
  241. * Handles the HTTP <code>GET</code> method.
  242. *
  243. * @param request servlet request
  244. * @param response servlet response
  245. * @throws ServletException if a servlet-specific error occurs
  246. * @throws IOException if an I/O error occurs
  247. */
  248. @Override
  249. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  250. throws ServletException, IOException {
  251. processRequest(request, response);
  252. }
  253.  
  254. /**
  255. * Handles the HTTP <code>POST</code> method.
  256. *
  257. * @param request servlet request
  258. * @param response servlet response
  259. * @throws ServletException if a servlet-specific error occurs
  260. * @throws IOException if an I/O error occurs
  261. */
  262. @Override
  263. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  264. throws ServletException, IOException {
  265. processRequest(request, response);
  266. }
  267.  
  268. /**
  269. * Returns a short description of the servlet.
  270. *
  271. * @return a String containing servlet description
  272. */
  273. @Override
  274. public String getServletInfo() {
  275. return "Short description";
  276. }// </editor-fold>
  277.  
  278. private void enviar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  279. int clave = Integer.parseInt(request.getParameter("txtClave"));
  280. String rut = request.getParameter("txtRut");
  281.  
  282. Cl_Cliente cli = new Cl_Cliente(clave, rut);
  283. DaoCliente dao = new DaoCliente();
  284. boolean resp = dao.validar(cli);
  285. String msj = (resp ? "Usuario existe" : "No existe");
  286. request.setAttribute("mensaje", msj);
  287. if (resp) {
  288. //se toma la sesion actual del sistema
  289. HttpSession se = request.getSession();
  290. String nombre_apellido = new DaoCliente().recuperarNombre(cli);
  291. se.setAttribute("nombreCompleto", "Bienvenido(a): " + nombre_apellido);
  292. request.getRequestDispatcher("menu.jsp").forward(request, response);
  293. } else {
  294. request.getRequestDispatcher("login.jsp").forward(request, response);
  295. }
  296. }
  297.  
  298. private void registrar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  299. boolean rpta ;
  300. try {
  301. int clave = Integer.parseInt(request.getParameter("txtClave"));
  302. int clave2 = Integer.parseInt(request.getParameter("txtConfirmarClave"));
  303. //validamos la contraseña
  304. if (clave != clave2) {
  305. request.setAttribute("mensaje", "La clave debe coincidir");
  306. request.getRequestDispatcher("registro.jsp").forward(request, response);
  307. rpta = false;
  308. }
  309. //recibimos parametros del formulario
  310. String rut = request.getParameter("txtRut");
  311. String nombre = request.getParameter("txtNombre");
  312. String apellido_p = request.getParameter("txtApPaterno");
  313. String apellido_m = request.getParameter("txtApMaterno");
  314. String direccion = request.getParameter("txtDireccion");
  315. int numeracion_d = Integer.parseInt(request.getParameter("txtNumeracion"));
  316. int id_comuna = Integer.parseInt(request.getParameter("cbo_opcion"));
  317. int telefono = Integer.parseInt(request.getParameter("txtTelefono"));
  318. //creamos un nuevo cliente
  319. Cl_Cliente nc = new Cl_Cliente(clave, numeracion_d, id_comuna, telefono, rut, nombre, apellido_p, apellido_m, direccion);
  320. DaoCliente dao = new DaoCliente();
  321. //lo agregamos con el metodo que está en el dao
  322. rpta = dao.agregarCliente(nc);
  323. String mensaje = "";
  324. mensaje = (rpta ? "Cliente agregado" : "Cliente no agregado");
  325. request.setAttribute("mensaje", ".." + mensaje);
  326. } catch (Exception e) {
  327. request.setAttribute("mensaje", "--" + e.getMessage());
  328. } finally {
  329. request.getRequestDispatcher("registro.jsp").forward(request, response);
  330. }
  331. }
  332.  
  333. }
  334.  
  335.  
  336. //////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement