Search This Blog

Friday, December 18, 2009

Logger in JSP

Apply log4j within jsp.


Information
none

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Tomcat
Jakarta log tag library
Log4j 1.2.9


Procedure

1. Download the Jakarta log tag library: jakarta-taglibs-log-1.0.zip

2. Extract this archive file, e.g.: C:\Tools\jakarta-taglibs-log-1.0

The archive contains the following files:

LICENSE
log-doc.war
log-examples.war
README
taglibs-log.jar
taglibs-log.tld

3. Download Log4j 1.2.9: logging-log4j-1.2.9.zip

4. Extract this archive file, e.g.: C:\Tools\logging-log4j-1.2.9

The archive contains serveral files. The most important one is: C:\Tools\logging-log4j-1.2.9\dist\lib\log4j-1.2.9.jar

5. Install Tomcat. Follow guide "Installing Tomcat 4.1.31".

6. Create a simple a Tomcat web application called "demo":

* Create the following directories inside the Tomcat "webapps" directory:

C:\Tools\Tomcat 4.1\webapps\demo
C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF
C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF\classes
C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF\lib

* Copy the tag library descriptor file "taglibs-log.tld" into WEB-INF.

* Copy the tag library JAR file "taglibs-log.jar" into WEB-INF/lib.

* Copy the log4j JAR file "log4j-1.2.9.jar" into WEB-INF/lib.

* Create a web.xml file in the WEB-INF directory:


PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">


Demonstration log4j usage in jsp

index.jsp


http://jakarta.apache.org/taglibs/log-1.0
/WEB-INF/taglibs-log.tld



Note: You MUST add a element in the web.xml file:


http://jakarta.apache.org/taglibs/log-1.0
/WEB-INF/log.tld


* Create a log4j.properties file in the WEB-INF/classes directory:

# ***** Set root logger level to DEBUG and its two appenders to stdout and R.
log4j.rootLogger=debug, stdout, R

# ***** stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# ***** stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%c] %p - %m%n

# ***** R is set to be a RollingFileAppender.
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/demo.log
# ***** Max file size is set to 100KB
log4j.appender.R.MaxFileSize=100KB
# ***** Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# ***** R uses PatternLayout.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%c] %p - %m%n

Note:
The location of the log file is set in:
log4j.appender.R.File=${catalina.home}/logs/demo.log



* Create two jsp files in the C:\Tools\Tomcat 4.1\webapps\demo directory:

File 1: index.jsp

<%@ taglib uri="http://jakarta.apache.org/taglibs/log-1.0" prefix="log" %>



Demonstration log4j usage in jsp





Show DEBUG message.
Show INFO message.
Show WARN message.
Show ERROR message.
Show FATAL message.


Message embedded within the open and close tags.


Using category attribute.



The log messages are shown in the Tomcat console and in the
${catalina.home}/logs/demo.log file.





File 2: test.jsp

<%@ page import="org.apache.log4j.Logger" %>



Demonstration log4j usage in jsp




<%
Logger log = Logger.getLogger("com.mobilefish.demo.test");
log.debug("Show DEBUG message");
log.info("Show INFO message");
log.warn("Show WARN message");
log.error("Show ERROR message");
log.fatal("Show FATAL message");
%>


The log messages are shown in the Tomcat console and in the
${catalina.home}/logs/demo.log file.





7. (Re)start Tomcat.

8. Access the index.jsp file:

http://localhost:8080/demo/index.jsp

9. The following log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.

2006-06-03 17:28:43,379 [root] DEBUG - Show DEBUG message.
2006-06-03 17:28:43,409 [root] INFO - Show INFO message.
2006-06-03 17:28:43,409 [root] WARN - Show WARN message.
2006-06-03 17:28:43,409 [root] ERROR - Show ERROR message.
2006-06-03 17:28:43,419 [root] FATAL - Show FATAL message.
2006-06-03 17:28:43,419 [root] FATAL - Message embedded within the open and close tags.
2006-06-03 17:28:43,419 [root] FATAL - Message passed as an attribute to the tag
2006-06-03 17:28:43,419 [com.mobilefish.demo.index] FATAL - Using category attribute.

10. Access the test.jsp file:

http://localhost:8080/demo/test.jsp

11. The following log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.

2006-06-03 17:55:43,379 [com.mobilefish.com.test] DEBUG - Show DEBUG message.
2006-06-03 17:55:43,409 [com.mobilefish.com.test] INFO - Show INFO message.
2006-06-03 17:55:43,409 [com.mobilefish.com.test] WARN - Show WARN message.
2006-06-03 17:55:43,409 [com.mobilefish.com.test] ERROR - Show ERROR message.
2006-06-03 17:55:43,419 [com.mobilefish.com.test] FATAL - Show FATAL message.

12. Change the root logger level to WARN in the log4j.properties file and restart Tomcat:

log4j.rootLogger=warn, stdout, R

When the index.jsp page is reloaded in your browser only the following messages are shown:

2006-06-03 17:48:43,409 [root] WARN - Show WARN message.
2006-06-03 17:48:43,409 [root] ERROR - Show ERROR message.
2006-06-03 17:48:43,419 [root] FATAL - Show FATAL message.
2006-06-03 17:48:43,419 [root] FATAL - Message embedded within the open and close tags.
2006-06-03 17:48:43,419 [root] FATAL - Message passed as an attribute to the tag
2006-06-03 17:48:43,419 [com.mobilefish.demo.index] FATAL - Using category attribute.

Monday, December 14, 2009

Different between jdk 1.1 to jdk 1.4 jdk 1.5 jdk 1.6

JDK 1.0 (january 23, 1996) oak
- Initial release


JDK 1.1 (february 19, 1997)
- Retooling of the AWT event model
- Inner classes added to the language
- JavaBeans
- JDBC
- RMI


J2SE 1.2 (December 8, 1998)

This and subsequent releases through J2SE 5.0 were rebranded retrospectively Java 2 & version name "J2SE"
(Java 2 platform, Standard edition) replaced JDK to distinguish the base platform from
J2EE (java 2 platform, enterprise edition) and J2ME (java 2 platform, micro edition).

- Strictfp keyword
- Reflection
- Swing api integration into the core classes
- JVM equipped with a jit compiler
- Java plug-in
- Java IDL
- An IDL implementation for corba interoperability
- Collections Framework


J2SE 1.3 (may 8, 2000)

- Hotspot jvm included
- JavaSound
- JNDI included in core libraries
- Java platform debugger architecture (jpda)
- RMI was modified to support optional compatibility with corba


J2SE 1.4 (february 6, 2002)
- assert keyword
- Regular expressions
- Exception chaining (allows an exception to encapsulate original lower-level exception)
- Internet protocol version 6 (IPV6) support
- Non-blocking nio (new input/output)
- Logging API
- Image i/o api for reading and writing images in formats like jpeg and png
- Integrated XML parser and XSLT processor (JAXP)
- Integrated security and cryptography extensions (JCE, JSSE, JAAS)
- Java web start


J2SE 5.0 (september 30, 2004) tiger [originally numbered 1.5]
- Generics: provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion).
- Metadata: also called annotations; allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities.
- Autoboxing/unboxing: automatic conversions between primitive types (such as int) and primitive wrapper classes (such as integer).
- Enumerations: the enum keyword creates a typesafe, ordered list of values (such as day.monday, day.tuesday, etc.). Previously this could only be achieved by non-typesafe constant integers or manually constructed classes (typesafe enum pattern).
- Swing: new skinnable look and feel, called synth.
- Var args: the last parameter of a method can now be declared using a type name followed by three dots (e.g. Void drawtext(string... Lines)). In the calling code any number of parameters of that type can be used and they are then placed in an array to be passed to the method, or alternatively the calling code can pass an array of that type.
- Enhanced for each loop: the for loop syntax is extended with special syntax for iterating over each member of either an array or any iterable, such as the standard collection classesfix the previously broken semantics of the java memory model, which defines how threads interact through memory.
- Automatic stub generation for rmi objects.
- Static imports concurrency utilities in package java.util.concurrent.
- Scanner class for parsing data from various input streams and buffers.
- Assertions
- StringBuilder class (in java.lang package)
- Annotations



Java SE 6 (december 11, 2006)
sun replaced the name "J2SE" with java se and dropped the ".0" from the version number.
Beta versions were released in february and june 2006, leading up to a final release that occurred on december 11, 2006.
The current revision is update 14 which was released in may 2009.

- Support for older win9x versions dropped.
- Scripting lang support: Generic API for integration with scripting languages, & built-in mozilla javascript rhino integration
- Dramatic performance improvements for the core platform, and swing.
- Improved web service support through JAX-WS JDBC 4.0 support
- Java compiler API: an API allowing a java program to select and invoke a java compiler programmatically.
- Upgrade of JAXB to version 2.0: including integration of a stax parser.
- Support for pluggable annotations
- Many GUI improvements, such as integration of swingworker in the API, table sorting and filtering, and true swing double-buffering (eliminating the gray-area effect).


Java se 6 update 10

A major enhancement in terms of end-user usability.
- Java Deployment Toolkit, a set of javascript functions to ease the deployment of applets and java web start applications.
- Java Kernel, a small installer including only the most commonly used jre classes. Enhanced updater.
- Enhanced versioning and pack200 support: server-side support is no longer required.
- Java quick starter, to improve cold start-up time.
- Improved performance of java2D graphics primitives on windows, using direct3D and hardware acceleration.
- A new Swing look and feel called NIMBUS and based on synth.
- Next-generation java plug-in: applets now run in a separate process and support many features of web start applications.


Java se 6 update 12
This release includes the highly anticipated 64-bit java plug-in (for 64-bit browsers only), windows server 2008 support,
and performance improvements of java and JAVAFX applications.

Hit Counter


View My Stats