Java Web Log: Fixes and Functionality Add-Ons

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

From Humanitarian-FOSS Project Development Site

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:

Contents

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

// iterate through the list of entries, and print them to the page {

 		out.println("" + rs.getString("nice_date") + "
" + rs.getString("data") + "

") + "<a href=/blog/add_comment.jsp?blogid="+rs.getString(1)+">Add Comment </a> <a href=/blog/ShowComment?blogid="+rs.getString(1)+">View Comments</a>"+"

"); counter++;

}

to

// iterate through the list of entries, and print them to the page {

 		out.println("" + rs.getString("nice_date") + "
" + rs.getString("data"); counter++;

}

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:

else // out of entries - time to go back to the beginning

           {
               out.println("That's it.  Go back to <a href=\"/blog/Show\">The Beginning</a> 
");

becomes this:

else // out of entries - time to go back to the beginning

           {
               out.println("That's it.  Go back to <a href=\"/blog/Show\">posts</a> 
");

out.println("<a href=/blog/add_comment.jsp?blogid="+blogid+">Add Comment </a>");

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)

// disconnect from MySQL out.println("<a href=\"/blog/ShowComment?blogid="+blogid+"\">Back to Comments</a>
");

           mydb.disconnect(stmt);

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 href="ShowComment?blogid=<%=blogid%>">Back to Comments</a>

<a href="Show">Back to Posts</a>

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:

// iterate through the list of entries, and print them to the page {

out.println("
" + rs.getString("nice_date")
                + "
" + rs.getString("data") + "

"+ "<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>
"+"

");
 		counter++;

} int prev = start - numPerPage;

           int next = start + numPerPage;

if(start!=0 && start%numPerPage==0) out.println("
| <a href=\"/blog/Show?start=" + prev + "\">Prev Page</a> | ");

           if (counter ==  numPerPage )
           {
               out.print("| <a href=\"/blog/Show?start=" + next + "\">Next Page</a> |

"); }

The

</code>

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":

<code>

out.println("

<a href=\"add_data.jsp\">New post</a>


");

</code>

Color!

If you want to make a solid color background to your weblog, insert one of the following into the out.println("<body bgcolor=\"white\">"); 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:

<code>

<html><head><title>Web log Entry</title></head><body bgcolor="white">

<form action="/blog/Delete"> <% String blogid = request.getParameter("blogid"); %>

<input type="submit" value="Confirm?"> <input type="hidden" name="blogid" value="<%=blogid%>">

<a href="Show">Back to posts</a>

</form>

</body></html> </code>

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:

<code>

package jblog;

import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.net.URLEncoder;

public class Delete extends HttpServlet { private MySQLConnector mydb = null; private PrintWriter out = null;

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); out = response.getWriter();

// write the HTML header out.println("<html><head><title>Java Web log</title></head>"); out.println("<body bgcolor=\"white\">"); String blogid = request.getParameter("blogid"); deleteData(blogid); } private void deleteData(String blogid) { try { mydb = new MySQLConnector(); Statement stmt = mydb.connect(); //ResultSet rs = stmt.executeQuery("select count(*) as okay from blog_pass where password = \"" + password + "\""); stmt.executeUpdate("delete from blog where entry_id=\"" +blogid+"\""); out.println("Entry deleted.


<a href=\"/blog/Show\">View entries</a>"); mydb.disconnect(stmt); } catch (Exception ex) { out.println(ex); } } } </code>

web.xml

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

Add this before the </web-app> tag:

<code>

<servlet> <servlet-name>Delete</servlet-name> <description> Delete entry </description> <servlet-class>jblog.Delete</servlet-class> </servlet>

<servlet-mapping> <servlet-name>Delete</servlet-name> <url-pattern>/Delete</url-pattern> </servlet-mapping> </code>

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:

<code>

Add Comment</a> | <a href=/blog/ShowComment?blogid="+rs.getString("entry_id")+">View Comments</a> | <a

href=/blog/delete_post.jsp?blogid="+rs.getString("entry_id")+">Delete Post</a>
"+"

");

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:

<html><head><title>Web log Entry</title></head><body bgcolor="aqua">

<form action="/blog/DeleteComment"> <% String blogid = request.getParameter("blogid"); String commentid = request.getParameter("commentid"); %>

<input type="submit" value="Confirm?"> <input type="hidden" name="blogid" value="<%=blogid%>"> <input type="hidden" name="commentid" value="<%=commentid%>">

<a href="ShowComment?blogid=<%=blogid%>">Back to comments</a>

<a href="Show">Back to posts</a>

</form>

</body></html>

DeleteComment.java

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

package jblog;

import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.net.URLEncoder;

public class DeleteComment extends HttpServlet { private MySQLConnector mydb = null; private PrintWriter out = null;

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); out = response.getWriter();

// write the HTML header out.println("<html><head><title>Java Web log</title></head>"); out.println("<body bgcolor=\"white\">"); String blogid = request.getParameter("blogid"); String commentid = request.getParameter("commentid"); deleteData(commentid,blogid); } private void deleteData(String commentid, String blogid) { try { mydb = new MySQLConnector(); Statement stmt = mydb.connect(); //ResultSet rs = stmt.executeQuery("select count(*) as okay from blog_pass where password = \"" + password + "\""); stmt.executeUpdate("delete from thread where comment_id=\"" +commentid+"\""); out.println("Comment deleted.


<a

href=\"/blog/ShowComment?blogid="+blogid+"\">View comments</a>");

mydb.disconnect(stmt); } catch (Exception ex) { out.println(ex); } } }

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:

// iterate through the list of entries, and print them to the page {

 		out.println("" + rs.getString("nice_date") + "
" + rs.getString("data") + "
");

out.println("<a href=\"/blog/delete_comment.jsp?blogid="+blogid+"&commentid="+rs.getString("comment_id")+"\">Delete Comment</a>

");

 		counter++;

}

Continue in the tutorial to learn about logging in ability!

Personal tools