Articles : Incorporate Logging within JSP :

Setting the Environment

Once you have followed the instructions in the "Installing Log Tag Library" section, you are all ready to use the Log tag library from within your JSP. Before you can see the example JSP in Listing 1 in action, you need to configure your web server to use your JSP and the log4j configuration file as described in the next few points.

  • First, put the JPS described in Listing 1 to a folder named TestLogTags (or any folder of your choice) under the webapps directory of your Tomcat installation.
  • In Tomcat, any web application typically contains a WEB-INF directory and one WEB-INF/classes directory. Place the "log4j.properties" file in one of these locations. For example, you may decide to put it in the WEB-INF/classes directory.
  • You need to tell Tomcat where to find this configuration file for the log4j.
  • You can pass the "log4j.properties" file as a system variable to the execution environment of tomcat.
  • Go to the "tomcat.bat" file in %TOMCAT_HOME%\bin.
  • Add an entry to set the classpath variable pointing to the directory containing the "log4j.properties" file. For this example, it may read like:

    set CP=%CP%;C:\Jakarta-tomcat-3.2.1\webapps\ TestLogTags\WEB-INF\classes

  • Add an entry to the "tomcat.bat" file:

    set TOMCAT_OPTS=-Dlog4j.configuration=log4j.properties

  • Startup tomcat and it will load the "log4j.properties" file and Log will use this configuration file to print logging information.

This is all you require to see the use of Log example in action.

The Log Example in Action

You can now start up your Tomcat and access the JSP page. Figure 1 presents a screenshot of the JSP.

Enlarge Picture

Figure 1: The JSP using the Log tag library

If you now look into the Tomcat console, you will see the logging information getting printed.

DEBUG[Thread-11](LoggerTag.java:109)-Message embedded within open and close tags. DEBUG[Thread-11](LoggerTag.java:97)-Message passed as an attribute to the tag INFO[Thread-11](LoggerTag.java:109)-Using category attribute.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.