Article

All Java, No Froth: 6 Easy Steps to MVC Web Apps

Page: 1 2 3 4 5 6 Next

The code for processing deleted items is very similar, except that it must convert the submitted string into an integer for the to-do list's deleteItem() method:

   String deleteid = request.getParameter("deleteid");  
   if (deleteid != null) {  
     try {  
       toDoList.deleteItem(Integer.parseInt(deleteid));  
 
       // Redirect to self  
       response.sendRedirect(request.getRequestURI());  
       return;  
     }  
     catch (NumberFormatException e) {  
       throw new ServletException("Bad deleteid value submitted.", e);  
     }  
   }

The conversion is achieved by the Integer.parseInt() method but, if the value submitted cannot be converted into a number, it will throw a NumberFormatException. If this happens, our code catches the exception and throws a ServletException (to which we pass the NumberFormatException as a root cause).

Remember, all standard servlet methods are allowed to throw ServletExceptions to let the server know something has gone wrong. That's exactly what we're doing here. When the code throws the ServletException, the server running the servlet catches it and displays an appropriate error message in the user's browser. You can configure most Java servers to control how much technical detail is included in such error pages.

Believe it or not, that's it for the servlet! You still have to compile it, though, and that's a little tricky. You see, the HttpServlet class that our servlet extends is included in the libraries distributed with every Java server, and you need that class to be able to compile your subclass.

In Tomcat 5.x, the servlet library is called servlet-api.jar and it can be found in the common\lib directory of your Tomcat installation. So you can compile your servlet by including that JAR file in the classpath:

javac -classpath  
".;c:\Program Files\Apache Group\Tomcat 5.5\common\lib\servlet-api.jar"  
com/sitepoint/*.java uk/co/anyware/html/*.java

Here's what your file and directory structure should look like now:

/WEB-INF/classes/com/sitepoint/ToDoItem.class  
/WEB-INF/classes/com/sitepoint/ToDoItem.java  
/WEB-INF/classes/com/sitepoint/ToDoList.class  
/WEB-INF/classes/com/sitepoint/ToDoList.java  
/WEB-INF/classes/com/sitepoint/ToDoServlet.class  
/WEB-INF/classes/com/sitepoint/ToDoServlet.java  
/WEB-INF/classes/uk/co/anyware/html/HTMLEscaper.class  
/WEB-INF/classes/uk/co/anyware/html/HTMLEscaper.java  
/WEB-INF/lib/mysql-connector-java-version-bin.jar

We have two things left to add to complete our Java web application. The first is the styles.css style sheet referenced by our servlet:

body, p, td, th {  
 background: #fff;  
 color: #000;  
 font: medium Verdana, Arial, Helvetica, sans-serif;  
}  
 
select {  
 width: 100%;  
}

Drop this in the main application directory, which also contains the WEB-INF directory.

The other thing we need is the XML configuration file for the application, web.xml. This file is called the deployment descriptor. It must start with a <web-app> tag like this one (again, any decent IDE will write this boilerplate code for you):

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  
   version="2.4">

Next up, we provide a name for our web application:

 <display-name>ToDoServlet</display-name>

Now we'll configure our servlet. We give it a name and specify its class:

 <servlet>  
   <servlet-name>toDo</servlet-name>  
   <servlet-class>com.sitepoint.ToDoServlet</servlet-class>

Now, remember the init() method of our servlet used two initialization parameters: one for the JDBC driver class name, and one for the JDBC connection string. We provide values for those parameters here:

   <init-param>  
     <description>The JDBC driver class.</description>  
     <param-name>jdbcDriver</param-name>  
     <param-value>com.mysql.jdbc.Driver</param-value>  
   </init-param>  
   <init-param>  
     <description>The JDBC connection string.</description>  
     <param-name>jdbcConnectionString</param-name>  
     <param-value>jdbc:mysql://localhost/todo?user=root&amp;password=password</param-value>  
   </init-param>  
 </servlet>

Finally, we supply a servlet mapping, which sends requests to a given URL to the servlet. For our example, we'll direct requests for index.html in the root of our application to our servlet:

 <servlet-mapping>  
   <servlet-name>toDo</servlet-name>  
   <url-pattern>/index.html</url-pattern>  
 </servlet-mapping>  
</web-app>

That's it for our application! Here's the complete list of files:

/styles.css  
/WEB-INF/web.xml  
/WEB-INF/classes/com/sitepoint/ToDoItem.class  
/WEB-INF/classes/com/sitepoint/ToDoItem.java  
/WEB-INF/classes/com/sitepoint/ToDoList.class  
/WEB-INF/classes/com/sitepoint/ToDoList.java  
/WEB-INF/classes/com/sitepoint/ToDoServlet.class  
/WEB-INF/classes/com/sitepoint/ToDoServlet.java  
/WEB-INF/classes/uk/co/anyware/html/HTMLEscaper.class  
/WEB-INF/classes/uk/co/anyware/html/HTMLEscaper.java  
/WEB-INF/lib/mysql-connector-java-version-bin.jar

You're now ready to bundle this application up so that it's ready to be deployed on a Java-capable server. To do this, you need to use the jar utility included with the JDK. If you've added the bin directory of your JDK to your system path, you should be able to run it from the command prompt just by typing jar.

Move to your working directory for the application (the one that contains styles.css and WEB-INF), and type this command:

jar cvf ToDoServlet.war .

This will produce a file called ToDoServlet.war containing your application. It's actually a ZIP file in disguise.

You now need to deploy this application on your server. Different servers provide different ways of doing this. If you're using Tomcat, simply copy the ToDoServlet.war file into the webapps directory of your installation. After a few moments, Tomcat will automatically extract the file into a new directory. You can then go and edit the web.xml file to make sure the JDBC connection string contains the correct details for your database server.

Once that's done, you should be ready to run your application. Load up http://localhost:8080/ToDoServlet/index.html in your browser (assuming Tomcat is running on port 8080 on your local machine). Here's what you should see:

1507_todoservlet

Have a play with the page. It should work just as you'd expect. Here's the WAR file (complete with source code if you unzip it):

Download the code. (250KB)

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links