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

Java Tutorial - Java Script :

Coding, Integration, and Testing

public Template handleRequest(
HttpServletRequest request,
HttpServletResponse response,
Context ctx
)
{
try
{
Properties p = new Properties();
String path = getServletContext().getRealPath(“/”);
FileInputStream is =
new FileInputStream(path + “OldFriends.properties”);
p.load(is);
String customer = p.getProperty(“oldfriends.institution”);
if (customer == null)
{
customer = “Unable to” + “ find OldFriends.properties”;
}
is.close();
ctx.put(“customer”, customer);
} catch (IOException ioe)
{
ctx.put(“customer”,”Exception:Contact “ + “Administrator”);
}
// get the template
Template out = null;
try
{
out = getTemplate(“OldFriends.vm”);
} catch (ParseErrorException pe)
{
System.out.println(“OldFriends : Error parsing” +
“template “ + pe);
}
catch (ResourceNotFoundException rnf)
{
System.out.println(“OldFriends : template not “ + “found “ + rnf);
}
catch (Exception e)
{
System.out.println(“Error “ + e);
}
return out;
}
}

Let’s take a look at what is happening here. The first thing that occurs is thatthe loadConfiguration method is invoked by init() of the superclass VelocityServlet. It creates a Properties object that contains the contents of the file velocity.properties. The Properties object is used by the Velocity- Servlet’s init() method during setup. It also ensures that the path information for the velocity.properties file and the directories named in Velocity loader path are relative to the Tomcat application home directory. The handleRequest method provides the HttpRequest and HttpResponse objects along with the Context object from the Velocity superclass. This method retrieves the institution name from our OldFriends.properties file and puts it into our context. The method then retrieves the template file and merges the context and template through macro expansion and emits the resultant output to the user’s browser.

Let’s take at look at the finished product, but before we do, make sure your web.xml file is properly configured and in the WEB-INF directory. Here it is

: <?xml version=”1.0” encoding=”ISO-8859-1”?> <!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>
<servlet>
<servlet-name>OldFriends</servlet-name>
<servlet-class>OldFriends</servlet-class>
<init-param>
<param-name>properties</param-name>
<param-value>/velocity.properties</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OldFriends</servlet-name>
<url-pattern>/OldFriends/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/Register/*</url-pattern>
</servlet-mapping>
</web-app>

Now we are ready to execute our URL; to do so use the http://localhost command. In Figure 6.11, you can see that our template engine worked flawlessly. We were able to completely separate the delicate presentation work (working with those nasty tables) from the data acquisition code (setting up retrieval of the data from the properties file). Both teams were able to work independently from a set of specifications without having to step on each other’s toes during development. Because this is so much fun, let’s take a look at what the registration module would look like!
In order to gain membership, we require applicants to fill out a registration form with their personal information. Access to the registration screen is from a button on the main menu on the left side of the page (the button is visible in Figure 6.11). The registration template, Register.vm, looks like this:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<meta name=”generator” content=”HTML Tidy, see www.w3.org”>
<title>$customer</title>
</head>
<body bgcolor=”#999999”>
<table width=”100%”>
<tbody>
<tr valign=”top”>
<!-- row 2 -->
<td colspan=”4”>
<table border=”0” width=”100%”>
<col span=”1”>
<col span=”1” align=”center”>
<tbody>
<tr valign=”middle” align=”center”>
<td valign=”bottom” align=”center”>
<form>
<input type=”BUTTON” value=”Home” onclick=
“window.location.href=’OldFriends’”>
</form>
</td>
<td valign=”top” align=”center”><font size=
“-1”><font size=”5”>Registration for $customer
Alumnus and Faculty</font></font></td>