Social Java
Social Application development using Java
Java Tutorial Facebook Application - Social Java Step-by-step:
Description:
A detailed description of the Step-by-step app is on the blog .
See Step-by-step Facebook app in Java - Introduction
1. Authenticating the user and adding the application
See prerequisites below
2. Shows the user id of the logged on user
This is pretty simple. If the user is logged on, the user id is passed in as a request parameter.
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
3. Shows the count of accesses for this page
In an earlier verion, I stored the access count in a file. Source code
To do that I used the counter code example from Jason Hunter's really great book on Servlets for this.
The goal is to show persistent storage and a file can accomplish that. Storing data in a file works well for simple programs and experimenting with the Facebook API.
The current version uses JDBC to store a single integer called COUNT in a table called COUNTER.
4. Accepts input on the canvas page to display on the profile page
The key idea in the PHP example is that request parameters drive what the applications does. For the Form on the Canvas Page, the text input is displayed on the profile page.
The form is output to the page. The POST is done to the same servlet. The parameter is checked to determine what action to take.
Here are the code snippets:
// Read the request parameters
String displayText = req.getParameter("profiletext");
if (displayText!=null){
facebook.profile_setFBML(displayText, userLong);
}
// Set up input form to mimic PHP Example code
servletOutput.println("< /pre > ");
servletOutput.println("< action="\" method="\">");
servletOutput.println("< name="\" type="\" size="\" value="\"> <>");
servletOutput.println("< name="\" type="\" value="\"> ");
servletOutput.println("< /form>")\
5. Includes mock ajax on the profile page.
Uses similar technique. mockfbmltext is a request parameter. If it is not null, the servlet returns the value in the parameter. The Mock Ajax is set up in the form to display these contents.
String mockfbmltext = req.getParameter("mockfbmltext");
if (mockfbmltext!=null){
servletOutput.println(mockfbmltext);
}
// Set up input form to mimic PHP Example code
servletOutput.println("< /pre> ");
servletOutput.println("< action="\" method="\"> ");
servletOutput.println("< name="\" type="\" size="\" value="\"> <> ");
servletOutput.println("< name="\" type="\" value="\"> ");
servletOutput.println("< /form> ");
String appCallBackUrl = "http://www.socialjava.com/servlet/SocialJavaTutorial/";
String mockAjax=null;
mockAjax+="<> ";
mockAjax+="< name="\" type="\" size="\"> ";
mockAjax+="<> ";
mockAjax+="< type="\" clickrewriteurl="\" clickrewriteid="\" value="\"> ";
mockAjax+="<> ";
mockAjax+="< id="\" style="\"> ";
mockAjax+="< /div> ";
mockAjax+="< /form> ";
facebook.profile_setFBML(mockAjax.toString(), userLong);
Prerequisites:
The Java Code :
Download Source
Screenshot of updating the profile from the Canvas Page
Screenshot of MockAjax on the Profile Page