Solved

How to initialise servlet?

Posted on 2003-11-03
10
234 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
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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
How to group array of objects ? 5 69
dateFashion java challenge 9 146
Spring MVC - sending raw charset to backend 3 222
web application structure 18 100
This article describes how to reset your Windows 10 password when you've forgotten it.
There's a lot of hype surrounding blockchain technology. Here's how it works and some of the novel ways it' s now being used - including for data protection.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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