Here is the code built it is very simple,has two JSP pages and a Servlet, here I use the standard DAO , we have the class User
representing our "user" object, the class UserDAO
that bridges the gap between the bank and the app, DbUtil
which creates the connection through the data inserted in the file db.proprerties
.
To be able to use it you must use within the lib directory of your web project the following files:
jtsl.jar
along with standart.jar
I used the Postgresql database, and so I used the file
postgresql-9.4-1202.jdbc4.jar
if you use Mysql or another database ,just change the file(connection driver) to suit your database.
The entire process works with session control and with the use of the Requestdispatcher class that is used within the application’s single Servlet UserController
.
First of all I would like to say that I only addressed the necessary to get your answer with the code , interesting things like error handling,To test the code suppose the user will type everything as expected!!
Here is the JSP page that inserts your link to delete users:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css"
href="css/estilo.css"rel="stylesheet" />
<title>Todos Usuários</title>
</head>
<body>
<table border=1>
<thead>
<tr>
<th>CPF</th>
<th>Nome</th>
<th>Sexo</th>
<th>Salário</th>
<th colspan=2>Ação</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td><c:out value="${user.CPF}" /></td>
<td><c:out value="${user.nome}" /></td>
<td><c:out value="${user.sexo}" /></td>
<td><c:out value="${user.salario}" /></td>
<td><a href="UserController?action=delete&CPF=<c:out value="${user.CPF}"/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<p><a href="UserController?action=insert">Adicionar novo Usuário</a></p>
</body>
</html>
You can download the complete project on GITHUB, to be able to analyze in more detail
you need to create a new Serrvlet or a method, it will receive the id of the person and delete. in the listing you need to pass the id in the link.
– rray
You can paste the code from the list?
– rray
@Heloísaalves has to be on an html page can’t be on a jsp page? With a jsp it would give to use jtsl and facilitate everything.
– Pena Pintada