Book Image

Learning Jakarta Struts 1.2: a concise and practical tutorial

By : Stephan Wiesner
Book Image

Learning Jakarta Struts 1.2: a concise and practical tutorial

By: Stephan Wiesner

Overview of this book

<p>Jakarta Struts is an Open Source Java framework for developing web applications. By cleanly separating logic and presentation, Struts makes applications more manageable and maintainable.<br />Since its donation to the Apache Foundation&nbsp; in 2001, Struts has been rapidly accepted as the leading Java web application framework, and community support and development is well established.<br /><br />Struts-based web sites are built from the ground up to be easily modifiable and maintainable, and internationalization and flexibility of design are deeply rooted. Struts uses the Model-View-Controller design pattern to enforce a strict separation between processing logic and presentation logic, and enables efficient object re-use.<br /><br />The book is written as a structured tutorial, with each chapter building on the last. The book begins by introducing the architecture of a Struts application in terms of the Model-View-Controller pattern. Having explained how to install Jakarta and Struts, the book then goes straight into an initial implementation of the book store. The well structured code of the book store application is explained and related simply to the architectural issues.<br /><br />Custom Actions, internationalization and the possibilities offered by Taglibs are covered early to illustrate the power and flexibility inherent in the framework. The bookstore application is then enhanced in functionality and quality through the addition of logging and configuration data, and well-crafted forms. At each stage of enhancement, the design issues are laid out succinctly, then the practical implementation explained clearly. This combination of theory and practical example lays a solid understanding of both the principles and the practice of building Struts applications.</p>
Table of Contents (20 chapters)
Learning Jakarta Struts 1.2
Credits
Preface
About the Book
Glossary
Literature

Chapter 5: Logging and Configuration


Exercise 1

1. How would you rewrite the Singleton class to create only two, only three or only ten instances?

Insert here a HashMap that contains the chosen number of instances.

Here is an example:

Listing A.2: PropertiesManager.java
import java.io.*;
import java.util.*;
public class PropertiesManager implements java.io.Serializable
{
/** The singleton reference */
private static PropertiesSingleton config;
/** Contains the configurations */
private static HashMap configurations = new HashMap();
private PropertiesManager() { }
private Properties load(String configFile) throws
IOException
{
Properties props = new Properties();
DataInputStream in = new DataInputStream(
new BufferedInputStream
(new FileInputStream(configFile)));
props.load(in);
return props;
}
public static PropertiesSingleton getInstance(String
configurationName, String configFile) throws IOException
{
if (config == null) { config = new PropertiesSingleton();
}
Properties p = config.load(configFile);
configurations.put(configurationName, p);
return config;
}
public static PropertiesSingleton getInstance()
{
if (config == null) {throw new RuntimeException(
"No configuration file was provided, yet.");}
return config;
}
public String getProperty(String configurationName,
String key)
{
return ((Properties)
configurations.get(configurationName)).getProperty(key);
}
}

2. It would be nice if configuration files were read automatically at every startup. How can you achieve this?

Include a Method reload (String Filename). This file deletes old entries of Properties-Objects and replaces them with the newly read values.

Exercise 4

1. Why does declaring the error statement help us much less in JSP than it would in a normal class?

The JSP is compiled into a Servlet. The declared statement refers to this servlet. You have an option to check the Servlet and then draw conclusions from your JSP.

2. Set the file path for Log4J to c:/ instead of c:\. Reload the context. What happens to the log file? Where do you see the error message (if at all)?

Unfortunately, the file is not created now and no error message is displayed either.

Exercise 5

  1. 1. The LoggerHTMLFormatter

The following statement is very important:

DateFormat dateFormatter = DateFormat.getDateInstance(
DateFormat.DEFAULT, currentLocale);

A glance in the Java-API shows us this method:

static DateFormat getDateInstance()
Gets the date formatter with the default formatting
style for the default locale.

2. Why does the LoggerInitiator

It is important that it’s declared as private and not so much that it is empty. If it is left out, then the class will automatically get an empty public constructor, and other classes can evade the Singleton mechanism.