Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. private void agregar(HttpServletRequest request, HttpServletResponse response) throws IOException {
  2. try {
  3. request.getSession().removeAttribute("msjNo");
  4. request.getSession().removeAttribute("msjOk");
  5. request.getSession().removeAttribute("mensaje");
  6.  
  7. String rut = request.getParameter("txtRut");
  8. String dv = request.getParameter("txtDv");
  9.  
  10. String user = request.getParameter("txtUser");
  11. String pass = request.getParameter("txtPass");
  12.  
  13. String nombre = request.getParameter("txtNombre");
  14. String apellido = request.getParameter("txtApellido");
  15.  
  16. //GUARDAR FECHA
  17. String fechaNacimiento = request.getParameter("dtpFechaNacimiento");
  18. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  19. SimpleDateFormat anio = new SimpleDateFormat("yyyy");
  20. Date fecha = sdf.parse(fechaNacimiento);
  21. DateFormat outputFormat = new SimpleDateFormat("yyyy");
  22. DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
  23. Date date = inputFormat.parse(fechaNacimiento);
  24. String outputText = outputFormat.format(date);
  25. int anioNacimiento = Integer.parseInt(outputText);
  26. Calendar fechaActual = Calendar.getInstance();
  27. int anioActual = fechaActual.get(Calendar.YEAR);
  28.  
  29. int edad = anioActual - anioNacimiento;
  30.  
  31. String direccion = request.getParameter("txtDireccion");
  32. int telefono = Integer.parseInt(request.getParameter("txtTelefono"));
  33.  
  34.  
  35.  
  36. //VALIDAR RUT
  37. boolean validacion = false;
  38.  
  39. rut = rut.toUpperCase();
  40. rut = rut.replace(".", "");
  41. rut = rut.replace("-", "");
  42. int rutAux = Integer.parseInt(rut.substring(0, rut.length()));
  43.  
  44. char dvVerificar = dv.charAt(0);
  45.  
  46. int m = 0, s = 1;
  47. for (; rutAux != 0; rutAux /= 10) {
  48. s = (s + rutAux % 10 * (9 - m++ % 6)) % 11;
  49. }
  50. if (dvVerificar == (char) (s != 0 ? s + 47 : 75)) {
  51. validacion = true;
  52. }
  53. //FIN VALIDAR RUT
  54.  
  55. TipoUsuario tipo = new TipoUsuario(3);
  56.  
  57. Persona per = new Persona(rut, dv, nombre, apellido, fecha, edad, direccion, telefono, true);
  58.  
  59. PersonaDao daoPer = (PersonaDao) request.getSession().getAttribute("personas");
  60.  
  61. Usuario usuario = new Usuario(per, tipo, user, pass, true);
  62.  
  63. UsuarioDao daoUser = (UsuarioDao) request.getSession().getAttribute("usuarios");
  64.  
  65. if (daoPer == null) {
  66. daoPer = new PersonaDao();
  67. }
  68. if (daoUser == null) {
  69. daoUser = new UsuarioDao();
  70. }
  71. if (daoPer.agregar(per) && daoUser.agregar(usuario) && validacion) {
  72. request.getSession().setAttribute("msjOk", "Bienvenido " + nombre + "!");
  73. } else {
  74. request.getSession().setAttribute("msjNo", "Cliente No Agregado!");
  75. }
  76. request.getSession().setAttribute("personas", daoPer);
  77. request.getSession().setAttribute("usuarios", daoUser);
  78. } catch (Exception e) {
  79. request.getSession().setAttribute("msjNo", "Datos incorrectos (" + e.getMessage() + ")");
  80. } finally {
  81. response.sendRedirect("agregarCliente.jsp");
  82. }
  83. }
  84.  
  85. --------------------------------------------------
  86.  
  87.  
  88. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  89. <%--
  90. Document : agregar
  91. Created on : 28-09-2018, 12:41:28
  92. Author : Claudio
  93. --%>
  94.  
  95. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  96. <!DOCTYPE html>
  97. <html>
  98. <head>
  99. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  100. <title>Agregar Cliente</title>
  101.  
  102. <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
  103. <script src="js/jquery-3.2.0.min.js" type="text/javascript"></script>
  104. <script src="js/bootstrap.min.js" type="text/javascript"></script>
  105. </head>
  106. <body>
  107. <div class="container">
  108. <div class="row">
  109. <div class="col-sm-12">
  110.  
  111. </div>
  112. </div>
  113. <div class="row">
  114. <div class="col-sm-3">
  115. </div>
  116. <div class="col-sm-6">
  117. <form action="procesoCliente" method="POST">
  118. <h1>Registro de cliente:</h1><br>
  119. <div class="form-group">
  120. <label>Rut (SIN DIGITO VERIFICADOR):</label>
  121. <input type="text" name="txtRut" value="" placeholder="Ej: 12345678" class="form-control" required="" />
  122. </div>
  123. <div class="form-group">
  124. <label>Digito Verificador:</label>
  125. <input type="text" name="txtDv" value="" placeholder="Ej: 1" class="form-control" required="" />
  126. </div>
  127. <div class="form-group">
  128. <label>Nombre de usuario:</label>
  129. <input type="text" name="txtUser" value="" placeholder="Ingrese nombre de usuario" class="form-control" required="" />
  130. </div>
  131. <div class="form-group">
  132. <label>Contraseña:</label>
  133. <input type="password" name="txtPass" value="" placeholder="Ingrese contraseña" class="form-control" required="" />
  134. </div>
  135. <div class="form-group">
  136. <label>Nombre:</label>
  137. <input type="text" name="txtNombre" value="" placeholder="Ingrese su nombre" class="form-control" required="" />
  138. </div>
  139. <div class="form-group">
  140. <label>Apellido:</label>
  141. <input type="text" name="txtApellido" value="" placeholder="Ingrese su apellido" class="form-control" required="" />
  142. </div>
  143. <div class="form-group">
  144. <label>Fecha Nacimiento:</label>
  145. <input type="date" name="dtpFechaNacimiento">
  146. </div>
  147. <div class="form-group">
  148. <label>Direccion:</label>
  149. <input type="text" name="txtDireccion" value="" placeholder="Ingrese su dirección" class="form-control" required="" />
  150. </div>
  151. <div class="form-group">
  152. <label>Telefono (SIN ANTEPONER EL (+569) PARA CELULAR O EL (2) PARA FIJOS):</label>
  153. <input type="text" name="txtTelefono" value="" placeholder="Ingrese su telefono" class="form-control" required="" />
  154. </div>
  155. <div class="form-group text-center">
  156. <input type="submit" name="btnAccion" value="Agregar" class="btn btn-primary" />
  157. <input type="reset" name="btnAccion" value="Limpiar" class="btn btn-danger" />
  158. </div>
  159. </form>
  160. <br>
  161. ${msjOk}
  162. ${msjNo}
  163. </div>
  164. <div class="col-sm-3">
  165. <div style="text-align: center;">
  166. <input type="button" onclick="history.back()" name="volver atrás" value="volver atrás">
  167. </div>
  168. </div>
  169. </div>
  170. </div>
  171. </body>
  172. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement