Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to initialise servlet?

Posted on 2003-11-03
10
Medium Priority
?
244 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 28

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
Industry Leaders: 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 28

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

926 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