2013-10-28 20 views
0

用戶需要提供特定的密碼才能被授權訪問特定頁面。如何從java中的方法發送成功的消息到javascript?

我顯示一個表單,如果用戶輸入正確的密碼將被重定向到一個新的頁面,否則需要顯示一條消息,指出密碼錯誤。我怎樣才能實現它?

目前,我可以使用以下代碼顯示「密碼錯誤」消息,但如果密碼正確,如何重定向用戶。

如果我可以修改xmlhttp對象的返回碼,我可能會滿足要求。

function auth(){ 
    var form = $('#form').serialize(); 
    if(window.XMLHttpRequest) 
    { 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else 
    { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      document.getElementById("background").style.display = "Block"; 
      document.getElementById("box").style.display = "Block"; 
      document.getElementById("results").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("get","../authenticate?"+form,false); 
    xmlhttp.send(); 
    return false; 
    } 

的Java

public String authenticate() 
{ 
    if(user is authenticated) 
     return a success parameter to JavaScript 
    else 
    return a wrong password message to JavaScript 
} 

信息

後端就是Java。

我知道我可以使用window.location.href來重定向,但需要確保用戶在重定向之前進行身份驗證。

+0

讓我知道什麼是投了反對票? – J888

+0

這是真的* Java問題嗎? –

+0

它與java有關,我想,知道java的人可能會有更好的解決方案。另外有些時候人們也會要求後端語言。 – J888

回答

1

需要成功作爲其文本返回一個頁面,並使用以下

if(xmlhttp.responseText.trim() == 'Success') 
      { 
       redirect code 
      } 
      else{ 
        show error message 
      } 
1
$.getJSON(url, myDataForTheServer) 
    .done(function (dataReturned) { 
     console.log(dataReturned); 
    }) 
    .fail(function (response, status, statusText) { 
     console.log(status + statusText); 
    }) 
    .always(function() { 
     console.log('the end'); 
    }) 
+0

謝謝應該怎樣的java代碼? – J888

+0

你在用什麼框架? – dbrin

1

我認爲你可以從後端用Java認證用戶。

驗證後,密碼錯誤或正確。基於此結果,分別給出一個鏈接作爲返回結果,併爲會話或cookie中的當前用戶設置一個狀態。

當然,在渲染已驗證頁面時,您需要檢查會話是否有效,以決定是否顯示內容。

您可以使用php或asp或任何動態web語言提供幫助。

相關問題