I am trying to fill a table using a POST command to my servlet, but instead of sending a POST request, my webpage sends a GET request to the servlet. Here is my
$(document).ready(function() {
$.get("league", function(responseJson) {
if (responseJson != null) {
$("#tableleague").find("tr:gt(0)").remove();
var table1 = $("#tableleague");
$.each(responseJson, function(key, value) {
var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
rowNew.children().eq(0).text(value['teams']);
rowNew.children().eq(1).text(value['playedgames']);
rowNew.children().eq(2).text(value['wongames']);
rowNew.children().eq(3).text(value['tiegames']);
rowNew.children().eq(4).text(value['lostgames']);
rowNew.children().eq(5).text(value['scoredgoal']);
rowNew.children().eq(6).text(value['scoredgoal']);
rowNew.children().eq(7).text(value['scoredgoal']);
rowNew.appendTo(table1);
});
}
});
});
function addData() {
var t1 = $("#firstteam").val();
var t2 = $("#secondteam").val();
var s1 = $("#score1").val();
var s2 = $("#score2").val();
$("#firstteam").val('');
$("#secondteam").val('');
$("#score1").val('');
$("#score2").val('');
var data = {
firstteam: "t1",
secondteam: "t2",
score1: "s1",
score2: "s2"
}
$.ajax({
type: "POST",
url: "league",
data: JSON.stringify(data),
contentType: "application/x-www-form-urlencoded",
dataType:'json',
success: function(data, textStatus, jqXHR) {
if (data.success) {
$("#error").html("<div><b>Success!</b></div>" + data);
} else {
$("#error").html("<div><b>Information is Invalid!</b></div>" + data);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Something really bad happened " + textStatus);
$("#error").html(jqXHR.responseText);
}
});
}
<h1>League Table</h1>
<table cellspacing="0" id="tableleague" style="width:100%">
<tr>
<th scope="col">Teams</th>
<th scope="col">Won</th>
<th scope="col">Tie</th>
<th scope="col">Lost</th>
<th scope="col">Scored</th>
<th scope="col">Received</th>
<th scope="col">Points</th>
</tr>
</table>
<form method="POST">
<div class="form_group">
<label for="FirstTeam">FirstTeam:</label>
<input type="text" class="form_control" id="firstteam">
</div>
<div class="form_group">
<label for="Score1">Score1:</label>
<input type="text" class="form_control" id="score1">
</div>
<div class="form_group">
<label for="Score2">Score2:</label>
<input type="text" class="form_control" id="score2">
</div>
<div class="form_group">
<label for="SecondTeam">SecondTeam:</label>
<input type="text" class="form_control" id="secondteam">
</div>
<div class="form_group">
<button type="submit" class="addbutton" id="addbut" onclick="addData();">Add match</button>
</div>
</form>
<div id="error"></div>
Here is what I see in my debugger immediately after I press the Add match button:
The error I receive in the console is this one:
POST http://localhost:8080/league/league 500 (Internal Server Error)
What am I doing wrong with my code? I tried many solutions but nothing worked.
My servlet code:
@WebServlet("/league/*")
public class PopulateTable extends HttpServlet {
private static final long serialVersionUID = 1L;
public PopulateTable()
{
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
ArrayList<League> league = new ArrayList<League>();
league= Controller.getAllController();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(league, new TypeToken<List<League>>() {}.getType());
JsonArray jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
response.getWriter().print(jsonArray);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try {
PrintWriter out = response.getWriter();
JsonObject data = new Gson().fromJson(request.getReader(), JsonObject.class);
int s1 = Integer.parseInt(data.get("score1").getAsString());
int s2 = Integer.parseInt(data.get("score2").getAsString());
String t1 = data.get("firstteam").getAsString();;
String t2 = data.get("secondteam").getAsString();;
int ok = Controller.update(t1, t2, s1, s2);
JsonObject myObj = new JsonObject();
if(ok == 1)
{
myObj.addProperty("success", true);
}
else {
myObj.addProperty("success", false);
}
out.println(myObj.toString());
out.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}