January 20, 2021
Hot Topics:

Writing Facebook Applications Using Java EE

  • 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) ) {
               "/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>();

         EnumSet<ProfileField> fields = EnumSet.of (

         Document d = facebook.users_getInfo(users, fields);
         String name = 
         String picture = 

         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) {

         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"
<strong>myacebookapp Main Page</strong>

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

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."

Page 6 of 7

This article was originally published on March 13, 2008

Enterprise Development Update

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

Thanks for your registration, follow us on our social networks to keep up-to-date