File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to Use ServletContext.log()" Watch "How to Use ServletContext.log()" New topic
Author

How to Use ServletContext.log()

Beth Barja
Greenhorn

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?
Thanks,
Beth
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

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();
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Use ServletContext.log()
 
Similar Threads
log messages
code on pg 675
Is Exception handling there
Writing to log files from jsp page
Tomcat 5.5 and context.log