How to create a facebook application to display friend list in jsp page without using script.
use FacebookConnect REST interface from jsp http://wiki.developers.facebook.com/index.php/API
To preprocess a request, you need to create a Servlet
and implement the doGet()
method. Inside this method you can freely write/invoke Java code as you want without cluttering the JSP file with scriptlets. Make use of java.net.URL
and/or java.net.URLConnection
inside the doGet()
method to access the Facebook API by HTTP requests and obtain data as HTTP response. Finally process this data in flavor of List<Friend>
and forward the request to JSP page for display.
Kickoff example:
public class FacebookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URL url = new URL("http://url/to/some/facebook/API/to/get/list/of/friends"); // Read Facebook API doc for the correct URL.
InputStream input = url.openStream();
List<Friend> friends = extractFriends(input); // Do your job here.
request.setAttribute("friends", friends); // It's then available as ${friends} in JSP.
request.getRequestDispatcher("/WEB-INF/friends.jsp").forward(request, response);
}
}
Map this servlet in web.xml
on an url-pattern
of for example /friends
so that you can invoke it by http://example.com/contextname/friends
.
Then, in your /WEB-INF/friends.jsp
(the JSP is placed in /WEB-INF
to prevent from direct access without using the servlet), make use of JSTL (just drop jstl-1.2.jar in /WEB-INF/lib
) c:forEach
tag to iterate over the List<Friend>
and print a HTML table row (<tr>
) on every iteration:
<table>
<c:forEach items="${friends}" var="friend">
<tr>
<td>${friend.name}</td>
<td>${friend.age}</td>
<td>${friend.email}</td>
<td>etc..</td>
</tr>
</c:forEach>
</table>