• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ways to stop servlet from loading\instantiating

 
Nishant Vartak
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!