Java 2 Network Security



Figure 2. (Part 2 of 2). GetPrintJob.java Applet Source Code
This is the code of an applet that, once downloaded on your system, does
nothing but displays a button. If you push the button, the applet attempts to
get a PrintJob object, which results in initiating a print operation on the
toolkit's platform.

In JDK 1.0, this operation would not have been allowed to a remote applet, by
default considered untrusted. In JDK 1.1, the remote applet should have been
signed and the signature considered as trusted. However, once granted the
permission to access your system resources, that applet could do everything
a local code would be allowed to do, not only print to a printer. The
fine-grained access control implemented by the Java 2 security model gives
you the possibility to grant only the permission to print (since this is the only
permission this applet requires) and only to the code you trust.

The applet above can be invoked by a very simple HTML page, such as the
following one:

add(b, BorderLayout.CENTER);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
try
{
Toolkit.getDefaultToolkit().getPrintJob(null, "PrintJob", null);
}
catch(Exception e)
{
System.out.println("There was an exception, "+ e.toString());
p=false;
}
if (p)
System.out.println("No exception. Test is successful.");
}
public void paint(Graphics g)
{
new GetPrintJob();
}