Send information from one JSP to another via servlet

0

I have a page to list all projects. Each project shown will appear the edit button.

I would like to submit the project information of the page list to the edit page via Servlet.

When I send this form, in the Servlet the Project is null, I believe due to the new Project instance in the Servlet.

list projects:

<div id="list" class="row">
<div class="table-responsive col-md-12">
    <form method="POST" action="ControleProjeto"/>
        <table class="table table-striped" cellspacing="0" cellpadding="0">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nome do projeto</th>
                    <th>Nome da empresa</th>
                    <th>Nome do Responsável</th>
                    <th class="actions">Ações</th>
                 </tr>
            </thead>
            <%
                for(Projetos p: listaprojetos){
            %>
            <tbody>
                <tr>
                    <td><%=p.getID()%></td>
                    <td><%=p.getNome()%></td>
                    <td><%=p.getEmpresa()%></td>
                    <td><%=p.getResponsavel()%></td>
                    <td class="actions">
                        <input type="submit" name="acao" value="Editar" class="btn btn-danger btn-xs" />
                    </td>
                </tr>
            </tbody>
            <%}%>
        </table>
    </form>
</div>
</div>

Servlet:

Projetos p = new Projetos();

if("Editar".equals(acao)){
if(p.getID() != 0){
    try{
        int id = p.getID();
        Projetos pj = new ProjetoDAO().get(id);

        RequestDispatcher rd = request.getRequestDispatcher("editarProjeto.jsp");
        request.setAttribute("projeto", pj);
        rd.forward(request, response);
        return ;
    }catch(Exception e){

    }
}
}

DAO:

public Projetos get(int id) throws ServletException {
    Projetos p = new Projetos();
    try{
        sql = "SELECT * FROM projeto WHERE idProjeto = ?;";
        con = Connect.conectar();
        ps = con.prepareStatement(sql);

        ps.setInt(1, id);

        rs = ps.executeQuery();

        if(rs.next()){
            p.setID(rs.getInt("idProjeto"));
            p.setEmpresa(rs.getString("empresa"));
            p.setNome(rs.getString("nome"));
            p.setResponsavel(rs.getString("responsavel"));
        }
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(ProjetoDAO.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }finally {             
        try {
            Connect.fechar();
        } catch (Exception sqlex) {
        }
    }
    return p;
}
    
asked by anonymous 19.01.2017 / 18:02

1 answer

1

In the Servlet you should get the ID parameter that you are not sending as follows:

String id = request.getParameter("id");

To send put a link put in any column of your table, informing its ID:

<td><a href="/ControleProjeto?id=0">Nome do Projeto</a></td>

Change the id by its respective Id of the line, as above:

<%=p.getID()%>

I believe this will solve your problem.

    
20.01.2017 / 03:13