Rachel
Rachel

Reputation: 103597

How to make sure that servlet is not loaded?

I have servlet in my web.xml but i don't want my application to load it, one would think that if we don't want that servlet to load then what is the purpose of putting it there, actually I need to have it in web.xml because am deploying two instances of application and on one instance I need to have that servlet and on another I do not want to have it and I'm using only one web.xml, am not sure how this can be done.

Here is my web.xml:

 <servlet>
      <servlet-name>StartServlet</servlet-name>
      <servlet-class>com.web.startServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

I cannot put -ve value because then container would invoke this servlet randomly, kindly correct me here and advise of an proper way to do it.

Upvotes: 2

Views: 918

Answers (3)

Piotr Nowicki
Piotr Nowicki

Reputation: 18224

You can disable the Servlet (which means that it will not be reachable through defined url-pattern mapping) by using enabled element in web.xml.

Servlets 3.0 specification in Chapter 8.2.3 Assembling the descriptor from web.xml, web-fragment.xml and annotations says:

(...) 3. If a servlet is disabled using the enabled element introduced in the web.xml then the servlet will not be available at the url-pattern specified for the servlet. (...)

The enabled element is defined in XML Schema here and can be found as a child element of the <servlet> element.

Upvotes: 1

Cody S
Cody S

Reputation: 4824

<load-on-startup> allows you to configure lazy-loading. By default the servlet is loaded only when it is accessed (by its url-pattern). You can set it to be loaded on startup instead.

That said, if you set the servlet that isn't supposed to load to only load on request, and then use a load balancer to ensure that whatever URL would hit that server hits the other one instead, you'd probably be good to go.

+1 to Bozho. His answer is a great place to start.

Upvotes: 0

Bozho
Bozho

Reputation: 597402

<load-on-startup> allows you to configure lazy-loading. By default the servlet is loaded only when it is accessed (by its url-pattern). You can set it to be loaded on startup instead.

Upvotes: 1

Related Questions