![](/img/trans.png)
[英]Java Application running as web service communicating with Java EE app on Glassfish
[英]Java EE web application asynchronous login in Glassfish 3.1.1
我正在嘗試使用javascript和XMLHttpRequest實現對JEE6 webapp的異步登錄。 我應該能夠使用XMLHttpRequest對/ app / j_security_check進行異步調用,並以某種方式解析響應,以便向用戶顯示“登錄失敗”或“登錄成功”對話框。 我正在使用Glassfish 3.1.1。
我嘗試過的方法,但響應始終為null。 我有一個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安全性引起的。 不管我用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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.