15.5 Using Commons Logging in JSP Pages


We've covered how to use the Commons Logging API within Java components, but we haven't yet discussed how to use them in JSP pages. There are a number of ways to use the library within JSP pages, but we'll just cover the two easiest approaches here.

The first approach is to use the same three steps defined earlier, this time performing them in the JSP page itself:

  1. Import the Commons Log and LogFactory classes.

  2. Define and initialize a logger variable for the page.

  3. Start logging.

Example 15-8 illustrates this approach in a basic JSP page.

Example 15-8. Using Commons Logging in a JSP page
<%@ page import="org.apache.commons.logging.Log" %> <%@ page import="org.apache.commons.logging.LogFactory" %> <%-- Get a reference to the logger for this class --%> <% Log logger = LogFactory.getLog( this.getClass( ) ); %> <% logger.debug( "This is a debug message from a jsp" ); %> <html> <head>   <title>Using Commons Logging in a JSP page</title> </head> <body>   <% logger.info( "This is another log message in the jsp" ); %>      There should be two log messages in the log file. </body> </html>

You must have the Commons Logging environment configured properly for this to work, just as when using it in the Java classes. Any JSP page that is part of the web application will be able to use the logging utilities. Because most containers use a different class loader for each web application, any JSP page that is not part of the log4j- configured web application may not be able to use the logging utilities.

Although Example 15-8 shows just how easy it can be to use the Commons Logging API in your JSP pages, there are a few issues with this approach. The most obvious one is that your JSP pages will contain Java code. As mentioned several times in this book, many developers see this as something that should be avoided. Fortunately, there is another way.

The Jakarta Taglibs project contains a custom JSP tag library designed for log4j called the Log tag library. You can view information and download the tag library from the Jakarta Taglibs web site at http://jakarta.apache.org/taglibs/doc/log-doc/ intro.html.

Just like any other custom tag library, you must properly configure it for your web application. This means putting the log.tld file into the WEB-INF directory and installing the Log tag JAR file into the WEB-INF/lib directory. You will also need to add the appropriate taglib element to your web application's deployment descriptor, similar to how you added the Struts tag libraries:

<taglib>   <taglib-uri>/WEB-INF/log.tld</taglib-uri>   <taglib-location>/WEB-INF/log.tld</taglib-location> </taglib>

Once the Log custom tag is installed and configured for your web application, you can use it in your JSP pages as shown in Example 15-9.

Example 15-9. An example JSP page using the Log tag
<%@ taglib uri="/WEB-INF/log.tld" prefix="logger" %> <logger:debug message="This is a debug message from a jsp using the Log tag" /> <html> <head>   <title>Using the Log Tag in a JSP page</title> </head> <body>   <logger:info message="This is another message using the log4j tag" />   There should be two log messages in the log4j log file. </body> </html>

Notice that the JSP page in Example 15-9 contains no Java code. Although it doesn't look too different from Example 15-8, this approach is much cleaner with a larger and more complex JSP page.

Another nice feature of the Log tag is that it gives you the ability to perform a dump of objects stored at the page, request, session, or application scope. This is very useful when you are in the middle of debugging your web application. Example 15-10 illustrates how easy this is to do.

Example 15-10. Using the Log tag library to dump information
<%@ taglib uri="/WEB-INF/log.tld" prefix="logger" %> <html> <head>   <title>Using the Log Tag in a JSP page</title> </head> <body>   <logger:dump scope="page"/>   <logger:dump scope="request"/>   <logger:dump scope="session"/>   <logger:dump scope="application"/>   The page, request, session, and application dumps should be in the log file. </body> </html>

Unfortunately, the Log tag doesn't yet work with the Commons Logging package. It depends on the log4j implementation. Still, it's a valuable tag if you need to provide additional debugging in your JSP pages.




Programming Jakarta Struts
Programming Jakarta Struts, 2nd Edition
ISBN: 0596006519
EAN: 2147483647
Year: 2003
Pages: 180

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net