import javax.servlet.http.*;
import java.io.IOException;
import javax.servlet.*;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.PrintWriter;
import com.facebook.api.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SocialJavaTutorial extends HttpServlet {
int count;
//Application API Key and application secret from creating app in FB
String appapikey = new String("7623a6ab94a7cc519b1fb73627afdda0");
String appsecret = new String("90e48- your secret here - df6d957e");
//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";
String profilePage = "http://www.facebook.com/profile.php";
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" );
// do authentication
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("
"); // displays numeric value
// use JDBC to store and update the count
Connection con;
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
servletOutput.println(ex +" error");
}
try {
con = DriverManager.getConnection("jdbc:mysql://HOST/DATABASE?" +
"user=USER_NAME&password=PASSWORD");
String query;
try {
stmt = con.createStatement();
query ="Select count from counter"; //counter is the table that contains count
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
int count = rs.getInt("count");
count++;
servletOutput.println("the count is " + count +"
");
query ="UPDATE counter SET count=" + count; //update count in Database
stmt.executeUpdate(query);
} else { // if it is the first time access
stmt = con.createStatement();
query ="INSERT INTO counter (count) VALUES (1)";
stmt.executeUpdate(query);
servletOutput.println("You are number 1");
}
con.close();
} catch(SQLException ex) {
servletOutput.println("SQLException: " + ex.getMessage());
}
} catch (SQLException ex) {
// handle any errors
servletOutput.println("SQLException: " + ex.getMessage());
servletOutput.println("SQLState: " + ex.getSQLState());
servletOutput.println("VendorError: " + ex.getErrorCode());
}
// Try FQL to get name
String query = "SELECT name FROM user WHERE uid=" + user;
org.json.JSONArray resultArray = (org.json.JSONArray)facebook.fql_query(query);
try{
JSONObject result = resultArray.getJSONObject(0);
servletOutput.println("User Name is " + result.getString("name"));
servletOutput.println("
");
} catch(JSONException jex){
servletOutput.println(">Error: JSON " + jex );
}
// Set up input form on canvas page
servletOutput.println("");
servletOutput.println("