servletcontext listener vs servlet with high priority on loading

Discussion in 'Java' started by a, Aug 11, 2011.

  1. a

    a Guest

    Hi,

    I have a question about initializing the web app. There are 2 ways to do the
    initialization.
    1> Create a servlet with high loading priority on web.xml.
    2> Use servletcontext listener

    Why the second way is better?

    I used to use a class with static logger to standardize the format of the
    log. I cannot use this class because it has not been loaded when the
    servlet context event is sent.
    Is there anyway to overcome my problem if I use the servletcontext listener?

    Thanks
     
    a, Aug 11, 2011
    #1
    1. Advertising

  2. a

    jaap Guest

    On 11-08-11 13:18, schreef a:
    > Hi,
    >
    > I have a question about initializing the web app. There are 2 ways to do
    > the
    > initialization.
    > 1> Create a servlet with high loading priority on web.xml.
    > 2> Use servletcontext listener
    >
    > Why the second way is better?
    >
    > I used to use a class with static logger to standardize the format of the
    > log. I cannot use this class because it has not been loaded when the
    > servlet context event is sent.
    > Is there anyway to overcome my problem if I use the servletcontext
    > listener?
    >
    > Thanks
    >


    I prefer the ServletContextListener, it is made for that job. Make it
    the first in the list of listeners in web.xml.

    Success,
    Jaap
     
    jaap, Aug 11, 2011
    #2
    1. Advertising

  3. a

    markspace Guest

    On 8/11/2011 4:18 AM, a wrote:
    > I cannot use this class because it has not been loaded when the
    > servlet context event is sent.



    I'd have to go check my JEE stuff again but I'd say in general the
    context listener is superior. Loading order has to be manually
    maintained. Context listeners fire automatically.


    >
    > I used to use a class with static logger to standardize the format of the
    > log.


    For this particular application, there are already ways to initialize
    your logger (both the standard SE one and log4j). Why are you not using
    the standard configuration files?
     
    markspace, Aug 11, 2011
    #3
  4. a

    markspace Guest

    On 8/11/2011 11:46 AM, a wrote:
    > I build my own version of Log Util based on the log4j. The purpose is for
    > convenience. It involves a lot of static method and static variable.



    I think you have just discovered that home built frameworks like this
    are often not more convenient. Please try the existing configuration files.


    > I expect objects can be instantiated in ServletContextListener, but not any
    > static in a class. Is that true? Pls explain.



    I don't understand.
     
    markspace, Aug 11, 2011
    #4
  5. a

    Lew Guest

    markspace wrote:
    > a wrote:
    >> I build my own version of Log Util based on the log4j. The purpose is for
    >> convenience. It involves a lot of static method and static variable.

    >
    > I think you have just discovered that home built frameworks like this
    > are often not more convenient. Please try the existing configuration files.


    +1

    With log4j, the same-named logger (i.e., via the class or class name you pass to the constructor) is the same logger instance. It is better to name alogger by the class that uses it, as documented in the log4j instructions.This helps ops to maintain the program. I don't know whose convenience you're trying to serve, "a", but I will bet dollars to doughnuts it's not the folks whose convenience you ought to serve, namely the production personnel.

    Logging isn't for programmers, it's for ops.

    >> I expect objects can be instantiated in ServletContextListener, but not any
    >> static in a class. Is that true? Pls explain.

    >
    > I don't understand.


    I wonder if "a" means that you cannot instantiate a logger as a static variable, which of course is nonsense. You most definitely can, and sometimes should, instantiate a logger as a static variable.

    If "a" refers to objects in general, then it's even more nonsensical. It'sJava. Of course you can instantiate objects as static members.

    Perhaps "a" ought to clarify his meaning. "a"?

    Also, "a", I heartily recommend you read *and study* the documentation for log4j (or any other framework that you use).

    --
    Lew
     
    Lew, Aug 11, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    10,334
  2. Major
    Replies:
    4
    Views:
    16,158
    johnrambo
    Mar 11, 2008
  3. cwdjrxyz
    Replies:
    2
    Views:
    366
    Adrienne Boswell
    Nov 3, 2006
  4. Marcel Müller
    Replies:
    3
    Views:
    565
    Marcel Müller
    Apr 27, 2009
  5. Sandy
    Replies:
    2
    Views:
    127
    Brian Candler
    Nov 4, 2008
Loading...

Share This Page