November 23, 2014
Hot Topics:

Writing Facebook Applications Using Java EE

  • March 13, 2008
  • By PJ Cabrera
  • Send Email »
  • More Articles »

Next, replace the MainPageServlet's doGet() and doPost() methods with the following code:

   protected void doGet(
         HttpServletRequest request,
            HttpServletResponse response)
         throws ServletException, IOException 
   {
      FacebookRestClient facebook =
         getAuthenticatedFacebookClient(request, response);

      if ( facebook != null) {
         if ( getFacebookInfo(request, facebook) ) {
            request.getRequestDispatcher(
               "/main_page.jsp").forward(request, response);
         }
      }
   }

   protected void doPost(
         HttpServletRequest request,
            HttpServletResponse response)
         throws ServletException, IOException
   {
      doGet(request, response);
   }

The MainPageServlet's doGet() method gets the authenticated FacebookRestClient immediately, and then calls a method called getFacebookInfo(), which you will define shortly. If getFacebookInfo() returns true, the code proceeds to redirect the server to "/main_page.jsp." You also will define this JSP page shortly.

Continue fleshing out MainPageServlet's code. Insert this getFacebookInfo() method right after the doPost() method.

   /*
    * This method obtains some basic Facebook profile
    * information from the logged in user who is
    * accessing our application in the current HTTP request.
    */
   private boolean getFacebookInfo(
         HttpServletRequest request,
         FacebookRestClient facebook) 
   {
      try {

         long userID = facebook.users_getLoggedInUser();
         Collection<Long> users = new ArrayList<Long>();
         users.add(userID);

         EnumSet<ProfileField> fields = EnumSet.of (
            com.facebook.api.ProfileField.NAME,
            com.facebook.api.ProfileField.PIC);

         Document d = facebook.users_getInfo(users, fields);
         String name = 
            d.getElementsByTagName("name").item(0).getTextContent();
         String picture = 
            d.getElementsByTagName("pic").item(0).getTextContent();

         request.setAttribute("uid", userID);
         request.setAttribute("profile_name", name);
         request.setAttribute("profile_picture_url", picture);

      } catch (FacebookException e) {

         HttpSession session = request.getSession();
         session.setAttribute("facebookSession", null);
         return false;

      } catch (IOException e) {

         e.printStackTrace();
         return false;
      }
      return true;
   }

This piece of code is where the meat of the Facebook action is for this example, so to speak. First, you get the user ID of the current logged-in user, and add this to a collection called users. Then, you define an emumeration set of Facebook API ProfileField types, com.facebook.api.ProfileField.NAME and com.facebook.api.ProfileField.PIC. You pass the users collection and the ProfileField enumeration set to a Facebook API called users_getInfo(). This returns an XML document with the profile name and profile image URL of the user IDs in the users collection—the current logged-in user. You parse out the <name> and <pic> elements out of the XML document, and store the user ID, profile name, and picture URL as request parameters. You do this to allow main_page.jsp to have access to this information as request scope beans.

Now, you get to the JSP part of the application. Create a JSP file called main_page.jsp, and insert in it the following code:

<%@ page language="java"
   contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"
%>
<strong>myacebookapp Main Page</strong>

<div>
      <a href="http://www.facebook.com/profile.php?id=${uid}">
      <img src="${profile_picture_url}"><br>
   </a>
   <a href="http://www.facebook.com/profile.php?id=${uid}">
      ${profile_name}</a>,
   you are special because you are using myfacebookapp!
</div>

This JSP page is straightforward and simple. It uses JSP Expression Language to output the value of the request parameters "uid," "profile_name," and "profile_picture_url" you placed in the request in the getFacebookInfo() method from MainPageServlet.java. Using these values, it displays the users' name and profile picture, with links to their profile, and congratulates them for using "myfacebookapp."


Tags: Facebook



Page 6 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel