There is a page with a form and a pair of buttons
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"/> <title>Site</title> <script> function signInAction(){ console.log("Sign In"); var xhr = new XMLHttpRequest(); xhr.open('POST', '/auth', true); xhr.send(); var log = document.getElementById('login').value; var pas = document.getElementById('password').value; console.log("LOGIN: " + log + " PASSWORD " + pas); var body = 'login=' + encodeURIComponent(log) + '&password=' + encodeURIComponent(pas); xhr.open("POST", '/auth', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(body); if (xhr.status != 200) { alert( xhr.status + ': ' + xhr.statusText ); } else { console.log( xhr.responseText ); } } function signUpAction(){ console.log("Sign Up"); } </script> </head> <body> <p>Login Please</p> <form action="" method="POST"> Login: <input id="login" type="text" name="login"/> Password: <input id="password" type="password" name="password"/> </form> <button name="SignIn" onclick="signInAction()">Sign In</button> <button name="SignUp" onclick="signUpAction()">Sign Up </button> </body> </html> and there is a servlet that should process the request when you click on the Sign In button.
public class SingInServlet extends HttpServlet { AccountService accountService; public SingInServlet(AccountService accountService){ this.accountService = accountService; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost: login: " + req.getParameter("login") + " password " + req.getParameter("password")); resp.getWriter().println("SIGNING IN"); resp.setStatus(HttpServletResponse.SC_OK); } @Override protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doDelete(req, resp); } private Map<String, Object> getRequestParametres(HttpServletRequest req){ Map<String, Object> map = new HashMap<>(); map.put("pathInfo", req.getPathInfo()); map.put("context", req.getContextPath()); map.put("method", req.getMethod()); map.put("session", req.getSession().getId()); map.put("URL", req.getRequestURI().toString()); return map; } } Main class:
public class Main { public static void main(String[] args) throws InterruptedException { // write your code here AccountService accountService = new AccountService(); Server server = new Server(8080); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.addServlet(new ServletHolder(new SingUpServlet(accountService)), "/reg"); context.addServlet(new ServletHolder(new SingInServlet(accountService)), "/auth"); ResourceHandler resource_handler = new ResourceHandler(); resource_handler.setResourceBase("public_page"); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[]{resource_handler, context}); server.setHandler(handlers); try { server.start(); System.out.println("Server started"); server.join(); } catch (Exception e) { e.printStackTrace(); } } } When you enter data into the fields and click on the Sign In button, the request goes to the server, but the answer does not come back to the client, as a result, an alert with the text 0: appears:.
Why did this happen and how can this be corrected?