how to make a Facebook application in java using eclipse and jboss? -
i trying develop facebook application using java. can please me out?
i have used code provided in http://www.developer.com/java/article.php/10922_3733751_7/writing-facebook-applications-using-java-ee.htm
here giving 2 servlets , 1 jsp file , 1 xml file got above mentioned site.
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 = "fb_api_key"; private static final string fb_secret_key = "fb_secret_key"; public abstractfacebookservlet() { super(); } /* * method used of application's servlets (or web * framework actions) authenticate app 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(); } }
and here second servlet file
mainpageservlet.java
public class mainpageservlet extends abstractfacebookservlet implements javax.servlet.servlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { facebookrestclient facebook = getauthenticatedfacebookclient(request, response); if (facebook != null) { if (getfacebookinfo(request, facebook)) { request.getrequestdispatcher("/main_page.jsp").forward(request, response); } } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } /* * method obtains basic facebook profile information * logged in user accessing our application in current http * request. */ private boolean getfacebookinfo(httpservletrequest request, facebookrestclient facebook) { try { long userid = facebook.users_getloggedinuser(); collection<long> users = new arraylist<long>(); users.add(userid); enumset<profilefield> fields = enumset.of( com.facebook.api.profilefield.name, com.facebook.api.profilefield.pic); document d = facebook.users_getinfo(users, fields); string name = d.getelementsbytagname("name").item(0) .gettextcontent(); string picture = d.getelementsbytagname("pic").item(0) .gettextcontent(); 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) { e.printstacktrace(); return false; } return true; } }
this jsp file
main_page.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %> <strong>myacebookapp main page</strong> <div> <a href="http://www.facebook.com/profile.php?id=${uid}"> <img src="${profile_picture_url}"><br> </a> <a href="http://www.facebook.com/profile.php?id=${uid}"> ${profile_name}</a>, special because using myfacebookapp! </div>
there mentioned can replace jsp file fbml file mentioned below
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %> <strong>myacebookapp main page</strong> <div> <fb:profile-pic uid="loggedinuser" size="small" linked="true" /><br> <fb:name uid="loggedinuser" useyou="false" linked="true" capitalize="true" />, special because using myfacebookapp! </div>
and web.xml file is
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>myfacebookapp</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
now, problem is, when accessing url http://http://localhost:8080/myfacebookapps/main_page.jsp/, giving 404 error.
can please me out doing wrong? new in thing.
your web.xml
has no references 2 servlets you've implemented.
add web.xml
. descriptor must have <servlet>
declaration , mapping map servlet (called <servlet-mapping>
)
example:
<servlet> <description>abstract facebook servlet</description> <display-name>abstractfacebookservlet</display-name> <servlet-name>abstractfacebookservlet </servlet-name> <servlet-class>abstractfacebookservlet </servlet-class> </servlet> <servlet> <description>main page servlet servlet</description> <display-name>mainpageservlet</display-name> <servlet-name>mainpageservlet </servlet-name> <servlet-class>mainpageservlet </servlet-class> </servlet>
make sure <servlet-class>
exactly fully-qualified servlet class.
and mappings servlet,
<servlet-mapping> <servlet-name>abstractfacebookservlet</servlet-name> <url-pattern>/facebook/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>mainpageservlet</servlet-name> <url-pattern>/main/*</url-pattern> </servlet-mapping>
further resources:
Comments
Post a Comment