• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Registering Listeners in web.xml

 
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In HFSJ they use an registering an ServletContextListener as their example in web.xml, which is something like this



With the comment that the <listener> element does not go inside the <servlet> element because a context listener is for the context not for a specific servlet. So my question is, what about a ServletRequestListener say? That can be specific to a servlet, in that example would <listener> appear inside <servlet>, or is it always outside <servlet>?
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So my question is, what about a ServletRequestListener say? That can be specific to a servlet, in that example would <listener> appear inside <servlet>, or is it always outside <servlet>?


Listeners (i.e. all type of listeners) are always registered outside <servlet>.

Regards,
Frits
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to create listener that is specific for a servlet you have to use some kind of workaround. As Frist already pointed you cannot declare <listener> inside <servlet> tag.
If your goal is to run the listener for every request that is forwarded to specific listenr you can use filtration code something like this.

Of course, this won't be a smart solution because you are hardcoding the servlet path into the listener code and once the servlet <url-pattern> changes in web.xml the code won't work. And the performance will suffer because this listener will be called for every request in the web application.
Because of that, it might be better in this case to use a filter, that is mapped to the servlet's url pattern. That way you will keep the configuration in the DD and it will be easier to make changes.
Yet another possibility is to put your code into servlet's init() method(if the code should be executed only once when servlet is initialized) or in service() or one of the doXXX() methods. Keep in mind that if you override service() method you've better call the overriden version with super.service()

HTH
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic