Java Tutorial - Java Script :
Coding, Integration, and Testing
<tr>
<td width=”35%”><input type=”submit” name=
“SUBMIT” value=”Submit”></td>
<td width=”65%”><input type=”reset” name=
“CANCEL” value=”Cancel”></td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
On this page, we are using the customer object and a date object, which is being placed in the Velocity context by our Register servlet. Following is the source for Register.java:
import java.util.Date;
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 Register 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 + “oldfriends.log” );
return p;
}
public Template handleRequest(
HttpServletRequest request,
HttpServletResponse response,
Context ctx )
{
try
{
Properties p = new Properties();
String path = getServletContext().getRealPath(“/”);
FileInputStream is = new FileInputStream(path +
“WEB-INF/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);
Date dte = new Date();
ctx.put(“date”,dte.toString());
}
catch (IOException ioe)
{
ctx.put(“customer”, “Exception - Contact Administrator”);
}
// get the template
Template out = null;
try
{
out = getTemplate(“register.vm”);
}
catch (ParseErrorException pe)
{
System.out.println(“Register : Error parsing template “ + pe);
}
catch (ResourceNotFoundException rnf)
{
System.out.println(“Register : template not found “ + rnf);
}
catch (Exception e)
{
System.out.println(“Error “ + e);
}
return out;
}
}
You can see that we have added a “date” element to our context for this screen. The java.util.Date() library is used to format a Date object and store it in our context. We can now execute the registrations screen from our
main page. The result is shown in Figure 6.12. The examples covered here only scratch the surface of the capabilities of Velocity. What hasn’t been demonstrated is the fact that the Velocity engine can be used for far more than generation of Web pages. For example, Velocity is used in Torque (another Jakarta project) to generate database-specific SQL. It can also be used to create XML or PostScript files. In XDoclet, Velocity is even used to generate Java programs. Velocity however has a new competitor in the Web-page-generation arena, and it is not a template engine.
