开发者

how to make a Facebook application in java using eclipse and jboss?

I am trying to develop a facebook application using java. Can anyone please help me out?

I have used the code provided in http://www.developer.com/java/article.php/10922_3733751_7/Writing-Facebook-Applications-Using-Java-EE.htm

Here I am giving two servlets and one jsp file and 1 xml file all that I got from the 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();
    }

    /*
     * This method is used by all of the application's servlets (or web
     * framework actions) to authenticate the app with 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 is the 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);
    }

    /*
     * 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>();
            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 is the 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>,
   you are special because you are using myfacebookapp!
</div>

There is also mentioned that we can also replace the jsp file with the 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" />,
   you are special because you are using myfacebookapp!
</div>

and the 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, what my problem is, When I am accessing the url http://http://localhost:8080/myfacebookapps/main_page.jsp/, it is giving a 404 error.

Can anyone please help me out what I am doing wrong?开发者_运维百科 Actually I am new in this thing.


Your web.xml has no references to your 2 servlets that you've implemented.

Add something like this to your web.xml. The descriptor must have a <servlet> declaration and a mapping to map to the 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 your <servlet-class> is exactly a fully-qualified servlet class.

And your mappings to your 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:

  • Java Servlet Technology.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜