import javax.servlet.http.*; import java.io.IOException; import javax.servlet.*; import java.io.PrintWriter; import com.facebook.api.*; import java.io.*; public class SocialJavaTutorial extends HttpServlet { int count; //Application API Key and application secret from creating app in FB String appapikey = new String("762- your api key here -a7fb73627afdda0"); String appsecret = new String("90e- your api secret here -7b47df6d957e"); //Facebook loginPage to this application. Parameter canvas=true shows the result in Facebook canvas String loginPage = "http://www.facebook.com/login.php?api_key="+"00b0049fc7cf0d1e4a4ba2ea3e55b269"+"&v=1.0&canvas=true"; FacebookJsonRestClient facebook; // the facebook client, talks to REST Server PrintWriter servletOutput; // output of servlet. HTML or FBML out public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException{ servletOutput = res.getWriter(); // response is sent to ServletOutput res.setContentType( "text/html" ); servletOutput.println("This is the doGet method"); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { servletOutput = res.getWriter(); // response is sent to ServletOutput res.setContentType( "text/html" ); servletOutput.println("This is the doPost method - 5"); servletOutput.println("
"); String user =null; String sessionKey=null; sessionKey = req.getParameter(FacebookParam.SESSION_KEY.toString()); // Session Key passed as request parameter if (sessionKey==null) { // If there is not session key, they user not logged in servletOutput.println(""); // Facebook Redirect to login page } else{ facebook = new FacebookJsonRestClient(appapikey, appsecret,sessionKey); // create Facebook Json Rest Client // servletOutput.println("Facebook Client created"); // servletOutput.println("
"); // with Facebook client created, now Facebook API calls can be made // In this case, a call to FQL to get username count++; servletOutput.println("Since the beginning, this servlet has been accessed " + count + " times."); try{ String mockfbmltext = req.getParameter("mockfbmltext"); String displayText = req.getParameter("profiletext"); if (mockfbmltext!=null){ servletOutput.println(mockfbmltext); }else{ user = req.getParameter("fb_sig_user"); // get user as a string. User info passed as request parameter servletOutput.println("User is " + user); // displays numeric value servletOutput.println("
"); String query = "SELECT name FROM user WHERE uid=" + user; org.json.JSONArray resultArray = (org.json.JSONArray)facebook.fql_query(query); // query return an object. Casting it as a String servletOutput.println("User Name is " + resultArray); Long userLong = new Long(user); // need user as a long for API calls if (displayText!=null){ facebook.profile_setFBML(displayText, userLong); } // Set up input form to mimic PHP Example code servletOutput.println(""); servletOutput.println("
"); servletOutput.println("
"); servletOutput.println(""); servletOutput.println("
"); String appCallBackUrl = "http://www.socialjava.com/servlet/SocialJavaTutorial/"; String mockAjax=null; mockAjax+="
"; mockAjax+=""; mockAjax+="
"; mockAjax+=""; mockAjax+=""; mockAjax+="
"; facebook.profile_setFBML(mockAjax.toString(), userLong); } } catch( FacebookException ex ) { servletOutput.println(">Error: Couldn't talk to Facebook> " + ex ); } } //else user is logged in servletOutput.close(); } // end doPost() public void init() throws ServletException { // Try to load the initial count from our saved persistent state FileReader fileReader = null; BufferedReader bufferedReader = null; try { fileReader = new FileReader("InitDestroyCounter.initial"); bufferedReader = new BufferedReader(fileReader); String initial = bufferedReader.readLine(); count = Integer.parseInt(initial); return; } catch (FileNotFoundException ignored) { } // no saved state catch (IOException ignored) { } // problem during read catch (NumberFormatException ignored) { } // corrupt saved state finally { // Make sure to close the file try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException ignored) { } } // No luck with the saved state, check for an init parameter String initial = getInitParameter("initial"); try { count = Integer.parseInt(initial); return; } catch (NumberFormatException ignored) { } // null or non-integer value // Default to an initial count of "0" count = 0; } public void destroy() { super.destroy(); // entirely optional saveState(); } public void saveState() { // Try to save the accumulated count FileWriter fileWriter = null; PrintWriter printWriter = null; try { fileWriter = new FileWriter("InitDestroyCounter.initial"); printWriter = new PrintWriter(fileWriter); printWriter.println(count); return; } catch (IOException e) { // problem during write // Log the exception. See Chapter 5. } finally { // Make sure to close the file if (printWriter != null) { printWriter.close(); } } } }