Reputation: 397
I've a web-app base on HTTP servlet. This is JS request to my servlet:
$.ajax({
type: "POST",
url: url,
xhrFields: {
withCredentials: false
},
data:{
"action" : action_type,
"fingerprint" : fingerprint,
"user_id" : user_id,
},
success: function(data) {
alert('sdp inviato!');
},
// vvv---- This is the new bit
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error, status = " + textStatus + ", " +
"error thrown: " + errorThrown
);
}
});
This is my servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action= request.getParameter("action");
UtenteModel um= new UtenteModel();
SDPLineModel sdpmodel= new SDPLineModel();
if (action.equals("CheckFingerprint")) {
String fingerprint = (String) request.getParameter("fingerprint");
String user_id = request.getParameter("user_id");
SDPLines sdpline = sdpmodel.findFingerprintNew(fingerprint, user_id);
if (sdpline == null) {
String message = "Fingerprint non valida!";
System.out.println("Fingerprint not found! ");
}
else {
System.out.println("Fingerprint found! ");
}
}
My question is: can anyone show me some example for modify my servlet and js code, setting a specific response data (for the two case, fingerprint found or not found) and how to check its value via JS code in success:function(data)?
Upvotes: 0
Views: 981
Reputation: 1358
if i understand very well your issues is that you want to show the response coming from your servlet to the client trough JS code !! you can try this :
if (sdpline == null) {
String message = "Fingerprint non valida!";
response.getOutputStream().print(message);
}else {
String message = ""Fingerprint found! ""
response.getOutputStream().print(message);
}
so now in your AJAX request you will get that message in case your request was successfully done .
},
success: function(data) {
alert('message coming from your servlet is '+data);
//here data will contain one of the messages depending on the if statement
},
Upvotes: 5