Ciao a tutti, io ho riscontrato un problema cercando di fare il login: quando invio il form al server, la servlet che controlla le credenziali (CheckLogin) mi restituisce l'errore secondo il quale le credenziali inserite sono nulle, e non capisco perchè. Ho provato a debuggare da Chrome e riesco a vedere i valori del form.
La servlet CheckLogin:
package it.tiw.controllers;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringEscapeUtils;
import it.tiw.beans.User;
import it.tiw.dao.UserDAO;
import it.tiw.utils.ConnectionHandler;
@WebServlet("/CheckLogin")
public class CheckLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection connection = null;
public CheckLogin() {
super();
}
public void init() throws ServletException {
connection = ConnectionHandler.getConnection(getServletContext());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = null;
String password = null;
username = StringEscapeUtils.escapeJava(request.getParameter("username"));
password = StringEscapeUtils.escapeJava(request.getParameter("pwd"));
if(username == null || password == null || username.isEmpty() || password.isEmpty()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().println("Credentials cannot be null");
return;
}
UserDAO userDao = new UserDAO(connection);
User user = null;
try {
user = userDao.checkCredentials(username, password);
} catch(SQLException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().println("Not possible to check credentials at the moment, try later");
return;
}
if (user == null) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().println("Incorrect credentials");
}
else {
request.getSession().setAttribute("user", user);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().println(username);
}
}
public void destroy() {
try {
ConnectionHandler.closeConnection(connection);
} catch(SQLException e) {
e.printStackTrace();
}
}
}
script login (loginManag.js):
(function() {
document.getElementById("loginButton").addEventListener('click', (e) => {
var form = new FormData(e.target.closest('form'));
if(form.checkValidity()) {
makeCall("POST", 'CheckLogin', e.target.closest('form'),
function(req) {
if(req.readyState == XMLHttpRequest.DONE) {
var message = req.responseText;
switch(req.status) {
case 200: //ok request
sessionStorage.setItem("username", message);
window.location.href = "Home.html";
break;
case 400: //bad request
document.getElementById("errorMessageLogin").textContent = message;
break;
case 401: //unauthorized
document.getElementById("errorMessageLogin").textContent = message;
break;
case 500: //server error
document.getElementById("errorMessageLogin").textContent = message;
break;
}
}
});
} else {
form.reportValidity();
}
});
})();
script che fa chiamate al server (utils.js):
function makeCall(method, url, formElement, cback, reset = true) {
var req = new XMLHttpRequest(); // visible by closure
req.onreadystatechange = function() {
cback(req)
}; // closure
req.open(method, url);
if (formElement == null) {
req.send();
} else {
req.send(new FormData(formElement));
}
if (formElement !== null && reset === true) {
formElement.reset();
}
}
file html con form:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
<script src="utils.js" charset="utf-8" defer></script>
<script src="loginManag.js" charset="utf-8" defer></script>
</head>
<body>
<h1>Welcome to the Money Transfer Application!!!!</h1>
<form action="#">
<fieldset>
USERNAME: <input type="text" name="username" required> <br>
PASSWORD: <input type="password" name="pwd" required><br>
<input id="loginButton" type="button" value="LOGIN">
<p id="errorMessageLogin"></p>
</fieldset>
</form>
</body>
</html>