访问JSON的对象值以将其发送到另一个servlet或jsp

问题描述 投票:1回答:1

我不熟悉使用Facebook应用程序环境。我正在开发一个小应用程序。我正在尝试在项目中使用一些社交数据来检查某些条件。我不知道我是否以正确的方式访问FB返回的JSON对象,但这是我的问题。

我想将名字从当前文件发送到另一个jsp或servlet。因此,我创建了jsp变量,并尝试在从Facebook获得的响应对象中分配“名称”的值。

[尝试在jsp文件末尾打印名字时,名字输出为空。如何在对象中使用名称和性别值并将其发送到另一个jsp文件或servlet文件?

B着头寻找解决方案。有人请帮忙!

<%@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> 
javascript facebook facebook-graph-api facebook-javascript-sdk dom-events
1个回答
0
投票
在您的JavaScript代码中,firstname=firstname+response.name;没有设置Java代码中先前定义的String变量。相反,它设置了一个新变量,该变量只能由您的JavaScript代码看到。

您的Java代码在服务器端运行,而JavaScript在客户端运行。它们是两个完全不同的程序,在完全不同的位置在不同的时间执行。由于这样的原因,不建议在同一文件中同时编写客户端和服务器端代码。

关于解决问题的方法,我建议要么为Facebook寻找Java API,以便可以在呈现JSP之前使用它,要么以JavaScript代码访问从FB返回的数据。仔细阅读Java,JavaScript,服务器端和客户端代码之间的差异也是个好主意。

© www.soinside.com 2019 - 2024. All rights reserved.