Solved

How to initialise servlet?

Posted on 2003-11-03
10
226 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
Comment Utility
>------ 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
Comment Utility
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
Comment Utility
Thank you. I have solved this problem. I have put in extra information on the path which is not required.
0
 
LVL 27

Expert Comment

by:rrz
Comment Utility
> I have solved this problem
Good, then ask to delete this.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Tuan_Jean
Comment Utility
Can you please delete this for me. Thank you.
0
 
LVL 3

Expert Comment

by:applekanna
Comment Utility
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
Comment Utility
This is what I used:

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

Expert Comment

by:applekanna
Comment Utility
Thx :)
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
Comment Utility
PAQed, with points refunded (20)

CetusMOD
Community Support Moderator
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Jquery ajax file upload not working 11 143
bean mapper frameworks 5 56
How to Post an If Statement in JSP 3 45
maven j2ee examles 2 43
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now