Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Registro</title>
- </head>
- <body>
- <form method="post" action="ServletUsuario">
- <table>
- <tr> <td>Registro</td></tr>
- <tr>
- <td>Rut</td>
- <td>
- <input type="text" name="txtRut"
- required/>
- </td>
- </tr>
- <tr>
- <td>Clave</td>
- <td>
- <input type="password" name="txtClave"
- required/>
- </td>
- </tr>
- <tr>
- <td>Confirmar Clave</td>
- <td>
- <input type="password" name="txtConfirmarClave"
- required/>
- </td>
- </tr>
- <tr>
- <td>Nombre</td>
- <td>
- <input type="text" name="txtNombre"
- required/>
- </td>
- </tr>
- <tr>
- <td>Apellido Paterno</td>
- <td>
- <input type="text" name="txtApPaterno"
- required/>
- </td>
- </tr>
- <tr>
- <td>Apellido Materno</td>
- <td>
- <input type="text" name="txtApMaterno"
- required/>
- </td>
- </tr>
- <tr>
- <td>Direccion</td>
- <td>
- <input type="text" name="txtDireccion"
- required/>
- </td>
- </tr>
- <tr>
- <td>Numeración</td>
- <td>
- <input type="text" name="txtNumeracion"
- required/>
- </td>
- </tr>
- <tr>
- <td>Comuna</td>
- <td>
- <sql:setDataSource driver="com.mysql.jdbc.Driver"
- password="" user="root"
- url="jdbc:mysql://localhost/examen"
- var="bd"/>
- <sql:query var="registro" dataSource="${bd}">
- SELECT * FROM COMUNA;
- </sql:query>
- <select name="cbo_opcion">
- <option value="0">Seleccione</option>
- <c:forEach var="reg" items="${registro.rows}">
- <option value="${reg.id_comuna}">
- <c:out value="${reg.NOMBRE}"/>
- </option>
- </c:forEach>
- </select>
- </td>
- </tr>
- <tr>
- <td>Teléfono</td>
- <td>
- <input type="text" name="txtTelefono"
- required/>
- </td>
- </tr>
- <tr>
- <td>
- <input type="submit" value="Registrar" name="btn_accion">
- </td>
- </tr>
- </table>
- </form>
- <div>
- Mensaje: ${mensaje}
- </div>
- </body>
- </html>
- ////////////////////////////////////////////////////////////////
- DAO
- package controladores;
- import conexion.Cl_Conexion;
- import pojos.Cl_Cliente;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- public class DaoCliente {
- private Connection conexion;
- private String rut="";
- public DaoCliente() {
- conexion=new Cl_Conexion().getConexion();
- }
- public String getRut() {
- return rut;
- }
- //metodo validar
- public boolean validar(Cl_Cliente cliente){
- try {
- PreparedStatement pstm=conexion.prepareCall("SELECT * FROM cliente WHERE rut=? AND clave=?");
- pstm.setString(1, cliente.getRut());
- pstm.setInt(2, cliente.getClave());
- ResultSet rs=pstm.executeQuery();
- if (rs.next()) {
- rut=rs.getString("rut");
- return true;
- }
- return false;
- } catch (Exception e) {
- return false;
- }
- }
- public String recuperarNombre(Cl_Cliente c){
- try {
- PreparedStatement pstm=conexion.prepareCall("SELECT * FROM cliente WHERE rut=? AND clave=?");
- pstm.setString(1, c.getRut());
- pstm.setInt(2, c.getClave());
- ResultSet rs=pstm.executeQuery();
- if (rs.next()) {
- return rs.getString("nombre");
- }
- return null;
- } catch (Exception e) {
- return null;
- }
- }
- public boolean agregarCliente(Cl_Cliente nc) {
- try {
- String sql = "INSERT INTO CLIENTE VALUES(?,?,?,?,?,?,?,?,?)";
- PreparedStatement pstm = conexion.prepareCall(sql);
- pstm.setInt(1, nc.getClave());
- pstm.setString(2, nc.getRut());
- pstm.setString(3, nc.getNombre());
- pstm.setString(4, nc.getApellido_p());
- pstm.setString(5, nc.getApellido_m());
- pstm.setString(6, nc.getDireccion());
- pstm.setInt(7, nc.getNumeracion_d());
- pstm.setInt(8, nc.getId_comuna());
- pstm.setInt(9, nc.getTelefono());
- int cantidad = pstm.executeUpdate();
- return cantidad > 0;
- } catch (Exception e) {
- System.out.println("Error mnmnmnm" + e.getMessage());
- return false;
- }
- }
- }
- ///////////////////////////////////////////
- SERVLET
- package servlet;
- import controladores.DaoCliente;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import pojos.Cl_Cliente;
- /**
- *
- * @author bACS
- */
- @WebServlet(name = "ServletUsuario", urlPatterns = {"/ServletUsuario"})
- public class ServletUsuario extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //response.setContentType("text/html;charset=UTF-8");
- String btn = request.getParameter("btn_accion");
- if (btn.equals("Enviar")) {
- enviar(request, response);
- }
- if (btn.equals("Registrar")) {
- registrar(request, response);
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- private void enviar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- int clave = Integer.parseInt(request.getParameter("txtClave"));
- String rut = request.getParameter("txtRut");
- Cl_Cliente cli = new Cl_Cliente(clave, rut);
- DaoCliente dao = new DaoCliente();
- boolean resp = dao.validar(cli);
- String msj = (resp ? "Usuario existe" : "No existe");
- request.setAttribute("mensaje", msj);
- if (resp) {
- //se toma la sesion actual del sistema
- HttpSession se = request.getSession();
- String nombre_apellido = new DaoCliente().recuperarNombre(cli);
- se.setAttribute("nombreCompleto", "Bienvenido(a): " + nombre_apellido);
- request.getRequestDispatcher("menu.jsp").forward(request, response);
- } else {
- request.getRequestDispatcher("login.jsp").forward(request, response);
- }
- }
- private void registrar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- boolean rpta ;
- try {
- int clave = Integer.parseInt(request.getParameter("txtClave"));
- int clave2 = Integer.parseInt(request.getParameter("txtConfirmarClave"));
- //validamos la contraseña
- if (clave != clave2) {
- request.setAttribute("mensaje", "La clave debe coincidir");
- request.getRequestDispatcher("registro.jsp").forward(request, response);
- rpta = false;
- }
- //recibimos parametros del formulario
- String rut = request.getParameter("txtRut");
- String nombre = request.getParameter("txtNombre");
- String apellido_p = request.getParameter("txtApPaterno");
- String apellido_m = request.getParameter("txtApMaterno");
- String direccion = request.getParameter("txtDireccion");
- int numeracion_d = Integer.parseInt(request.getParameter("txtNumeracion"));
- int id_comuna = Integer.parseInt(request.getParameter("cbo_opcion"));
- int telefono = Integer.parseInt(request.getParameter("txtTelefono"));
- //creamos un nuevo cliente
- Cl_Cliente nc = new Cl_Cliente(clave, numeracion_d, id_comuna, telefono, rut, nombre, apellido_p, apellido_m, direccion);
- DaoCliente dao = new DaoCliente();
- //lo agregamos con el metodo que está en el dao
- rpta = dao.agregarCliente(nc);
- String mensaje = "";
- mensaje = (rpta ? "Cliente agregado" : "Cliente no agregado");
- request.setAttribute("mensaje", ".." + mensaje);
- } catch (Exception e) {
- request.setAttribute("mensaje", "--" + e.getMessage());
- } finally {
- request.getRequestDispatcher("registro.jsp").forward(request, response);
- }
- }
- }
- //////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement