最终编辑:我实际上使用的是log4j并且配置不佳导致它对每个LOGGER.debug()进行双重记录;
我无法弄清楚为什么我的servlet正在为我的前端发送的每个ajax调用接收2个请求。
我用Tomcat运行我的Servlet。
当我将自己与前端连接时,我的System.out.println()打印“Hello”两次。
我在Firefox中使用F12多次验证,我只发送一个参数action = register的ajax请求。
我的HttpServlet:
@WebServlet(name = "MyServlet", urlPatterns = { "/actions" })
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = -983728805534985870L;
public MyServlet() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET,POST");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
switch (request.getParameter("action")) {
case "call":
callAction(request, response);
break;
case "register":
System.out.println("Hello");
register(request, response);
break;
case "unregister":
unregister(request, response);
break;
}
}
private void callAction(HttpServletRequest request, HttpServletResponse response) {
/*
...
*/
}
private void register(HttpServletRequest request, HttpServletResponse response) {
String connectedUserNumber = request.getParameter("number");
String uniqueID = agentUCC.register(connectedUserNumber);
LOGGER.debug("Register request from " + request.getRemoteAddr() + " with phone number " + connectedUserNumber);
LOGGER.info("User with phone number [{}] has been connected with uniqueID [{}]", connectedUserNumber, uniqueID);
handleSuccess(response, uniqueID);
}
private void unregister(HttpServletRequest request, HttpServletResponse response) {
/*
...
*/
}
// utility method - avoid code redundancy
private void handleSuccess(HttpServletResponse response, String message) {
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("Access-Control-Allow-Origin", "*");
try {
response.getWriter().print(message);
} catch (IOException e) {
LOGGER.error("ERROR : {}", e.getMessage(), e);
}
}
}
我正在用一个简单的前端测试它:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<!-- This will be displayed first. When the user isn't logged yet. -->
<div id="connectionPage">
<h2>Connection</h2>
<br/>
<p>Your phone number :</p>
<input type="text" id="connectionNumber"/>
<br/><br/>
<input type="button" value="Connect" id="connect"/>
<br/>
<p id="connectionError"></p>
</div>
<!--
...
-->
<script src="resources/js/jquery-3.3.1.min.js"></script>
<script src="resources/js/script.js"></script>
</body>
</html>
和我的script.js:
$(function() {
var connectedUserNumber;
// session's ID
var uniqueID;
// Connection
$("#connect").click(function(){
connectedUserNumber = $("#connectionNumber").val();
register();
});
// Register to the back-end
function register(){
console.log("Register !");
$.ajax({
xhrFields: {
withCredentials: false
},
type: 'POST',
url: 'foo/actions',
data: 'action=register&number=' + connectedUserNumber,
datatype: 'text',
success: function(data) {
uniqueID = data;
doPoll();
},
error: function(jqXHR, textStatus, exception) {
/*
...
*/
}
});
}
// Send every 2 seconds an ajax's request to retrieve incoming call's notifications
function doPoll() {
$.ajax({
type: 'POST',
url: 'foo/actions',
data: 'action=getNotificationToPush&uniqueID=' + uniqueID,
datatype: 'json',
success: function(data) {
handleDoPollSuccess(data);
},
error: function(jqXHR, textStatus, exception) {
handleEventError(jqXHR, textStatus, exception);
}
}).always(function() {
// Waits 2 seconds before sending the next request
setTimeout(doPoll, 2000);
});
}
/*
...
*/
});
你们是否知道这可能来自哪里? 如果需要,我可以提供更多细节。
编辑:我在我的JS文件中按ctrl + f +“register()”,我调用此方法的唯一地方是第9行。无论如何,我会在Firefox调试控制台中看到第二个ajax调用。
编辑之二:两个请求都在相同的千分之一秒内收到。 (例如:两者均于14:40:30:859收到)