There is a servlet:
@WebServlet("/") public class MainServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map columns = new HashMap(); columns.put("FirstName", request.getParameter("FirstName")); columns.put("SecondName", request.getParameter("SecondName")); columns.put("LastName", request.getParameter("LastName")); columns.put("Receiver", request.getParameter("Receiver")); columns.put("Theme", request.getParameter("Theme")); columns.put("Message", request.getParameter("Message")); boolean hasConnected; DBController db = new DBController(); hasConnected = db.isConnected(); if(hasConnected) { System.out.println("DB connected"); System.out.println(db.insert("mytable", columns)); } else System.out.println("Connection failed!"); doGet(request, response); } } and a JSP page with several input fields:
<!DOCTYPE html> <HTML> <HEAD> <TITLE>Π€ΠΎΡΠΌΠ° ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠΉ ΡΠ²ΡΠ·ΠΈ</TITLE> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script type="text/javascript"> function sendData() { $.ajax({ type: "POST", url: "/servlet/ShowParameters", data: $('#formId').serialize(), }).done(function (res) { alert("send by ajax"); }); } </script> <script type="text/javascript"> $(document).ready(function() { $(".button").click(function(){ ver = true; first_name = $("#FirstName").val(); second_name = $("#SecondName").val(); last_name = $("#LastName").val(); var p = /^[Π°-ΡΠ-Π―ΡΠa-zA-Z0-9 \-]{2,35}$/i; if(!p.test(first_name)) { message_a = "Π£ΠΊΠ°ΠΆΠΈΡΠ΅ Π€Π°ΠΌΠΈΠ»ΠΈΡ"; ver = false; } else if(!p.test(second_name)) { message_a = "Π£ΠΊΠ°ΠΆΠΈΡΠ΅ ΠΠΌΡ"; ver = false; } else if(!p.test(last_name)) { message_a = "Π£ΠΊΠ°ΠΆΠΈΡΠ΅ ΠΡΡΠ΅ΡΡΠ²ΠΎ"; ver = false; } if(ver) { sendData(); return true; } else { alert(message_a); return false; } }); }); </script> </HEAD> <BODY> <form ACTION = "/servlet/ShowParameters" method = "post" id = "formId"> Π€Π°ΠΌΠΈΠ»ΠΈΡ:<input TYPE="TEXT" id = "FirstName" NAME = "FirstName"> ΠΠΌΡ:<input TYPE="TEXT" id = "SecondName" NAME = "SecondName"> ΠΡΡΠ΅ΡΡΠ²ΠΎ:<input TYPE="TEXT" id = "LastName" NAME = "LastName"> <input type = "button" class = "button" value = "Π‘ΠΎΡ
ΡΠ°Π½ΠΈΡΡ"/> <div style = "font-family: 'Calibri Light'"> <br><hr> <table id = "myTable" width="100%"> <tr> <td>Π€Π°ΠΌΠΈΠ»ΠΈΡ</td> <td>ΠΠΌΡ</td> <td>ΠΡΡΠ΅ΡΡΠ²ΠΎ</td> </tr> </table> </div> </form>
When I press the " Save " button, I have a field validation check, and then the AJAX " sendData () " function works. The data is successfully saved to the PostgreSQL table.
I now want that after saving the next record in the table, some sort of select should occur and all the contents of the table should be uploaded to the same page below.
I read that in JQuery there is a load () method that can load not only the content of the page, but also the selected container on it. This is done as follows:
$("#area").load("something.html #content"); This code will find a container with id content on the something.html page, take its contents and load it into a container with id area .
I donβt know how. I think there is a table with id "myTable", but how to load data there?
Or is it not done at all?