*
The moose likes Servlets and the fly likes Can we turn on and off the servlets in web.xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can we turn on and off the servlets in web.xml?" Watch "Can we turn on and off the servlets in web.xml?" New topic
Author

Can we turn on and off the servlets in web.xml?

nacassa sam
Greenhorn

Joined: Mar 27, 2012
Posts: 7
Is it possible to turn on and off the particular servlets from web.xml ((ie) Disable or enable particular servlet in web.xml) externally without updating the data manually in web xml?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

Not with anything already available from the Servlet API.

What's the use case? Perhaps there's a way to achieve what you are actually trying to accomplish.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nacassa sam
Greenhorn

Joined: Mar 27, 2012
Posts: 7
Thanks for the reply, I need to disable or enable the servlets depending on the input data for example, if its a production eniv, I need few servlets to be available and for test i doesn't require them, so basically i need to update the servlets in web xml to support my configuration.

Is there any possibility of using load-on-startup values and setting this?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Perhaps you could do this with a front controller and context attributes that you can set through some servlet, but it would require your entire web application to be rewritten to use that front controller.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What would it mean to "disable" a servlet? Would that require sending a 404 error response to any requests for that servlet?

Anyway keeping track in the application context of which servlets were "disabled", along with a filter to enforce that disabling, might also be a solution.
nacassa sam
Greenhorn

Joined: Mar 27, 2012
Posts: 7
No, It does not need to send a 404 message, the scenario here is, I have a bunch of servlets in web xml file and depending on the input data my web xml should be modified such that only servlets required for my app based on the input should be present in the web xml and other servlets in the web xml should be disabled.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

But what does "disabled" mean? What should happen if you still try to access that servlet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

Sounds like this a build-time issue not a run-time issue. If you are building for the test environment, create a web.xml that includes the extra servlets. For a production build, leave them out of the web.xml.
nacassa sam
Greenhorn

Joined: Mar 27, 2012
Posts: 7
@Rob Those servlets should not be available for access in that particular environment.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

nacassa sam wrote:@Rob Those servlets should not be available for access in that particular environment.

Which brings us back to my reply. If they don't need to be there, don't put them there in the first place.
nacassa sam
Greenhorn

Joined: Mar 27, 2012
Posts: 7
Yeah thanks for the info, but i was trying to find whether there is a possibility of having one web xml and the servlets related to the app in it as like i have now, and to turn off the servlets depending on the environment selected, without doing it manually.

But guess from your answers, there is no option to do so, Am i right?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Let's assume that to "turn off" a servlet means to not include it in the web.xml file -- that seems the most likely thing for it to mean based on what I've seen so far.

And let's assume that "doing it manually" means "using a text editor to modify the web.xml file" -- although it could mean a lot of other things.

Then no, you don't have to "do it manually". You could write an application which transformed a master version of the web.xml file into a subset suitable for your various applications.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

How are you doing your build? Ant has plenty of options for doing this sort of thing.
nacassa sam
Greenhorn

Joined: Mar 27, 2012
Posts: 7
I am using Maven for my build.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we turn on and off the servlets in web.xml?
 
Similar Threads
ResultSet vs. ScrollableResults
How to enable JSP COMPILATION?
how to delete huge amount of records
Doubt in isELIgnored
What is invoker servlet in Tomcat Container? How can we disable it