Part III: A Better Weblog

[article] [edit page] [discussion] [history]

From Humanitarian-FOSS Project Development Site

This step in the tutorial will add functionality to the previously created web log by allowing readers to comment on entries and see all comments on a specific entry.

Contents

Adding A Table To The Database

Add a new table to the blog database using the following mysql script [this will make a table that keeps track of all the comments of each entry]:

mysql> use blog;

mysql> create table thread 
(comment_id int not null auto_increment primary key, blog_id int not null, data blob, created timestamp not null);


Changing Show.java

Show.java needs to be changed in order to provide links to both add and view comments.


Change

 ResultSet rs = stmt.executeQuery("SELECT data, DATE_FORMAT(created,'%M %d, %Y %H:%i') as nice_date from blog order by created desc limit " + start 
+ ", " + numPerPage);

to

ResultSet rs = stmt.executeQuery("SELECT data,  entry_id, DATE_FORMAT(created,'%M %d, %Y %H:%i') as 
nice_date from blog order by created desc limit " + start + ", " + numPerPage);

This changes the query to the database to get the entry_id data as well, so that we can link the comments to their corresponding blog posts. Change

while (rs.next() ) 
// iterate through the list of entries, and print them to the page 
{
  out.println("<b>" + rs.getString("nice_date") + "</b><br>" + rs.getString("data") + "<br><br>");  counter++;
}

to:

while (rs.next() ) 
// iterate through the list of entries, and print them to the page 
{
  out.println("<b>" + rs.getString("nice_date") + "</b><br>" + rs.getString("data") + "<br><br>"+ "<a href=/blog/add_comment.jsp?blogid="
+rs.getString("entry_id")+">Add Comment  </a> <a href=/blog/ShowComment?blogid="+rs.getString("entry_id")+">View Comments</a>"+"<br><br>");
  counter++;
}

Now we add two links below the blog post called "Add Comment" and "View Comments". When Add Comment is clicked, add_comment.jsp is loaded with a parameter called blogid which corresponds to the id of the entry in the database. The way View Comments works will be explained later.

Adding Comments

Improving the web log's functionality by adding a feature which allows you to comment on an entry requires you to add three files. The following code will create these three new files so you can edit them:

vagrawal@apl:/home3/vagrawal/apps/apache-tomcat-6.0.16/webapps/blog$cp add_data.jsp add_comment.jsp
vagrawal@apl:/home3/vagrawal/apps/apache-tomcat-6.0.16/webapps/blog$cd WEB-INF/classes/jblog
vagrawal@apl:/home3/vagrawal/apps/apache-tomcat-6.0.16/webapps/blog$cp Show.java ShowComment.java
vagrawal@apl:/home3/vagrawal/apps/apache-tomcat-6.0.16/webapps/blog$cp Write.java WriteComment.java


add_comment.jsp

add_comment.jsp is adapted from add_data.jsp, so a few things need to be changed. First we need to change the form action by changing this line:
<form action="/blog/Write"> 
to
 <form action="/blog/WriteComment"> 


Then we need to get the specific blog id of the post. This is achieved by changing these lines: Add String blogid = request.getParameter("blogid"); after

 
<%
String data = request.getParameter("data"); 

and before the next %> tag.

Next, we need to add a hidden field to the .jsp file to pass along the blog id value. Add this line to your add_comment.jsp:

<input type="hidden" name="blogid" value="<%=blogid%>">

WriteComment.java

Make sure to change the class declaration of your file to public class WriteComment {}.

Add this line

String blogid = request.getParameter("blogid"); 

after this snippet of code:

String data = request.getParameter("data");
String password = request.getParameter("password");

This gets the blog id from add_comment.jsp so that we can write the appropriate blog id to the database.

Next you need to add in a parameter to the if statement: if ( (data != null) && (password != null) && (blogid !=null)

The private method writeData() needs to then take the blogid as a parameter. Just add:

private void writeData(String data, String password, String blogid)

Next, we need to change the query so that we can get the blog id data from the database.

int written =  stmt.executeUpdate("insert into blog (data) values (\"" + data + "\")");

to

int written =  stmt.executeUpdate("insert into thread (data,blog_id) values ('"+data+"','"+blogid+"')");

ShowComment.java

Don't forget to change the class declaration of your file to public class ShowComment {}.

Add a String parameter to printEntries() called blogid, ie

private void printEntries(int start, String blogid)

Change the executeQuery() function to

 ResultSet rs = stmt.executeQuery("SELECT data, DATE_FORMAT(created,'%M %d, %Y %H:%i') as nice_date from thread where blog_id='"+blogid+"'"+" order by created desc limit " + start + ", " + numPerPage);

This gets a set of comments that are associated with this particular blog id. Next, we will display them on the page. Replace the call to printEntries() with this piece of code:

String blogid = request.getParameter("blogid");
printEntries(start, blogid);

Finishing Up

Don't forget to change your web.xml to include the new servlets!

<servlet>
         <servlet-name>WriteComment</servlet-name>
		<description>
			Write comment
		</description>
		<servlet-class>jblog.WriteComment</servlet-class>
	</servlet>
<servlet>
	<servlet-name>ShowComment</servlet-name>
	<description>
		Show Comment
	</description>
	<servlet-class>jblog.ShowComment</servlet-class>
</servlet>
	
<servlet-mapping>
	<servlet-name>WriteComment</servlet-name>
	<url-pattern>/WriteComment</url-pattern>
</servlet-mapping>
	
<servlet-mapping>
	<servlet-name>ShowComment</servlet-name>
	<url-pattern>/ShowComment</url-pattern>
</servlet-mapping>

Go to http://localhost:8080/blog/Show to check out your newly-upgraded blog!

Continue in the tutorials to add a login feature!

Continue here for fixes, new functions, and a login/admin part of your weblog.

Personal tools