How to connect a link to an object? Servlet

Asked

Viewed 910 times

-1

It was made a solution proposal in Servlet, I have a form elaborated in html, save the data in a database, take this data and dispose in an html table, in this table has the attributes of the person object, one of the values of these columns is a link, when the user clicks on this link I have to delete the person corresponding to that line, the question is: how do I connect a person with a link? Here is a picture of the table. Delete is a link to delete the person, but is a link that does not direct to any place, merely illustrative, to help in abstraction... inserir a descrição da imagem aqui

  • 1

    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.

  • 1

    You can paste the code from the list?

  • @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.

1 answer

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.