Load data from html form, update database and display data on the same page in table using java servlet -
i need help. have small application consists of java servlet , html form. user puts data in html form , after clicking on submit button java servlet gets data in it's post method (loads in database). till works fine. using java servlet , tomcat. want display data in table on same page on html. have found, possible through ajax method. somehow can't right. here simplified code of application: 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 values submitted user html form string name = request.getparameter("name"); string surname = request.getparameter("surname"); // here goes logic load data in database // data database recieved array , converted // json // here can print whole json retrieved db // , displayed on page 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 html form have following (the important things here only):
<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 stored in database , working fine. problem is, no response , display of data given user in table, schould updated after each submit , user schould stay on same page (don't know, how this). database array can convert json , information schould displayed in table. tried write ajax.get (i real newby in 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 displayed in table or somewhere. glad hints doing wrong , how should done properly? in advance.
execute these lines , tell me response in browser's console:
$.ajax({ url: "/myservlet", type: "post", datatype: '{json: json}', data: myvalue, success: function(data) { console.log( data ); // logs on success $('#table').append(data); }, **error: function (textstatus, errorthrown) { console.log( textstatus ); // log on error console.log( errorthrown ); }** });
Comments
Post a Comment