Letters to the editor

1 2 Page 2
Page 2 of 2

If you implement it that way, you run the risk of it being garbage-collected. In their book

Java in Practice,

(Addison Wesley, 1999) Nigel Warren and Philip Bishop address this problem. They suggest two solutions:

  1. Run the program with the -noclassgc option
  2. Create a singleton registry

Obviously, number 1 isn't the best solution; you basically suggest number 2. A third, slightly different approach is to obtain singleton references from factory instances. That is similar to the repository, in that you have to keep the factory instance alive somewhere. However, it is a bit more obvious that you need to keep it alive. With the static method, a programmer is allowed to become lazy. I'm thinking along these lines:

 
public class SingletonFactory
{
    public SingletonFactory() {}
 
    public Singleton getInstance()
    {
        synchronize (SingletonFactory.class)
        {
            if( _instance == null)
            {
                _instance = Singleton.getInstance();
            }
        }
        return _instance;
    }
 
    private static Singleton _singleton;
}

Now, throughout the program, use the factory instance to retrieve the singleton on which you want to perform some action. I'm unsure if this is really much better than just keeping a singleton in a member somewhere. However, it is another option. Tony Sintes

Java Print API

'Printing in Java'

Part 1: Acquaint yourself with the Java printing model

Part 2: Print your first page and render complex documents

Part 3: Jean-Pierre Dubé introduces the print framework that works on top of the Java Print API

Jean-Pierre Dubé

Printing reports in HTML

Jean-Pierre,

Thanks for your series of printing-related articles. I am looking for advanced printing techniques/capabilities as part of a Web-based reporting project.

The application's architecture is MVC (servlet: JavaServer Page (JSP), model 2). The reports are basically JSP, and I want to make a print option available. I would like to include some page formatting from the application, like setting the header, footer, and page number, and printing the cover page with summary details (e.g., total pages). Also, when the user prints, I don't want the information that Netscape and Internet Explorer include on the pages (title, URL, page number, date) to print.

Please provide me with some suggestions.

Atchutarao Killamsetty

Atchutarao, Trying to create reports in HTML is a real pain, because HTML has no concept of a physical page. To achieve your goal, you need to be able to convert from one representation to another. For example your source could be XML, and your program could convert to HTML, PDF, etc. I think that the print framework presented in this series will help you. Stay tuned for more articles. Jean-Pierre Dubé

Related:
1 2 Page 2
Page 2 of 2