Java Tutorial - Java Script : Coding, Integration, and Testing

Java Tutorial - Java Script :

Coding, Integration, and Testing

Now that we have decided on Velocity as a template engine for our project, let’s take a detailed look at the development of one of our views for the Old Friends Incorporated application described in Chapter 3. Old Friends Incorporated has a dual business model, one offering is a hosting service for institutions that choose to outsource the development and management of their site to a third party and one that involves selling the software and license outright to the customer, where the customer manages the site themselves. Either way, it is desirable to be able to configure the site for an institution without having to resort to custom programming. Through the use of a properties configuration file and the Velocity template engine, this is easy to manage.

 Consider the default page for an institutional Web site; our example will use Jim’s alma mater, Mullins High School. We have decided to use the java .util.Properties classes to retrieve the institution name from a configuration file named OldFriends.properties. The OldFriends .properties file will reside in the root directory for our application TOMCAT_ROOT \webapps\ROOT.

 OldFriends.properties contains a single entry:

 oldfriends.institution=Mullins High School Tigers

This entry defines our institution as the Mullins High School Tigers. Let’s take a look at our Velocity template OldFriends.vm, which will use this information from the properties file:

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<meta name=”generator” content=”HTML Tidy, see http://www.w3.org/”>

<title>$customer</title>
</head>

<body bgcolor=”#FFFFFF”>
<table width=”100%” height=”100%”>
<tbody>
<tr>
<td height=”20%” bgcolor=”#999999”>
<center>
<h2>$customer</h2>
</center>
</td>
</tr>

<tr>
<td height=”80%”>
<table width=”100%” height=”100%”>
<tbody>

<tr>
<td height=”100%” width=”15%” bgcolor=”#999999”>
<table border=”0” width=”138”>
<col span=”1” align=”center” valign=”middle”>
<tbody>
<tr>
<td height=”27”>UserID</td>
</tr>
<tr>
<td height=”24”>
<form>
<input size=”15” type=”text” name=
“USERID”>
</form>
</td>
</tr
<tr>
<td height=”30”>Password</td>
</tr>
<tr>
<td height=”34”>
<form>
<input size=”15” type=”text” name=
“PASSWORD”>
</form>
</td>
</tr>
<tr>
<td height=”69”>
<form>
<input type=”button” name=”Login”
value=” Login “>
</form>
</td>
</tr>
<tr>
<td height=”69”>
<form>
<input type=”BUTTON” value=”Register”
onclick=
“window.location.href=’/servlet/Register’”>
</form>
</td>
</tr>
<tr>
<td height=”89”>
<form>
<input type=”button” name=
“AboutButton” value=” About “>
</form>
</td>
</tr>
</tbody>
</table>
</td>
<td height=”100” width=”80%” bgcolor=”#cfe7e7”
valign=”top”>
<center>
<h3>Welcome $customer Alumnus!</h3>
</center>
<br>
<br>
<h3 align=”left”>We hope you enjoy YOUR Web site. Registration is free (after all, you provide our content)!<br>
<br>
If you have already registered, please log in using the menu on the left. If you are new to our site, please take the time to register. Registered members have access to the alumni directory, access to our souvenir shop, and many other member privileges!<br><br> Don’t delay! Your long-lost schoolmate is waiting to hear from you!</h3>
<br>
<br>
<br>
<br>
<h3 align=”bottom”>Copyright Old Friends
Incorporated</h3>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>

You will notice the bold references to $customer in the above template; these will be expanded into the name of our institution after it is retrieved from the properties file and put into our context. Let’s take a look at the
OldFriends.java source and see how this is done.

import java.io.*;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
public class OldFriends extends VelocityServlet
{
protected Properties loadConfiguration(ServletConfig config)
throws IOException, FileNotFoundException
{
Properties p = new Properties();
String path = config.getServletContext().getRealPath(“/”);
if (path == null)
{
System.out.println(
“loadConfiguration() : unable to “+
“get the current webapp root. Using ‘/’.”);
path = “/”;
}
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
p.setProperty( “runtime.log”, path + “WEB-INF/oldfriends.log” );
return p;
}