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;
}
How to pass information from a JSP page to a Servlet
– rray
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
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.
– Vinicius