0
I am learning Java Web and would like to know if they could help me in the following: I have a form that I insert data into the database, and below it a table in which I list what was entered into the database, but what happens is the following, I’m trying to insert a die into the database for this field, only I wanted the table row to update asynchronously. Could someone help?
This code is only sending a message as a response because I had tried other ways and as I messed up I decided to leave anyway for now.
Index.jsp:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script src="bootstrap/js/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function salvar() {
var descricao = $("input[name=descricao]").val();
$.post("TextoServlet", {descricao: descricao})
.done(function (data) {
descricao = $("input[name=descricao]").val('');
$("#mensagem").html(data);
});
return false;
}
</script>
<title>JSP</title>
</head>
<body>
<div class="container">
<h1>Texto</h1>
<form action="TextoServlet" method="post" onsubmit="return salvar()">
<div class="form-group">
<label for="nome">Descrição:</label>
<input id="nome" type="text" name="descricao">
<button class="btn btn-default" type="submit">
Salvar
</button>
</div>
</form>
<jsp:useBean id="dao" class="modelo.TextoDAO"></jsp:useBean>
<div id="mensagem">
Aqui está aparecendo a mensagem!
</div>
<table class="table table-bordered">
<tr>
<th>Codigo</th>
<th>Descricao</th>
<th>Alterar</th>
<th>Excluir</th>
</tr>
<tbody>
<c:forEach var="txt" items="${dao.listar}">
<tr>
<td>${txt.codigo}</td>
<td>${txt.descricao}</td>
<td><button>Alterar</button></td>
<td><button>Deletar</button></td>
</tr>
</c:forEach>
</tr>
</tbody>
</table>
</div>
</body>
</html>
And here is the Servlet:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.Texto;
import modelo.TextoDAO;
public class TextoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String acao = request.getParameter("acao");
String codigo = request.getParameter("codigo");
if (acao.equals("excluir")) {
TextoDAO texto = new TextoDAO();
int cod = Integer.parseInt(codigo);
try {
texto.excluir(cod);
} catch (Exception ex) {
out.print("Excluido");
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
//objeto acima para escrever na tela
String descricao = request.getParameter("descricao");
try {
if (!descricao.equals("") && descricao != null) {
Texto texto = new Texto();
texto.setDescricao(descricao);
TextoDAO dao = new TextoDAO();
dao.salvar(texto);
out.print("<p class='alert alert-info'>Salvo com Sucesso!</p>");
}
else {
out.print("<p class='alert alert-warning'>Preencha o campo corretamente!</p>");
}
} catch (Exception e) {
out.print("<p class='alert alert-danger'>Erro ao salvar Texto</p>");
}
}
}
Could enter in question the code you have already implemented?
– Don't Panic
I don’t know if I sent it right but it’s there
– Marcos Cordeiro