A Java Blog Application

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

From Humanitarian-FOSS Project Development Site

This tutorial will demonstrate how to bring together the skills learned in the previous tutorial [assuming that you have completed it] to create a web blog. It assumes you have downloaded and installed Apache Tomcat in /usr/apps/ folder . Also, you need to make sure that tomcat is running.

Contents

File Structure

For this tutorial, you should first create a new Context for the application. Do this by adding these lines to your server.xml file in your apache-tomcat-{version}/conf directory [create a new one if it doesn't exist]:

<Context path="/blog" docBase="blog" debug="0" reloadable="true">
  <Logger className="org.apache.catalina.logger.FileLogger"
                   prefix="localhost_blog_log." suffix=".txt"
         timestamp="true"/>
</Context>

Next, you should create a new directory named blog, in your Tomcat webapps directory and then copy the MySQL JDBC driver into the lib directory for your new application:

vagrawal@localhost:~$ cd /usr/apps/apache-tomcat-{version}/webapps
vagrawal@localhost:webapps$ mkdir blog
vagrawal@localhost:webapps$ mkdir blog/WEB-INF
vagrawal@localhost:webapps$ mkdir blog/WEB-INF/classes
vagrawal@localhost:webapps$ mkdir blog/WEB-INF/classes/jblog
vagrawal@localhost:webapps$ mkdir blog/WEB-INF/lib
vagrawal@localhost:webapps> 
cp mine/WEB-INF/lib/mysql-connector-java-3.1.10.bin.jar blog/WEB-INF/lib/

Java Servlet

Create the configuration file web.xml in your WEB-INF directory with the following context:


<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>


    <!-- General description of your web application -->

    <display-name>JBlog</display-name>
    <description>
      Simple Servlet Web log Example
    </description>

    <context-param>
      <param-name>webmaster</param-name>
      <param-value>your@email.address</param-value>
      <description>
        The EMAIL address of the administrator to whom questions
        and comments about this application should be addressed.
      </description>
    </context-param>

    <servlet>
      <servlet-name>Write</servlet-name>
      <description>
      Write web log data
      </description>
      <servlet-class>jblog.Write</servlet-class>
    </servlet>

    <servlet>
      <servlet-name>Show</servlet-name>
      <description>
      Show Web log
      </description>
      <servlet-class>jblog.Show</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>Write</servlet-name>
        <url-pattern>/Write</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Show</servlet-name>
        <url-pattern>/Show</url-pattern>
    </servlet-mapping>


</web-app>

The next step is to create a table for log entries, as well as a table to hold a simple password. Since this is just an example application, we’re only including the simplest possible security. Anyone who goes to /blog/add_data.jsp and enters a password which is stored in the passwords table will be allowed to add an entry to the log. Create the tables with the following in phpMyAdmin(find the database, go to SQL tab):


mysql> use blog; //if you are in the database already, this line is unnecessary

mysql> create table blog 
(entry_id int not null auto_increment primary key, data blob, created timestamp not null);

mysql> create table blog_pass 
(password_id int not null auto_increment primary key, password varchar(20));

mysql> insert into blog_pass (password) values ("hushhush");

Java Server Pages (JSPs)

Next, create a JSP page in webapps/blog called add_data.jsp. It will contain a form for entering your web log data:

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

<form action="/blog/Write">
Log Entry:<br>
<%
        String data = request.getParameter("data");
%>

<textarea name="data" rows="10" cols="60">
<%= data != null ? data : "" %></textarea>
<br>
Password: <input type="text" name="password" value="" size="30">
<br>
<input type="submit" value="Submit">
</form>

</body></html>


Next you can create a generic MySQL connector object in blog/WEB-INF/classes/jblog, called MySQLConnector.java. This object will be used by the Write and Show servlets below.


The Finishing Touch: More Servlets

Now it’s time to create a servlet that knows how to write log entries to the database table. Create a file called Write.java in your classes/jblog directory. This will allow you to actually write entries that will be recorded in the blog.

The final piece of this application is a servlet that can show log entries. This servlet will show 10 entries at a time, starting with the latest, and knows how to link back to the beginning once the user has seen all entries.

Create a file called Show.java in your classes/jblog directory.

Once you’ve re-started Tomcat (remember, you’ve added a new Servlet Context and registered several new servlet mappings), added the tags for each servlet in web.xml, and compiled the .java files, you should have a simple but functional web log.

Go to http://localhost:8080/blog/Show to test out your flashy new web log!

Making a Better Weblog

To give this simple web log better functionality and more features, do the next step in this tutorial.

References

Personal tools