我正在尝试使用java开发一个facebook应用程序。有人可以帮帮我吗?
中提供的代码这里我给出了两个servlet,一个jsp文件和一个xml文件,这些都是我从上面提到的网站获得的。
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();
}
}
这是第二个servlet文件
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;
}
}
这是jsp文件
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>
还提到我们也可以用下面提到的fbml文件替换jsp文件
<%@ 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>
并且web.xml文件是
<?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>
现在,我的问题是,当我访问网址http://http://localhost:8080/myfacebookapps/main_page.jsp/时,它会出现404错误。
任何人都可以帮我解决我做错的事吗?其实我是这个人的新手。
答案 0 :(得分:2)
您的web.xml
没有引用您已实施的2个servlet。
将这样的内容添加到web.xml
。描述符必须具有<servlet>
声明和映射到servlet的映射(称为<servlet-mapping>
)
示例:
<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>
确保您的<servlet-class>
完全一个完全限定的servlet类。
您对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>
更多资源: