Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void agregar(HttpServletRequest request, HttpServletResponse response) throws IOException {
- try {
- request.getSession().removeAttribute("msjNo");
- request.getSession().removeAttribute("msjOk");
- request.getSession().removeAttribute("mensaje");
- String rut = request.getParameter("txtRut");
- String dv = request.getParameter("txtDv");
- String user = request.getParameter("txtUser");
- String pass = request.getParameter("txtPass");
- String nombre = request.getParameter("txtNombre");
- String apellido = request.getParameter("txtApellido");
- //GUARDAR FECHA
- String fechaNacimiento = request.getParameter("dtpFechaNacimiento");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat anio = new SimpleDateFormat("yyyy");
- Date fecha = sdf.parse(fechaNacimiento);
- DateFormat outputFormat = new SimpleDateFormat("yyyy");
- DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date date = inputFormat.parse(fechaNacimiento);
- String outputText = outputFormat.format(date);
- int anioNacimiento = Integer.parseInt(outputText);
- Calendar fechaActual = Calendar.getInstance();
- int anioActual = fechaActual.get(Calendar.YEAR);
- int edad = anioActual - anioNacimiento;
- String direccion = request.getParameter("txtDireccion");
- int telefono = Integer.parseInt(request.getParameter("txtTelefono"));
- //VALIDAR RUT
- boolean validacion = false;
- rut = rut.toUpperCase();
- rut = rut.replace(".", "");
- rut = rut.replace("-", "");
- int rutAux = Integer.parseInt(rut.substring(0, rut.length()));
- char dvVerificar = dv.charAt(0);
- int m = 0, s = 1;
- for (; rutAux != 0; rutAux /= 10) {
- s = (s + rutAux % 10 * (9 - m++ % 6)) % 11;
- }
- if (dvVerificar == (char) (s != 0 ? s + 47 : 75)) {
- validacion = true;
- }
- //FIN VALIDAR RUT
- TipoUsuario tipo = new TipoUsuario(3);
- Persona per = new Persona(rut, dv, nombre, apellido, fecha, edad, direccion, telefono, true);
- PersonaDao daoPer = (PersonaDao) request.getSession().getAttribute("personas");
- Usuario usuario = new Usuario(per, tipo, user, pass, true);
- UsuarioDao daoUser = (UsuarioDao) request.getSession().getAttribute("usuarios");
- if (daoPer == null) {
- daoPer = new PersonaDao();
- }
- if (daoUser == null) {
- daoUser = new UsuarioDao();
- }
- if (daoPer.agregar(per) && daoUser.agregar(usuario) && validacion) {
- request.getSession().setAttribute("msjOk", "Bienvenido " + nombre + "!");
- } else {
- request.getSession().setAttribute("msjNo", "Cliente No Agregado!");
- }
- request.getSession().setAttribute("personas", daoPer);
- request.getSession().setAttribute("usuarios", daoUser);
- } catch (Exception e) {
- request.getSession().setAttribute("msjNo", "Datos incorrectos (" + e.getMessage() + ")");
- } finally {
- response.sendRedirect("agregarCliente.jsp");
- }
- }
- --------------------------------------------------
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <%--
- Document : agregar
- Created on : 28-09-2018, 12:41:28
- Author : Claudio
- --%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Agregar Cliente</title>
- <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
- <script src="js/jquery-3.2.0.min.js" type="text/javascript"></script>
- <script src="js/bootstrap.min.js" type="text/javascript"></script>
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="col-sm-12">
- </div>
- </div>
- <div class="row">
- <div class="col-sm-3">
- </div>
- <div class="col-sm-6">
- <form action="procesoCliente" method="POST">
- <h1>Registro de cliente:</h1><br>
- <div class="form-group">
- <label>Rut (SIN DIGITO VERIFICADOR):</label>
- <input type="text" name="txtRut" value="" placeholder="Ej: 12345678" class="form-control" required="" />
- </div>
- <div class="form-group">
- <label>Digito Verificador:</label>
- <input type="text" name="txtDv" value="" placeholder="Ej: 1" class="form-control" required="" />
- </div>
- <div class="form-group">
- <label>Nombre de usuario:</label>
- <input type="text" name="txtUser" value="" placeholder="Ingrese nombre de usuario" class="form-control" required="" />
- </div>
- <div class="form-group">
- <label>Contraseña:</label>
- <input type="password" name="txtPass" value="" placeholder="Ingrese contraseña" class="form-control" required="" />
- </div>
- <div class="form-group">
- <label>Nombre:</label>
- <input type="text" name="txtNombre" value="" placeholder="Ingrese su nombre" class="form-control" required="" />
- </div>
- <div class="form-group">
- <label>Apellido:</label>
- <input type="text" name="txtApellido" value="" placeholder="Ingrese su apellido" class="form-control" required="" />
- </div>
- <div class="form-group">
- <label>Fecha Nacimiento:</label>
- <input type="date" name="dtpFechaNacimiento">
- </div>
- <div class="form-group">
- <label>Direccion:</label>
- <input type="text" name="txtDireccion" value="" placeholder="Ingrese su dirección" class="form-control" required="" />
- </div>
- <div class="form-group">
- <label>Telefono (SIN ANTEPONER EL (+569) PARA CELULAR O EL (2) PARA FIJOS):</label>
- <input type="text" name="txtTelefono" value="" placeholder="Ingrese su telefono" class="form-control" required="" />
- </div>
- <div class="form-group text-center">
- <input type="submit" name="btnAccion" value="Agregar" class="btn btn-primary" />
- <input type="reset" name="btnAccion" value="Limpiar" class="btn btn-danger" />
- </div>
- </form>
- <br>
- ${msjOk}
- ${msjNo}
- </div>
- <div class="col-sm-3">
- <div style="text-align: center;">
- <input type="button" onclick="history.back()" name="volver atrás" value="volver atrás">
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement