Javascript 我们可以停止JSP的服务器端重定向吗
这可能是个愚蠢的问题,但请容忍我 在我的Javascript 我们可以停止JSP的服务器端重定向吗,javascript,java,jsp,Javascript,Java,Jsp,这可能是个愚蠢的问题,但请容忍我 在我的login.jsp中,我正在进行一个AJX调用,以使用AJAX与服务器通信 function validateLoginForm() { var url = "<%=(request.getContextPath())%>/Login.do"; var username = document.getElementById("username").value; var password = document.getEle
login.jsp
中,我正在进行一个AJX调用,以使用AJAX与服务器通信
function validateLoginForm() {
var url = "<%=(request.getContextPath())%>/Login.do";
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = { "username":username,
"password":password
};
var http = new XMLHttpRequest();
http.open("POST", url, false);
http.setRequestHeader("X-Requested-With","XMLHttpRequest");
http.setRequestHeader("Content-type", "application/json");
http.onreadystatechange = function() {
debugger;
if (this.readyState == 4 && this.status) {
debugger;
}
};
http.send(JSON.stringify(params));
}
因此,它再次重定向回同一登录页面
如果我看到我的AJAX响应,我将返回完整的HTML,并填充登录错误div,如下所示:
this.responseText
"
<html>
<head>
<title>Informatica Administrator</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="shortcut icon" href="/administrator/favicon.ico" type="image/x-icon" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<script type="text/javascript">
....
...
function validateLoginForm() {
var url = "/administrator/Login.do";
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = { "username":username,
"password":password
};
var http = new XMLHttpRequest();
http.open("POST", url, false);
http.setRequestHeader("X-Requested-With","XMLHttpRequest");
http.setRequestHeader("Content-type", "application/json");
http.onreadystatechange = function() {
debugger;
if (this.readyState == 4 && this.status) {
debugger;
}
};
http.send(JSON.stringify(params));
return true;
}
</script>
<body style='margin:0;padding:0;' id="login">
<div class="loginBody">
<form name="loginForm" method="POST" autocomplete="off" onsubmit="return validateLoginForm()">
<table class='bodyClass' border=0 cellspacing=0 cellpadding=0 width="100%">
<tr>
<td valign="top">
<table border=0 cellspacing=0 cellpadding=0 width="100%">
<tr class="loginForm">
<td>
<table cellspacing="2" cellpadding="3">
<tr>
<td> </td>
<td id="loginError">The login information is not valid.</td>
</tr>
你能给我看看你使用的servlet代码吗?@UllasHunka我放了一些Java端代码。。我对Java非常陌生,所以,如果我需要更改某些内容,请解释一下。您希望ajax响应中有什么内容?@RoshanaPitigala我肯定想要登录错误。但是,即使在登录错误的情况下它应该重定向回同一个登录页面,不知何故,该重定向也应该包含上一个AJAX调用的登录错误信息。作为初学者,我无法将两者联系起来。我认为您误解了AJAX的用途。如果您使用的是ajax,那么根本不需要重定向页面
this.responseText
"
<html>
<head>
<title>Informatica Administrator</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="shortcut icon" href="/administrator/favicon.ico" type="image/x-icon" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<script type="text/javascript">
....
...
function validateLoginForm() {
var url = "/administrator/Login.do";
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = { "username":username,
"password":password
};
var http = new XMLHttpRequest();
http.open("POST", url, false);
http.setRequestHeader("X-Requested-With","XMLHttpRequest");
http.setRequestHeader("Content-type", "application/json");
http.onreadystatechange = function() {
debugger;
if (this.readyState == 4 && this.status) {
debugger;
}
};
http.send(JSON.stringify(params));
return true;
}
</script>
<body style='margin:0;padding:0;' id="login">
<div class="loginBody">
<form name="loginForm" method="POST" autocomplete="off" onsubmit="return validateLoginForm()">
<table class='bodyClass' border=0 cellspacing=0 cellpadding=0 width="100%">
<tr>
<td valign="top">
<table border=0 cellspacing=0 cellpadding=0 width="100%">
<tr class="loginForm">
<td>
<table cellspacing="2" cellpadding="3">
<tr>
<td> </td>
<td id="loginError">The login information is not valid.</td>
</tr>
public class LoginCommand extends AbstractCommand {
// name of request parameters.
public static final String REQUEST_PARAM_USERNAME = "username";
public static final String REQUEST_PARAM_PASSWORD = "password";
public static final String LOGIN_PAGE = "/login.jsp";
public boolean execute(Context context) throws Exception {
ActionContext actionCtx = (ActionContext) context;
ServletActionContext servletCtx = (ServletActionContext) actionCtx;
HttpServletRequest request = servletCtx.getRequest();
HttpServletResponse response = servletCtx.getResponse();
String requestBody = request.getReader().lines().collect(Collectors.joining());
JSONObject jsonObj = new JSONObject(requestBody);
String username = (String) jsonObj.get(REQUEST_PARAM_USERNAME);
String password = (String) jsonObj.get(REQUEST_PARAM_PASSWORD);
....
....
....
else{// Trying to Login
try {
// Block to execute in case of a valid login creds
} catch(WebSSOAccountLockedException e) {
....
....
} catch (WebSSOException e) {
redirectURL = UserIdentifierFilter.getLoginErrorPage(request);
loginError = true;
} catch(Exception e) {
redirectURL = UserIdentifierFilter.getLoginErrorPage(request);
loginError = true;
}
}
if(loginError){
RequestDispatcher rd = request.getRequestDispatcher(
LOGIN_PAGE);
rd.forward(request, response);
}
return loginError;
}