简体   繁体   English

使用Java Servlet从html表单加载数据,更新数据库并在表的同一页上显示数据

[英]Load data from html form, update database and display data on the same page in table using java servlet

I need some help. 我需要协助。 I have a small application which consists of Java Servlet and html form. 我有一个包含Java Servlet和html表单的小应用程序。 User puts some data in a html form and after clicking on submit button the java servlet gets the data in it's post method (loads it in a database). 用户以html形式放置一些数据,然后单击“提交”按钮后,java servlet将数据放入其post方法中(将其加载到数据库中)。 Till now all works fine. 到现在一切正常。 I am using java servlet and tomcat. 我正在使用Java servlet和tomcat。 What I want to do now is to display the data in a table on the same page on html. 我现在想做的是在html的同一页上的表中显示数据。 I have found, that this would be possible through ajax get method. 我发现,这可以通过ajax get方法实现。 But somehow I can't get it right. 但是不知何故我做对了。 Here simplified code of my application: Java Servlet: 这里是我的应用程序的简化代码:Java Servlet:

@WebServlet(name = "MyServlet", urlPatterns = { "/MyServlet" })
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse    response) throws ServletException, IOException {
response.setContentType("text/html");
// getting the values submitted by the user from the html form
String name = request.getParameter("name");
String surname = request.getParameter("surname");
// here goes some logic to load data in the database
// data from database is recieved as an array and is then converted to 
// json 
// here I can only print the whole json retrieved from DB 
// and it is displayed on another page with myurl/MyServlet
 response.setContentType("application/json");
 PrintWriter out = response.getWriter();
 response.setCharacterEncoding("UTF-8");
 out.println(json);

}
public void doGet(HttpServletRequest request, HttpServletResponse   response) throws IOException{
// nothing happens here
}

In my html form I have the following (the most important things here only): 在我的html表单中,我有以下内容(仅在这里最重要):

<form action="MyServlet" method="post">
<input class="form-control" type="text" name="name" id="name">
<input class="form-control" type="text" name="surname" id="surname">
<input type="submit" class="btn btn-info" id="MyS"value="MyServlet">
<table class="table" id="table">
<tr>
    <th>name</th>
    <th>surname</th>
</tr>

The user input is stored in a database and everything is working fine. 用户输入存储在数据库中,一切正常。 The problem is, that I get no response and I would like to display all of the data given from user in a table, which schould be updated after each submit and user schould stay on the same page (don't actually know, how to do this). 问题是,我没有响应,我想将用户提供的所有数据显示在一个表中,应该在每次提交后将其更新,并将用户保持在同一页面上(实际上不知道如何处理)。做这个)。 From the database I get an Array which I can convert to JSON and all this information schould be displayed in a table. 从数据库中,我得到一个可以转换为JSON的数组,所有这些信息都应显示在表中。 I tried to write some ajax.get like this (I am a real newby in javascript): 我试图写一些像这样的ajax.get(我是JavaScript中的真正的新人):

<script>
<script type="text/javascript">
$("#MyS").click(function() {

}
$.ajax({
    url: "/MyServlet",
    type: "POST",
    dataType: '{json: json}',
    data: myvalue,

    success: function(data) {  
        $('#table').append(data);
    },
});
</script>

But no data is displayed in the table or somewhere. 但是,表或某处没有数据显示。 I would be glad to get some hints i what I am doing wrong and how should it be done properly? 我很高兴得到一些提示,我做错了什么以及应该如何正确完成? Thanks in advance. 提前致谢。

Execute it with these lines and tell me what response you get in your browser's console: 使用以下行执行它,并告诉我您在浏览器控制台中得到的响应:

$.ajax({
    url: "/MyServlet",
    type: "POST",
    dataType: '{json: json}',
    data: myvalue,
    success: function(data) 
    {  
        console.log( data ); // logs this on success
        $('#table').append(data);
    },
    **error: function (textStatus, errorThrown) 
    {
        console.log( textStatus ); // log this on error
        console.log( errorThrown );
    }**
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM