CSci 4608 Problem set 4 (project): XML, XSL and XML Schema, Java Servlets

Due Monday, December 20th at 8pm

General requirements

For this project you will implement a web site that hosts a simple message board using servlets and XML. The web site will consist of just a couple of pages. A user will be able to post messages and read messages posted earlier. No login is required, but you need to maintain a session using the HttpSession object (you don't need handle cookies explicitly).

A message can be posted using a form (with a POST method). The form has the following fields:

Messages are stored in an XML file. In addition to the above information, the file should contain the date generated automatically from the System time. The date can be a string containing mm-dd-yyyy (or a similar format of date, choose your own if you prefer) or a type xs:date, whatever works better for sorting (see below). Don't worry about adding time to it, working with just dates will be sufficient.

A user should be able to display the message ordered by subject (in alphabetical order) or by date. They should also be able to select messages posted by a specific user. The messages should be color-coded based on the importance (choose your color scheme and explain it on the web page). The importance doesn't affect the order in which the messages are displayed.

If a user posts more than one message in one session, the username for all messages after the first one gets filled in automatically (use session attributes to store the user name).

A user should be able to reply to other messages by clicking a reply button next to the message. In this case the subject of the message gets filled in automatically (note that this means that each message is displayed as a form). It's OK to send the subject as a hidden form field in this case.

Specifics

Writing XML schema

Before adding a message to the XML file, you need to validate it against an XML schema (or, strictly speaking, you have to add it to a temporary XML file, check that file, and if it checks out OK, replace the old file by the new one). If the message doesn't satisfy the schema, display an error message to the user. Do not quit the servlet! The user may continue browsing or try to post another message. This example shows how you can validate an XML document against a schema in a Java program.

Your schema must check the following for each message:

Start your project by writing the schema for the XML document containing messages, write a sample XML file (or two, or three) and validate the file(s) against the schema.

Server-side XSL processing

Write XSL stylesheets to display the messages, i.e. to convert an XML file to an HTML file. You need at least three XSL files: The importance of the message should be used for color-coding, but should not be displayed.

Use the command-line option for running Xalan (the XSL parser) or use this example to run it from a Java program. The second option is better since you get familiar with Xalan API.

Try out your stylesheets and make sure they work.

Writing the servlets

Your task for this part is to write servlets that will generate and handle the forms. Specifically, your servlets should do the following: Technical details. The servlets should be a part of the default web application servlets-examples. The compiled servlet class files must be placed into the following subdirectory of jakarta-tomcat-5.0.30:

webapps/servlets-examples/WEB-INF/classes
To compile the servlets, you need to use the jar file servlet-api.jar in jakarta-tomcat-5.0.30/common/lib directory. Either include the absolute path to this file in your CLASSPATH or specify the absolute or relative path to this file when compiling. For instance, in my case servlet files would be compiled like this:

javac IceCream.java -classpath .:jakarta-tomcat-5.0.30/common/lib/servlet-api.jar
assuming that the servlet files are in the same directory as the jakarta-tomcat-5.0.30 folder.

When you are adding new servlets to the web application, you need to add the corresponding elements to the the web.xml file in jakarta-tomcat-5.0.30/webapps/servlets-examples/WEB-INF. For instance, if I am adding IceCream servlet, I have to add the following element to the group of servlet elements:


    <servlet>
        <servlet-name>IceCream</servlet-name>
        <servlet-class>IceCream</servlet-class>
    </servlet>
and the following element to the group of servlet-mapping elements:

    <servlet-mapping>
        <servlet-name>IceCream</servlet-name>
        <url-pattern>/servlet/IceCream</url-pattern>
    </servlet-mapping>
You only need to do it the first time you are adding a servlet. You will need to reload the application (see instructions for Tomcat Manager below). You should be able to access your servlets at

http://localhost:8080/servlets-examples/servlet/MyServlet 

If you have modified an existing servlet, i.e. replaced its .class file by a newer version, try reloading the page to start the new version. If this doesn't help, you can use Tomcat manager to reload classes without having to restart Tomcat. You need to set up a manager's password before you can use the Tomcat manager:

To use Tomcat Manager, go to the front page localhost:8080, click on Tomcat Manager, and type in the manager's user name and password. You will see the table with all web applications. Click "reload" for servlets-examples, this will update all the servlets to the new .class files.

Some odds and ends:

See this example of validating a schema in a servlet. The example explains how to compile and "run" such servlets.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.