CSci 4608 Problem set 4 (project): XML, XSL and XML Schema, Java
Due Monday, December 20th at 8pm
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
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
- user name
- text of the message
- importance (on the scale from 1 to 3), chosen by the user.
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.
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.
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:
- The user name cannot be less than 1 character or longer than 10.
- Subject cannot be longer than 40 characters.
- Message body must be between 1 and 300 characters.
- The message importance must be an integer, one of 1, 2, 3 (even if
your form has a selection menu, you still need to check this because
data may be submitted bypassing the form.
- The date must be of the specified format (whatever you use for
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
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.
- Data sorted by the subject
- Data sorted by the date. Note that the pre-defined xs:date type
doesn't get automatically sorted in XSL. See this
example on how to accomplish the sorting.
- Data selected by the username. You will get extra credit if this one is
done using a parameter-passing mechanism of XSL, but changing the the
value of username in XSL directly would be OK.
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
- Display the initial web page with a form for a user to post a
message and a link to a page with the search options.
- When a user posts the message, the servlet should acknowledge a
successful submission. The posting form and the link to a page with
the search options should be provided.
- The search options should allow the user to display messages
sorted by subject or by date, the user should also be able to select
all messages posted by a specific user (type in the name). You don't
have to provide any combinations of these searches (such as select by
the user name and then by sort by date). The displayed messages must
be color-coded by their importance (see above). All displayed messages
must have a Reply button.
- There is no limit on the number of messages displayed on one page.
- If the user's submission is incorrect (see the validation rules
above), display an error message and allow the user to continue
browsing. You don't need to display a detailed message about the
problem (but you can if you prefer).
servlets-examples. The compiled servlet class
files must be placed into the following subdirectory of
To compile the servlets, you need to use the jar file
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:
assuming that the servlet files are in the same directory as the
javac IceCream.java -classpath .:jakarta-tomcat-5.0.30/common/lib/servlet-api.jar
When you are adding new servlets to the web application, you need to
add the corresponding elements to the the web.xml file in
if I am adding IceCream servlet, I have to add the following
element to the group of servlet elements:
and the following element to the group of servlet-mapping elements:
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
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
- Go to the folder jakarta-tomcat-5.0.30/conf and open the file
tomcat-users.xml in a text editor
- Add a new
user element with any user name and
- Close the file. Type
ls -lt to check the file's
permissions. Some text editors (including emacs) set file permissions
as 'readable for everyone', which is not what you want for the
password file. If you see these permissions, type
tomcat-users.xml to remove the read permissions for the group
- Restart Tomcat so that the update takes affect. After that you can
always use Tomcat Manager to reload the servlet classes, and you don't
need to restart Tomcat after you made a change.
Some odds and ends:
See this example of validating a schema
in a servlet. The example explains how to compile and
"run" such servlets.
- When you are accessing files from servlets (such as xml or xsl
files), the path to the files
must be specified relative to
jakarta-tomcat-5.0.30/bin. However, it's probably not a
good idea to store your files directly in bin, creating a subfolder
within servlets-examples would be better.
- Do not use System.exit() in your servlets, this would stop
- In a real-life application file locking would be an issue
(servlets are multi-threaded, and writing to a file in two servlets
simultaneously might cause inconsistencies). You don't need
to handle file locking in this problem. However, it's a good idea to
make a copy of your XML file every once in a while in case there are
some simultaneous writes to the same file.
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.