• 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

Ways to stop servlet from loading\instantiating

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to stop servlet from getting loaded based on following conditions.
1)A parameter is not matched.(It might be in DD or application specific properties file).
2)based on a parameter fethced from database.

What should i use amongst the following:
1. servlet's init().
2. ServletContext Listener.
Which is better/appropriate?
for database connection i would like to do a look up on aready configured resource in Application server.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet class would definitely be loaded (if not at startup then at the request time ) and the init method in that would definitely be called.

The thing that you can do is , do not execute the code based on the condition within the init method.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Throw javax.servlet.UnavailableException in the init method to let the container know that the servlet is not in service.
 
Nishant Vartak
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should i write the code to connect DB in ContextListener and then populate the result in ContextAttribute. Based on the result i can dedcide to throw javax.servlet.UnavailableException in servlet init() method.
or should i write the code in servlet's init() method.
Does it make any difference?
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you write your code in ServletContextListener, your check would be done only once when the servlet context is initialized while if you write your code in the init method, the check would be done everytime the servlet is initialized, which may happen more than once if multiple servlet instances are created. You can make a choice based on your business requirement.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nishant Vartak:
Should i write the code to connect DB in ContextListener and then populate the result in ContextAttribute. Based on the result i can dedcide to throw javax.servlet.UnavailableException in servlet init() method.
or should i write the code in servlet's init() method.
Does it make any difference?



Is the information only used in that servlet?
If not, then your first option would be better.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic