Learn Servlet-JSP By Building A Social Network Website — Part XVI

Sera Ng.
Tech Training Space
3 min readOct 30, 2020

Implement loading posts and comments features

Task 23: Implement loading posts and comments

In this task, we are going to implement features that showing posts and their comments on the news feed page.

For the currently logged-in user, we will show his/her posts as well as his/her friends’ posts. And comments for those posts, if any, will be fetched along as well.

Step 1:

In the Key class, add the following new constant variable:

public static final String POST_LIST = “postlist”;

Step 2:

Since now we are going to interact with posts and comments, we need to create model classes corresponding to the table tb_post and tbl_comment.

In the package net.learnbyproject.model, create a new class called Comment with the following trivial implementation:

Next, in the same package, create another new class to store post data called Post with the following structure:

Since the tbl_post has a one-to-many relationship with the table tbl_comment, for convenient processing, in the Post class, we have a list of Comment instances.

Step 3:

We need to create a new service class to conduct post-related business logic.

In the package net.learnbyproject.service, create a new service class called: PostService.

To retrieve a list of comments based on a particular post, which is represented by post id, we implement the following method in the PostService class:

Next, we need the following method to fetch a list of the currently signed-in user’s posts:

The above query will fetch not only the author’s posts but also the author’s friends’ post by applying subquery in the select statement. The result will be sorted in descending order as well.

Pay attention to that, since each post might have a list of comments, in the while loop, for each iteration of fetching a post, we need to retrieve a list of associated comments as well before we add the post to the returned list.

Step 4:

In the NewsFeedController servlet, update the processRequest() method to get a list of posts as follows:

Step 5:

Now everything in the back is ready, we need to update the news-feed.jsp to display the current user’s posts and associated comments.

At the top of the news-feed.jsp, add the following 2 lines if you have not already done so:

<%@page import=”net.learnbyproject.helper.Keys”%>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core" %>

Then, locate the <div class=”middle-section user-post”> … </div> section and update as follows:

There are many places needed to be updated, but the code itself is easy to comprehend. All we need to do is to replace the sample data element with the corresponding EL expression.

And as you can see in the update since we have built up for each post instance containing a list of comment instances, it is extremely convenient to get and traverse through a list of comment objects to show the data:

Now everything should be ready for a test.

The database is shipped with some sample posts and comments, be sure to log in with the user what is already a posts’ author.

--

--