Here is the code on my JSP page:
<button data-ng-click="login()">Fetch data from server</button>
In the mainController.js file:
$scope.login = function() {
var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open('POST', "http://localhost:8080/WEB-war/FES", false);
xmlHttpReq.setRequestHeader('Content-Type', 'application/json');
xmlHttpReq.send(JSON.stringify($scope.strings));
if (xmlHttpReq.status === 200) {
alert(xmlHttpReq.responseText)
}
}
The FileEditServlet.java file contains:
@WebServlet("/FileEditServlet")
public class FileEditServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("inside do POST");
PrintWriter out = response.getWriter();
out.println("response");
response.getWriter().write("do something omg");
}
The web.xml file configuration:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>FileEditServlet</servlet-name>
<servlet-class>servlet.FileEditServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileEditServlet</servlet-name>
<url-pattern>/FES</url-pattern>
</servlet-mapping>
</web-app>
When clicking the button to fetch data from the server and invoking the login function in the mainContrller, the output is empty. The alert shows no response, and there are two mysterious words "test test" appearing in the console which cannot be located in the code.