为Facebook API调用Servlet以获取访问令牌

问题编辑:

我正在使用Captain Casa框架。

我有一个按钮,将打开一个新标签并转到Facebook登录页面。

public void goTofbPage(javax.faces.event.ActionEvent event){ FBConnection fbConnection = new FBConnection(); setBrowserUrl(fbConnection.getFBAuthUrl()); m_browserTrigger.trigger(); } 

如果用户成功登录,它将与用户的数据一起重定向到我的页面。

为了从用户那里获取accessToken,我使用了HttpServlet。

但我怎么能称这个HttpServlet?

我真的需要调用它还是会自动运行?

我的jsp看起来像这样。

                                  

我已经将它添加到web.xml中了。

  MainMenu managedbeans.MainMenu 1   MainMenu /MainMenu  

我的Servlet代码:

 public class MainMenu extends HttpServlet{ private static final long serialVersionUID = 1L; private String code=""; public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { code = req.getParameter("code"); if (code == null || code.equals("")) { throw new RuntimeException( "ERROR: Distadn't get code parameter in callback."); } FBConnection fbConnection = new FBConnection(); String accessToken = fbConnection.getAccessToken(code); FBGraph fbGraph = new FBGraph(accessToken); String graph = fbGraph.getFBGraph(); Map fbProfileData = fbGraph.getGraphData(graph); ServletOutputStream out = res.getOutputStream(); out.println("
Welcome "+fbProfileData.get("first_name")); out.println("
Your Email: "+fbProfileData.get("email")); }

如果它是一个Servlet,那么你需要在Servlet容器中运行它,比如Tomcat或Jetty 。

当Servlet容器正在运行时,它会绑定到特定的IP或主机名以及TCP端口,您应该通过该地址访问它。 例如,如果您在localhost上运行Tomcat(例如,在开发中)并且它侦听端口8080,那么您可以通过对HTTP调用来执行您的servlet

http://127.0.0.1:8080/MainMenu

“/ MainMenu”端点必须与您在url-pattern下的web.xml部署描述符中输入的值匹配