Solved

How to initialise servlet?

Posted on 2003-11-03
10
233 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
  • 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reflection example 3 102
using if condition without JSTL 2 109
struts spring hibernate example 12 122
runtime exception 2 28
Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
Adding Computers to AD groups through an SCCM Task Sequence
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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