The Java Web Start application sandbox lets users run applications from the Web with minimal access to system resources, such as a printer. This sandbox protects users’ systems from damage caused by malicious applications.

However, when an application developer and the user both want the application to have access to the printer, the sandbox is more of a frustrating chaperone than a valued protector. Let’s take a look at how to get pages past the sandbox and into the printer.

As discussed in part two of my Web Start series, there are two principle ways to circumvent the sandbox. You can either sign your code (as explained in that article), which grants it full permissions to users’ computers if they’re willing to grant them, or you can use the Web Start services. Code signing is the easier solution, since it requires no modification of your source code, but if a user is unwilling to grant your application full permissions to run on his or her system, you’re out of luck.

Printing inside the sandbox
On the other hand, the Web Start services let you ask the user for permission in small doses and only at times you need them. A signed application asks the user on launch, “Can I do anything I want?” An application using the Web Start PrintService says, “Can I print this page?” and usually does so only when the user has already indicated a desire to print.

Using the Web Start PrintService couldn’t be easier. The PrintService interface makes available just a few methods, only two of which we need to look at to actually get documents to paper. However, before we look at the methods offered by the PrintService, let’s first look at getting an instance of the PrintService.

In source Listing A, we see the code necessary to obtain an object implementing the Javax.jnlp.PrintService interface. The name of the class returned depends on the implementation provided by the specific Web Start application, but as long as it’s guaranteed to offer the PrintService interface, we’ve got all we need.

The first methods of interest to us in our newly obtained PrintService object are the getDefaultPage() and showPageFormatDialog() pair. getDefaultPage() returns the current system default printed page layout. The returned java.awt.print.PageFormat object contains information about the size and orientation of the page to be printed. The showPageFormatDialog method asks the user to alter the current default PageFormat. Upon calling this method, the system standard page orientation dialog will be displayed. If the user alters the settings, they will be reflected in the returned PageFormat.

The last method to look at provides the meat of the print service, print(). When print() is called with java.awt.print.Printable or a java.awt.print.Pageable as its sole parameter, the user sees a dialog box asking them to grant approval for the print job. This user interaction allows the PrintService to play nicely in the Web Start sandbox. Without the user check, a rogue application could start a printer spewing pages. But with this safeguard, the user can decide if they want each print job to find its way to the page.

Putting it all together
In source Listing B, you can see the code from Listing A and the methods discussed above. This example takes a Printable page and prints it using the user specified PageFormat. Notice the creation of a java.awt.print.Book object as required by the Java 2 Printing API, regardless of whether you use the Web Start PrintService at all.

Java Web Start provides a secure, easy solution for deploying Java applications. And when the need arises, you can still access resources, such as the printer, without leaving the security of the sandbox.