logging in class files
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Stephen_Ward
Posted On:   Monday, November 26, 2001 07:20 AM

Hi there Im developing an application using jsp 1.2 and Tomcat4. Several of my jsp pages call class files held in the WEB-INF directory. That works fine! But I now want these class files to use the log() method contained in the HttpServlet class and so my class files now inherit from HttpServlet. Is this OK? I am essentially making my class files servlets. I dont know too much about servlets so can you tell me if there will be any probs. I am not overriding any methods (e.g. DoGet) as this servlet doesnt return a response. Will the constructor still be called, and will there be any changes in the objects lifecycle? Cheers guys thanks for the help Steve    More>>

Hi there
Im developing an application using jsp 1.2 and Tomcat4.

Several of my jsp pages call class files held in the WEB-INF directory. That works fine! But I now want these class files to use the log() method contained in the HttpServlet class and so my class files now inherit from HttpServlet.

Is this OK? I am essentially making my class files servlets. I dont know too much about servlets so can you tell me if there will be any probs. I am not overriding any methods (e.g. DoGet) as this servlet doesnt return a response. Will the constructor still be called, and will there be any changes in the objects lifecycle?

Cheers guys
thanks for the help
Steve

   <<Less

Re: logging in class files

Posted By:   damon_henry  
Posted On:   Monday, November 26, 2001 01:58 PM

I agree with Luigi. I personally stay away from extending HttpServlet if the class is not a true servlet. Another approach would be to place the ServletContext in the constructor making it available to all methods or pass the ServletContext to the methods that need it. i.e.:
public void testMethod(ServletContext context) {

    context.log("hey I'm logging");

}

Re: logging in class files

Posted By:   Luigi_Viggiano  
Posted On:   Monday, November 26, 2001 01:26 PM

No, it's a bad practice to make your classes to extend HttpServlet if they are not servlets. For different reasons:

  1. It makes code confusing, people reading: they will firstly thing to having to do with a Servlet...
  2. Extending from HttpServlet will fire your option to extend a more appropriate superclasses.
  3. Extension implies in a IS-A relation, so every class in your project IS-A Servlet, and this is obviously wrong.
  4. Your classes will have meaningless methods that can generate confusion on client programmers using them...


These are only some reasons come up in my mind right now. And just them can be justified for a logging facility involving in 1 single method log()? No!

Make your own implementation of log, or use Log4J or new jdk 1.4 API for logging, instead.

About | Sitemap | Contact