• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Use ServletContext.log()

 
Beth Barja
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks,
Beth
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
regards
maulin
 
Beth Barja
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
SC.log(e,sErrorString);
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?
Thanks,
Beth
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic