Solved

How to initialise servlet?

Posted on 2003-11-03
10
238 Views
Last Modified: 2010-04-01
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");%>

0
Comment
Question by:Tuan_Jean
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 9675756
>------ WEB-INF/class/com/log/Log4jInit.java ----
 but do you have ?
WEB-INF/classes/com/log/Log4jInit.class

Also, show  us your servlet mapping element in your web.xml
0
 

Author Comment

by:Tuan_Jean
ID: 9675935
Yes. I do have WEB-INF/classes/com/log/Log4jInit.class


The servlet mappting is as follow:

--- 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>
0
 

Author Comment

by:Tuan_Jean
ID: 9676344
Thank you. I have solved this problem. I have put in extra information on the path which is not required.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:rrz
ID: 9678938
> I have solved this problem
Good, then ask to delete this.
0
 

Author Comment

by:Tuan_Jean
ID: 9682588
Can you please delete this for me. Thank you.
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9683092
HI Tuan_Jean.
Can you tell me what exactly you did to solve the proble, so that I could also know.
Thx for your help!
0
 

Author Comment

by:Tuan_Jean
ID: 9683107
This is what I used:

String LogConfigFilePath = getServletContext().getRealPath("/")
String file = getInitParameter("log4j");
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9683164
Thx :)
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 10882466
PAQed, with points refunded (20)

CetusMOD
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With the rising number of cyber attacks in recent years, keeping your personal data safe has become more important than ever. The tips outlined in this article will help you keep your identitfy safe.
This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question