It's not a secret anymore!
The moose likes Servlets and the fly likes How to Use ServletContext.log() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to Use ServletContext.log()" Watch "How to Use ServletContext.log()" New topic

How to Use ServletContext.log()

Beth Barja

Joined: Nov 02, 2001
Posts: 10
I need to log error messages to the tomcat log.
Someone suggested using the ServletContext.log().
I tried this and I am getting the following error. "Non-static method log() cannot be reference from static context" What am I doing wrong. Could you give me a code example?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi Beth,
u can view the servlet api at,
JakartaTomcat Page
log() is a method in GenericServlet Class and so HttpServlet has it and as a result our servlets that extends HttpServlet also has it. so we can just call log() method in our servlet without referring to any other class.
log() isn't a static method. it's an instance method.
Beth Barja

Joined: Nov 02, 2001
Posts: 10
Here is what I tried:
At the top of the bean I have:
import javax.servlet.http.*; //already had this
//added the 3 below to try to get this to work.
import javax.servlet.http.HttpServlet;
import javax.servlet.*;
import javax.servlet.ServletContext;
I tried:
ServletConfig sConf = Servlet.getServletConfig(); ServletContext SC = sConf.getServletContext();
Error: "non-static method getServletConfig() cannot be referenced from a static context
ServletConfig sConf = Servlet.getServletConfig();"
I also tried:
log(e, sErrorString);
Error: "cannot resolve symbol
symbol : method log (java.lang.Exception,java.lang.String)
log(e, sErrorString);"
Help, What am I doing wrong?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13018
You got that error because getServletConfig() is an instance method (in GenericServlet).
The log method has two forms
log( String msg )
log( String msg, Throwable t )
You got the order of parameters backwards.
I agree. Here's the link:
subject: How to Use ServletContext.log()
It's not a secret anymore!