• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I initialise a servlet ?

 
Chuan Ren
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I am very new to servlet and would appreciate your assistance in this problem.
I am using j2sdk1.3.1_08, Tomcat 4.1.27, Log4j 1.2.28
I would like to initialise log4j using servlet. Following are the codes I used. Somehow, this servlet does not get initialised as there are no logs generated. But if I run runLog.jsp following by test.jsp, it works. Do you know why?
------ runLog.jsp ---
<%@ page import="org.apache.log4j.*"%>
<%
Logger logger = Logger.getLogger(getClass());
String LogConfigFilePath = getServletContext().getRealPath("/")+"WEB-INF/classes/config/log4j.properties";
BasicConfigurator.resetConfiguration();
PropertyConfigurator.configure(LogConfigFilePath);
logger.info("runLog successful");
%>
------ WEB-INF/class/com/log/Log4jInit.java ----
package com.log;
import org.apache.log4j.*;
import javax.servlet.http.*;
public class Log4jInit extends HttpServlet {
public void init() {
String LogConfigFilePath = getServletContext().getRealPath("/") + "config//log4j.properties";
BasicConfigurator.resetConfiguration();
PropertyConfigurator.configure(LogConfigFilePath);
public void doGet(HttpServletRequest req, HttpServletResponse res) {}
}
--- web.xml ---
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.log.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/classes/config/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
--- test.jsp ---
<%@ page import="org.apache.log4j.*" %>
<% Logger logger = Logger.getLogger("test.jsp");
logger.info("befor say hi");
%>
<h3> Hi</h3>
<% logger.info("after say hi");%>
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you have it set up, you'd have to include the Log4J initialisation code in every class that wants to use the Logger, or call either the runLog.jsp or Log4jInit servlet before any other code can use the logger.
The easier way to get things working is to allow Log4J to loads it's configuration. The first time Log4J is called, it looks on the ClassPath for its log4j.properties. If the log4j.properties is not on the classpath, then you need to use the code you've used above, but letting Log4J take care of it is much easier.
Drop the log4j.properties in the WEB-INF/classes directory, then all web application code should be able to refer to the logger the same way you have it in test.jsp - ie you don't need all that other rubbish!
The WEB-INF/classes is on the classpath for the web application, so log4j should be able to find the properties file automatically.
Dave
 
Chuan Ren
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That has solved my problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic