HTTP Servlet设置数据响应并通过Javascript检查

HTTP Servlet设置数据响应并通过Javascript检查,javascript,jquery,http,servlets,Javascript,Jquery,Http,Servlets,我有一个基于HTTP servlet的web应用程序。 这是对我的servlet的JS请求: $.ajax({ type: "POST", url: url, xhrFields: { withCredentials: false }, data:{ "a

我有一个基于HTTP servlet的web应用程序。 这是对我的servlet的JS请求:

        $.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
                    );
              }
            });
这是我的servlet代码:

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! ");
        }
    }

我的问题是:有人能给我举一些例子来修改我的servlet和js代码,设置一个特定的响应数据(对于两种情况,指纹找到或没有找到),以及如何在success中通过js代码检查其值:function(data)?

如果我非常了解您的问题,您希望通过JS代码向客户机显示来自servlet的响应!!您可以尝试以下方法:

if (sdpline == null) {
    String message = "Fingerprint non valida!";
    response.getOutputStream().print(message);
}else {
    String message = ""Fingerprint found! ""
    response.getOutputStream().print(message);
} 
所以现在在AJAX请求中,如果请求成功完成,您将得到该消息

},
 success: function(data) {
     alert('message coming from your servlet is '+data);
     //here data will contain one of the messages depending on the if statement 
},

如果我非常了解您的问题,那么您希望通过JS代码向客户机显示来自servlet的响应!!您可以尝试以下方法:

if (sdpline == null) {
    String message = "Fingerprint non valida!";
    response.getOutputStream().print(message);
}else {
    String message = ""Fingerprint found! ""
    response.getOutputStream().print(message);
} 
所以现在在AJAX请求中,如果请求成功完成,您将得到该消息

},
 success: function(data) {
     alert('message coming from your servlet is '+data);
     //here data will contain one of the messages depending on the if statement 
},

@pier92很高兴,因为它有帮助!!如果我的答案有帮助,请检查它是否正确,以帮助其他人获得答案。现在,当我尝试检查无效值时,我在浏览器网络控制台中看到500错误,并且已为此响应调用getOutputStream()。。我怎样才能解决这个问题?我正在尝试添加response.getOutputStream().flush();response.getOutputStream().close();响应后.getOutputStream().print(消息);在这两种情况下。。我说的对吗?@pier92在任何情况下,您都会得到
响应
,这样您就可以在值上做一个
if语句
,检查它是否有效,然后不要打印任何内容@pier92很高兴,因为它有帮助!!如果我的答案有帮助,请检查它是否正确,以帮助其他人获得答案。现在,当我尝试检查无效值时,我在浏览器网络控制台中看到500错误,并且已为此响应调用getOutputStream()。。我怎样才能解决这个问题?我正在尝试添加response.getOutputStream().flush();response.getOutputStream().close();响应后.getOutputStream().print(消息);在这两种情况下。。我说的对吗?@pier92在任何情况下,您都会得到
响应
,这样您就可以在值上做一个
if语句
,检查它是否有效,然后不要打印任何内容!!