April 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Writing Facebook Applications Using Java EE, Page 5

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

Eclipse will create a package in the "Java Resources: src" folder inside the project folder, and in this package you will find AbstractFacebookServlet.java. Eclipse will open AbstractFacebookServlet.java for you. Replace all of the code in it with the following:

Listing 1.1: AbstractFacebookServlet.java

package org.myfacebookapp;

import java.io.IOException;

import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.w3c.dom.Document;

import com.facebook.api.*;

/**
 * Servlet implementation class for Servlet:
 * AbstractFacebookServlet
 *
 */
public class AbstractFacebookServlet
      extends javax.servlet.http.HttpServlet
      implements javax.servlet.Servlet
{
   protected static final String FB_APP_URL =
      "http://apps.facebook.com/myfacebookapp/";

   protected static final String FB_APP_ADD_URL =
      "http://www.facebook.com/add.php?api_key=";

   protected static final String FB_API_KEY =
      "your-api-key-goes-here";

   private static final String FB_SECRET_KEY =
      "your-secret-key-goes-here";

   public AbstractFacebookServlet() {
      super();
   }

   /*
    * This method is used by all of the application's servlets
    * (or  web framework actions) to authenticate the app with
    * Facebook.
    */
   protected FacebookRestClient getAuthenticatedFacebookClient(
         HttpServletRequest request, HttpServletResponse response)
   {
      Facebook fb = new Facebook(request, response,
         FB_API_KEY, FB_SECRET_KEY);

      String next = request.getServletPath().substring(1);

      if (fb.requireLogin(next)) return null;

      return fb.getFacebookRestClient();
   }
}

The getAuthenticatedFacebookClient() method above needs to be called before making any calls to the Facebook API through the FacebookRestClient. You could put the FacebookRestClient in the session object with the idea of authenticating less often, but you will find in practice that your stored FacebookRestClient will lose authentication even though your app session hasn't yet expired. Authenticating every time doesn't use that much more bandwidth because you're going to be calling back through the API anyhow to get information to process your users' requests.

Before you continue, replace the FB_API_KEY and FB_SECRET_KEY constants with the correct settings from your registered Facebook application (the last step from the section "First Steps").

Now, create another servlet, called MainPageServlet.java. Once you have the MainPageServlet.java created, replace the package, import statements, comment block, and class definition with this block of code:

package org.myfacebookapp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import java.util.*;

import org.w3c.dom.Document;

import com.facebook.api.*;

/**
 * Servlet implementation class for Servlet: MainPageServlet
 *
 * @web.servlet
 *   name="MainPageServlet"
 *   display-name="MainPageServlet"
 *
 * @web.servlet-mapping
 *   url-pattern="/"
 *
 */
public class MainPageServlet extends AbstractFacebookServlet
      implements javax.servlet.Servlet {

Note that this servlet will extend from AbstractFacebookServlet, not from javax.servlet.http.HttpServlet. This will allow MainPageServlet to inherit getAuthenticatedFacebookClient(). The comment block is a set of XDoclet properties for servlet definitions in the web.xml document in the application's WEB-INF folder. This declares that MainPageServlet should be mapped to the root ("/" or the slash) of the web app's base URL.





Page 5 of 7



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel