Send information from one JSP to another via Servlet

Asked

Viewed 572 times

0

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

I would like to send the project information from the list page to the edit page via Servlet.

When sending this way, in Servlet the Project is null, I believe due to the new instance of the Project in 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;
}
  • If I understand correctly, you don’t send the id of the project in place never to the Servlet so it returns null. You need to take the value that comes from the request and pass to the DAO to retrieve all project information.

  • rray: I’m not really sending anything to Servlet, because the fields are label. If it was a textbox I could name it "id" and so in Servlet take the id typed by the user. But the intention is not the user to enter the id.

1 answer

1


In Servlet you must take the ID parameter that you are not sending as follows:

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

To submit place a link put in any column of your table, informing the respective ID:

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

Exchange the id for your respective line ID, as you did above:

<%=p.getID()%>

I believe this will solve your problem.

  • Renato: That’s how it worked. <td><a href="/WebService/ControleProjeto?id=<%=p.getID()%>&acao=Editar" class="btn btn-danger btn-xs">Nome do Projeto</a></td>

Browser other questions tagged

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