Java Web Log: Fixes and Functionality Add-Ons

The purpose of this tutorial is to add nicer formatting to the weblog and to create an administrative end to keep track of different users and sessions, allowing users with verified passwords to browse the weblog as they choose, without having to sign in every time they want to add an entry or post. This will provide a login/logout option.

As you may have discovered as you explore your web log, you can actually make comments on comments, and comments on those which show up in another list of comments. This is confusing! So, here are a couple of fixes:

Fixes

 * comment on threads (entries) only
 * add links to each page so that you don't have to do everything manually in the browser
 * options to add color to your web log

ShowComment
First, in ShowComment:

Change

to

This will eliminate the links to adding and viewing comments while you are viewing a comment, simplifying everything. Next, we add links at the bottom of the comments page to posts and an option to add a new comment.

This: becomes this:

WriteComment
Now that the commenting is all taken care of, it's time to add links onto the pages that say "Your entry was recorded." So, in WriteComment:

Add in this line before disconnecting from MySQL (at the bottom)

This way, you can navigate back to the ShowComment page using a link instead of having to retype it in the browser.

add_comment.jsp
You will also need to change the add_comment.jsp file because of this.

After the new hidden field from the last tutorial, add these lines:

A small "fix" (optionally) is to change the page name and form to something that is more clearly a comment, rather than a log entry.

So, at the top of the add_comment file, change the title to something you want, say, Web Log Comment. Then, right after the form action="/blog/WriteComment">, change Web Entry to something like Comment Entry

Show
The following adds some formatting to the Show page, and makes links at the bottom to the next and prev pages in the weblog. This is done by calculating how many posts there already are (10 to a page) and so how many of the pages ought to have a next or prev. If you are on the first page, obviously there will not be a page before that. If you are on the last page, then there are no more pages after it. Here's the code:

The moves all the entries over by 30 pixels, making the page look a little better.

We want to also add an option for making a log, or entry, in the weblog.

Remove the else statement about being out of entries. Thus you should go straight to disconnecting from MySQL after showing the previous page. Then, when printing the HTML header, add this after printing "My Web log":

Color!
If you want to make a solid color background to your weblog, insert one of the following into the out.println(""); where the color = : aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, or yellow. If you want to use other colors, you must specify their RGB or HEX value, which can be found here.

Deleting Entries
So you have a nicely working blog. But what if you want to delete an entry you made? Here's how to add this functionality:

delete_post.jsp
First you will need to create a new .jsp file. delete_post.jsp looks like this:

and goes into your blog directory, just like the other jsp's.

Delete.java
Next, you will need a Delete.java class. This is very similar to other classes, and you can still add in the colors and formatting if you wish.

Delete.java:

web.xml
Don't forget to change your web.xml file to include this new class!

Add this before the  tag:

Show
As before, we want to add in a link to this function on your weblog.

Add this to your (long) println, where you display the links for adding and viewing comments:

Deleting Comments
This function allows you to delete specific comments.

delete_comment.jsp
Like deleting entries, you will need a new .jsp file, delete_comment.jsp as follows:

DeleteComment.java
...And a class to go with it...

web.xml
Additionally, add this as a servlet into web.xml Follow previous examples-you've been doing this a lot now!-and good luck.

ShowComment.java
Again, we want to add a link in to delete a specific comment. This works best, if there is more than one comment, if that option exists for every comment. So, when ShowComment iterates through the list of entries, it will also list an option to delete the comment. The code is as follows:

Continue in the tutorial to learn about logging in ability!