简体   繁体   English

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

[英]Java EE web application asynchronous login in Glassfish 3.1.1

I'm trying to implement asynchronous login to JEE6 webapp using javascript and XMLHttpRequest. 我正在尝试使用javascript和XMLHttpRequest实现对JEE6 webapp的异步登录。 I should be able to make an asynchronous call with XMLHttpRequest to /app/j_security_check and parse the response somehow so that I can show the user a dialog with "Login Failed" or "Login success". 我应该能够使用XMLHttpRequest对/ app / j_security_check进行异步调用,并以某种方式解析响应,以便向用户显示“登录失败”或“登录成功”对话框。 I am using Glassfish 3.1.1. 我正在使用Glassfish 3.1.1。

Something I tried, but response is always null. 我尝试过的方法,但响应始终为null。 I have a login.jsp that holds the login form and the following script: 我有一个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);
}

So my question is, is this possible and how should implement it? 所以我的问题是,这可能吗,应该如何实施?

Edit: The problem here seems to arise from the redirect Java Security is enforcing after succesful/failed login. 编辑:这里的问题似乎是由成功/失败的登录后强制执行Java安全性引起的。 It seems to always redirect the page, no matter what I do with javascript. 不管我用JavaScript做什么,它似乎总是重定向页面。 I also tried jQuery's ajax methods with no avail. 我也尝试了jQuery的ajax方法,但无济于事。

I do something similar maybe this will help you : 我做类似的事情,也许这会帮助您:

        //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);
             }
           }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM