1

我是使用Facebook應用程序環境的新手。我正在開發一個小應用程序。我試圖在我的項目中使用一些社交數據來檢查一些條件。 我不知道我是否以正確的方式訪問FB返回的JSON對象,但這是我的問題。訪問JSON的對象值,將它們發送到另一個servlet或jsp

我想從當前文件發送firstname到另一個jsp或servlet。所以,我創建了jsp變量,並嘗試在從Facebook獲取的響應對象中分配「name」的值。

當試圖在jsp文件的末尾打印firstname時,firstname輸出爲空。如何在對象中使用名稱和性別值並將其發送到另一個jsp文件或servlet文件?

幫我找頭解決方案。有人請幫忙!

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
         "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Facebook authentication usage</title> 
    </head> 
    <body> 

    <%! String firstname=""; 
     String lastname=""; 
     String middlename="lynne"; 

    %> 



    <div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
             // init the FB JS SDK 
             FB.init({ 
             appId  : '532238706811106', // App ID from the App Dashboard 
             //channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication 
             status  : true, // check the login status upon init? 
             cookie  : true, // set sessions cookies to allow your server to access the session? 
             frictionlessRequests : true, 
             xfbml  : true // parse XFBML tags on this page? 

              }); 

    // Additional initialization code such as adding Event Listeners goes here 


    FB.getLoginStatus(function(response) { 
              if (response.status === 'connected') 
              { 

               // the user is logged in and has authenticated your 
               // app, and response.authResponse supplies 
               // the user's ID, a valid access token, a signed   
               // request, and the time the access token 
               // and signed request each expire 
               var uid = response.authResponse.userID; 
               var accessToken = response.authResponse.accessToken; 
               FB.api('/me', function(response) 
                  { 
                   console.log(response); 
                   console.log('Good to see you, ' + response.name + '.'); 
                   firstname=firstname+response.name; 
                   lastname=lastname+response.last_name; 
                   document.write(response.name+middlename); 
                  }); 
              } 
              else if (response.status === 'not_authorized') 
              { 
               // the user is logged in to Facebook, 
               // but has not authenticated your app 
               var oauth_url = 'https://www.facebook.com/dialog/oauth/'; 
               oauth_url += '?client_id=532238706811106'; //Your Client ID 
               oauth_url += '&redirect_uri=' + 'https://apps.facebook.com/newjsp'; //Send them here if they're not logged in 
               //oauth_url += '&scope=user_about_me,email,user_location,user_photos,publish_actions,user_birthday,user_likes'; 

               window.top.location = oauth_url; 

              } 
              else 
              { 
               // the user isn't logged in to Facebook. 

               window.top.location ='https://www.facebook.com/index.php'; 
              } 
         }); 

    }; 
    // Load the SDK's source Asynchronously 
    // Note that the debug version is being actively developed and might 
    // contain some type checks that are overly strict. 
    // Please report such bugs using the bugs tool. 
    (function(d, debug) 
      { 
       var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
       if (d.getElementById(id)) {return;} 
       js = d.createElement('script'); js.id = id; js.async = true; 
       js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js"; 
       ref.parentNode.insertBefore(js, ref); 
      }(document, /*debug*/ false)); 


</script> 
    <h1>this is a sample to test facebook authentication 
     Welcome <%=firstname+""+lastname+middlename%></h1> 

     <%out.print(firstname+""+lastname+""+middlename); %> 

    </body> 
</html> 

回答

0

這是因爲你設置firstname""但永遠不會從該值改變。

在您的JavaScript代碼中,firstname=firstname+response.name;未設置先前在Java代碼中定義的字符串變量。相反,它會設置一個只能由JavaScript代碼看到的新變量。

您的Java代碼在服務器端運行,而JavaScript在客戶端運行。他們是兩個完全不同的節目,在不同的時間在不同的地點執行。出於這個原因,不建議在同一個文件中寫入客戶端和服務器端代碼。

至於你的問題的解決方案,我會建議爲Facebook尋找一個Java API,這樣你就可以在JSP呈現之前使用它,或者你可以在JavaScript代碼中完全訪問從FB返回的數據。閱讀Java,JavaScript,服務器端和客戶端代碼之間的區別也是一個好主意。

相關問題