doPost
正在返回游戏时间超过5分钟的游戏,即使我在HTML表单中输入最多5分钟作为参数也是如此。为什么?我没有线索。我已打印出Boolean
表达式中使用的所有参数,以查看它们是否包含正确的值,并且它们似乎是这样做的。这是印刷品的例子:
信息:拥有:false minPlayers:3 selectedPlayers:1 maxPlayers:6 selectedPlaytime:5 gameTime:90信息:拥有:true minPlayers:2 selectedPlayers:1 maxPlayers:-1 chosenPlaytime:5 gameTime:-1 Info:
拥有:false minPlayers:2 selectedPlayers:1 maxPlayers:6 selectedPlaytime:5 gameTime:90信息:拥有:false minPlayers:1 selectedPlayers:1 maxPlayers:1 selectedPlaytime:5 gameTime:50 Info:
拥有:false minPlayers:-1 selectedPlayers:1 maxPlayers:-1 selectedPlaytime:5 gameTime:-1信息:拥有:true minPlayers:2 selectedPlayers:1 maxPlayers:7 selectedPlaytime:5 gameTime:30
这是我的代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
String players = request.getParameter("players");
String playtime = request.getParameter("playtime");
int chosenPlayers = Integer.parseInt(players);
int chosenPlaytime = Integer.parseInt(playtime);
URL url = new URL("http://bgg-json.azurewebsites.net/collection/Dan%20Xxxxxxx"); //hidden by me now
URLConnection yc = url.openConnection();
try (BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()))) {
String inputLine = null;
String result = "{\"boardgames\":";
while ((inputLine = in.readLine()) != null) {
result = result + inputLine;
}
result = result + ", \"responseStatus\": 200}";
JSONTokener tokener = new JSONTokener(result);
JSONObject joRoot = (JSONObject) tokener.nextValue();
JSONArray jarr = joRoot.getJSONArray("boardgames");
int numberOfBoardgames = jarr.length();
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BGGCollectionServlet</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"BGGCollection.css\" />");
out.println("</head>");
out.println("<body>");
for (int i = 0; i < numberOfBoardgames; i++) {
JSONObject jObject = jarr.getJSONObject(i);
Boolean owned = jObject.getBoolean("owned");
int minPlayers = jObject.getInt("minPlayers");
int maxPlayers = jObject.getInt("maxPlayers");
int gameTime = jObject.getInt("playingTime");
int rating = jObject.getInt("rating");
System.out.println("owned: " + owned + " minPlayers :" + minPlayers + " chosenPlayers :" + chosenPlayers + " maxPlayers :" + maxPlayers + " chosenPlaytime :" + chosenPlaytime + " gameTime :" + gameTime);
if (owned && (minPlayers <= chosenPlayers && maxPlayers >= chosenPlayers) && chosenPlaytime <= gameTime) {
String thumbNail = jObject.getString("thumbnail");
String image = jObject.getString("image");
String gameName = jObject.getString("name");
if (rating >= 9.0) {
out.println("<div class=\"boardgame good\">");
}
else {
out.println("<div class=\"boardgame\">");
}
out.println("<h1 class=\"gameName\">" + gameName + "</h1>");
out.println("<p><a href=\" " + image + " \" target=\"_blank\"><img class=thumbnail src=" + thumbNail + "></a></p>");
out.println("<p class=\"gameDetails\">Speltid: " + gameTime + "</p>");
out.println("<br>");
if (minPlayers == maxPlayers) {
out.println("<p class=\"gameDetails\">" + minPlayers + " Spelare" + "</p>");
}
else {
out.println("<p class=\"gameDetails\">" + minPlayers + " - " + maxPlayers + " Spelare" + "</p>");
}
if (gameTime == -1) {
out.println("<p class=\"gameDetails\">Speltid: " + "?" + "</p>");
}
out.println("</div>");
}
}
out.println(" <div class=\"sokruta\"><h1>Sök Spel</h1><br><form method=\"post\">"
+ "<p>Antal spelare: <input type=\"number\" name=\"players\" min=\"1\" max=\"12\" \n"
+ "value=\"" + players + "\"></p>"
+ "<p>Maximal speltid: <input type=\"number\" name=\"playtime\" min=\"5\" \n"
+ "max=\"240\" value=\"" + playtime + "\"></p>"
+ "<p><input type=\"submit\" value=\"Sök spel\" name=\"button\"></p>\n"
+ "</form></div>");
out.println("</body>");
out.println("</html>");
}
} catch (Exception e) {
System.out.println("Exception");
}
}