Glassfish 3.1.1中的Java EE web应用程序异步登录

Java EE web application asynchronous login in Glassfish 3.1.1

本文关键字:应用程序 异步 web 登录 Java 中的 Glassfish EE      更新时间:2023-09-26

我正在尝试使用javascript和XMLHttpRequest实现对JEE6 Web应用程序的异步登录。我应该能够使用XMLHttpRequest对/app/j_security_check进行异步调用,并以某种方式解析响应,这样我就可以向用户显示一个带有"登录失败"或"登录成功"的对话框。我使用的是Glassfish 3.1.1。

我尝试过一些东西,但响应总是无效。我有一个login.jsp,其中包含登录表单和以下脚本:

function submitLogin(formName) {
    var urlAction = "/app/j_security_check";
    var client;
    var dataString;
    if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari
        client = new XMLHttpRequest();
    } else { // IE6, IE5
        client = new ActiveXObject("Microsoft.XMLHTTP");
    }
    client.onreadystatechange = function() {
        var response = client.responseText; // this is always null
                    /* ALERT THE DIALOG HERE ACCORDING TO RESPONSE? */
    };
    var form = document.forms[formName];
    var username = form.elements["j_username"].value;
    var password = form.elements["j_password"].value;
    dataString = "j_username=" + username + "&j_password=" + password;
    client.open("POST", urlAction, true);
    client.setRequestHeader("Content-type",
            "application/x-www-form-urlencoded");
    client.send(dataString);
}

所以我的问题是,这可能吗?应该如何实施?

编辑:这里的问题似乎是由Java Security在成功/失败登录后强制执行的重定向引起的。不管我用javascript做什么,它似乎总是重定向页面。我还尝试了jQuery的ajax方法,但没有成功。

我做了一些类似的事情,也许这会对你有所帮助:

        //Get a XMLHttpRequest object. 
        //The XMLHttpRequest specification defines an API that provides 
        //scripted client functionality for transferring data between a client and a server. 
        function getXMLObject()  //XML OBJECT
        {
           var xmlHttp = false;
           try {
             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
           }
           catch (e) {
             try {
               xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
             }
             catch (e2) {
               xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
             }
           }
           if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
             xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
           }
           return xmlHttp;  // Mandatory Statement returning the ajax object created
        }
        var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax obj
        /*
         * Use this method to send data to server using ajax.
         * Sent attribute name is : attribute
         * Sent attribute value is : attribute:val
         */
        function ajaxFunction(attribute,val, url) {
          if(xmlhttp) {
            var param = attribute+"="+attribute+":"+val;
            param +="&tiers_payant="+document.getElementsByName("tiers_payant")[0].value;  //Add the value to send here
            xmlhttp.open("POST",url,true);
            xmlhttp.onreadystatechange  = handleServerResponse;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttp.send(param);
          }
        }
        /**
         * When client received response from server,
         * set the inner HTML of the page with the one returned by server.
         */
        function handleServerResponse() {
           if (xmlhttp.readyState == 4) {
             if(xmlhttp.status == 200) {
                // DO what you want with : xmlhttp.responseText;
             }
             else {
                 document.getElementById("erreur").innerHTML="Le serveur le répond pas.";
                //alert("Error during AJAX call. Please try again"+xmlhttp.status);
             }
           }
        }