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;
}