10.10. ServletContext

Another important class is the ServletContext. This class allows access to servlets that can pose a potential security risk but at times may be necessary, and offers other useful functions. Sun provides a convenient way to access this context, through the getServletContext(), as shown below:

ServletContext scont = this.getServletContext();

The ServletContext provides a variety of information that is useful for debugging (such as logging an error condition to a server log), as well as for diagnostic purposes. These methods are shown in Table 10-6.

The logging methods allow entries to be made in the server-wide servlet log file. You can provide a simple message, or a message with an exception, such as this:

 public void ...

Get Java™ Network Programming and Distributed Computing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.