Glassfish 3.1.1中的Java EE web应用程序异步登录
Java EE web application asynchronous login in Glassfish 3.1.1
我正在尝试使用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);
}
}
}
相关文章:
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- Emberjs应用程序加载在除Index之外的所有路由上
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- Node.js应用程序中从模型层异步返回数据
- 异步加载AngularJS应用程序
- 在express应用程序中异步执行res.render
- 在应用程序框架中处理异步 Javascript 代码的最佳实践
- Angular 应用程序在异步调用 Firebase 后未更新(脏检查)
- 如何异步并行多个应用程序使用调用
- express.js(或类似)应用程序的异步初始化
- Glassfish 3.1.1中的Java EE web应用程序异步登录
- 如何在 node.js 中处理异步应用程序中的模块
- 为什么不在MVC应用程序中的所有操作上使用异步呢
- 如何为angularjs应用程序中的每个异步操作(使用$q)制作加载指示器
- 以异步方式向前端web应用程序添加设置文件
- 回调函数和异步应用程序
- node.js/express应用程序-使用哪个异步方法来替换我对HGET的嵌套调用
- 如何针对后台多个异步请求导致的延迟优化基于web的应用程序
- Silverlight应用程序在使用异步AJAX时不会变为空闲
- JS/HTML - 单页应用程序 - 取消异步操作