Este es el código:
Lista.jsp
Código:
<%@page import="java.util.Iterator"%>
<%@page import="Modelo.Persona"%>
<%@page import="java.util.List"%>
<%@page import="ModeloDAO.PersonaDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/estilos.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Listar</title>
</head>
<body>
<header class="header">
<div class="container">
<!-- Navbar-->
<nav class="row justify-content-between align-itms-center text-uppercase pt-2" style="background-color: whitesmoke; border-radius: 14px">
<!-- Logo-->
<a href="Principal.jsp" class="col-auto">
<img src="img/zorzalG.png" alt="Logo Zorzal" class="img-logo"/>
<span class="textologo" style="color: black">El zorzal</span>
</a>
<div class="dropdown">
<a style="color: black;font-family: Century Gothic;" href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">Cerrar Sesion</a>
<div class="dropdown-menu text-center"style="width:250px;margin-right: 65px; background-color: whitesmoke;">
<a><img src="img/fotoalex.jpg" hight="80" width="80"</a><br>
<a style="text-transform: capitalize;">${nom}</a>
<a style="text-transform: lowercase;">73707044@continental.edu.pe</a>
<div class="dropdown-divider"></div>
<a href="Controlador2?accion=Salir" class="dropdown-item"style="text-transform: capitalize;">Salir</a>
</div>
</div>
</nav>
</div>
</header>
<div class="container">
<h1 style="margin-top: 30px; font-family: Century Gothic; color: white;">Personas</h1>
<a class="btn btn-success" href="Controlador?accion=add">Agregar Nuevo</a>
<br>
<br>
<table style="background-color: whitesmoke;font-family: Century Gothic; border-collapse: collapse; border-radius: 5px 5px 0 0; overflow: hidden; box-shadow: 0 0 20px rgba(0,0,0,0.15);" class="table table-hover table-bordered">
<thead style="background-color:#A76726;color: whitesmoke;">
<tr>
<th class="text-center">DNI</th>
<th class="text-center">NOMBRES</th>
<th class="text-center">CELULAR</th>
<th class="text-center">HABITACION</th>
<th class="text-center">FECHA</th>
<th class="text-center">PAGO</th>
<th class="text-center">ACCIONES</th>
</tr>
</thead>
<%
PersonaDAO dao=new PersonaDAO();
List<Persona>list=dao.listar();
Iterator<Persona>iter=list.iterator();
Persona per=null;
while(iter.hasNext()){
per=iter.next();
%>
<tbody>
<tr>
<td class="text-center"><%= per.getDni()%></td>
<td><%= per.getNom()%></td>
<td><%= per.getCel()%></td>
<td><%= per.getHab()%></td>
<td><%= per.getFec()%></td>
<td><%= per.getPag()%></td>
<td class="text-center">
<a class="btn" style="background-color: #A76726; color: White;" href="Controlador?accion=editar&id=<%= per.getId()%>">Editar</a>
<a class="btn btn-danger" href="Controlador?accion=eliminar&id=<%= per.getId()%>">Remove</a>
</td>
</tr>
<%}%>
</tbody>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
add.jsp
Código:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href="css/estilos.css" rel="stylesheet" type="text/css"/>
<title>JSP Page</title>
</head>
<body>
<header class="header">
<div class="container">
<!-- Navbar-->
<nav class="row justify-content-between align-itms-center text-uppercase pt-2" style="background-color: whitesmoke; border-radius: 14px">
<!-- Logo-->
<a href="Principal.jsp" class="col-auto">
<img src="img/zorzalG.png" alt="Logo Zorzal" class="img-logo"/>
<span class="textologo" style="color: black">El zorzal</span>
</a>
<div class="dropdown">
<a style="color: black;font-family: Century Gothic;" href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">Cerrar Sesion</a>
<div class="dropdown-menu text-center"style="width:250px;margin-right: 65px; background-color: whitesmoke;">
<a><img src="img/fotoalex.jpg" hight="80" width="80"</a><br>
<a style="text-transform: capitalize;">${nom}</a>
<a style="text-transform: lowercase;">73707044@continental.edu.pe</a>
<div class="dropdown-divider"></div>
<a href="Controlador2?accion=Salir" class="dropdown-item"style="text-transform: capitalize;">Salir</a>
</div>
</div>
</nav>
</div>
</header>
<div class="container">
<div class="col-pt-4" style="float: left;">
<h1 style="margin-top: 30px; font-family: Century Gothic; color: white; ">Agregar Inquilino</h1>
<form action="Controlador" style="font-family: Century Gothic; color: white;">
DNI: <br>
<input class="form-control" type="text" name="txtDni"><br>
Nombres: <br>
<input class="form-control" class="form-control" type="text" name="txtNom"><br>
Celular: <br>
<input class="form-control" class="form-control" type="text" name="txtCel"><br>
Habitacion: <br>
<input class="form-control" class="form-control" type="text" name="txtHab"><br>
Fecha de Ingreso: <br>
<input class="form-control" class="form-control" type="text" name="txtFec"><br>
Renta Mensual: <br>
<input class="form-control" class="form-control" type="text" name="txtPag"><br>
<input class="btn" type="submit" name="accion" value="Agregar" style="background-color: gray; color: whitesmoke;"><br>
<a style="color: whitesmoke;" href="Controlador?accion=listar">Regresar</a>
</form>
</div>
</div>
</body>
</html>
Controlador.java
Código:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Controlador;
import Modelo.Persona;
import ModeloDAO.PersonaDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Controlador extends HttpServlet {
String listar="vistas/listar.jsp";
String add="vistas/add.jsp";
String edit="vistas/edit.jsp";
Persona p=new Persona();
PersonaDAO dao=new PersonaDAO();
int id;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Controlador</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Controlador at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String acceso="";
String action=request.getParameter("accion");
if(action.equalsIgnoreCase("listar")){
acceso=listar;
}else if (action.equalsIgnoreCase("add")){
acceso=add;
}
else if(action.equalsIgnoreCase("Agregar")){
String dni=request.getParameter("txtDni");
String nom=request.getParameter("txtNom");
String cel=request.getParameter("txtCel");
String hab=request.getParameter("txtHab");
String fec=request.getParameter("txtFec");
String pag=request.getParameter("txtPag");
p.setDni(dni);
p.setNom(nom);
p.setCel(cel);
p.setHab(hab);
p.setFec(fec);
p.setPag(pag);
dao.add(p);
acceso=listar;
}
else if(action.equalsIgnoreCase("editar")){
request.setAttribute("idper",request.getParameter("id"));
acceso=edit;
}
else if(action.equalsIgnoreCase("Actualizar")){
id=Integer.parseInt(request.getParameter("txtid"));
String dni=request.getParameter("txtDni");
String nom=request.getParameter("txtNom");
String cel=request.getParameter("txtCel");
String hab=request.getParameter("txtHab");
String fec=request.getParameter("txtFec");
String pag=request.getParameter("txtPag");
p.setId(id);
p.setDni(dni);
p.setNom(nom);
p.setCel(cel);
p.setHab(hab);
p.setFec(fec);
p.setPag(pag);
dao.edit(p);
acceso=listar;
}
else if(action.equalsIgnoreCase("eliminar")){
id=Integer.parseInt(request.getParameter("id"));
p.setId(id);
dao.eliminar(id);
acceso=listar;
}
RequestDispatcher vista=request.getRequestDispatcher(acceso);
vista.forward(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>
}
Alguna idea?
Muchas gracias.