4

Lets say I have a simple "Hello world" type servlet, configured with the annotation @WebServlet("/hello").

I want to disable it for build/deployment, so it will not be possible to "call" the servlet. How would I do that?

Then, through a configuration file, I want to be able to enable the servlet at run-time, so it can be used by a client. How would I do that?

Is either of these possible?

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550

3 Answers3

9

You can't enable servlets during runtime via standard API. It can at most only be enabled during build time in web.xml or during deploy time by ServletContext#addServlet(). Your best bet is to always enable it and control it on a per-request basis. You can use a servlet filter for this.

First give the servlet a name.

@WebServlet(urlPatterns="/hello", name="yourServlet")
public class YourServlet extends HttpServlet {
    // ...
}

So that you can easily map a filter directly to it without worrying about servlet's URL patterns.

@WebFilter(servletNames="yourServlet")
public class YourFilter implements Filter {
    // ...
}

In your filter, just decide whether to continue the chain, or to return a 404 based on your configuration file setting.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (isYourConfigurationFileSettingSet()) {
        chain.doFilter(request, response);
    } else {
        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

The isYourConfigurationFileSettingSet() part can't be answered in detail based on the information provided so far. In case you actually also couldn't figure out that, then head to Where to place and how to read configuration resource files in servlet based application?

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
1

Instead of defining the servlet through an annotation, do it in the web.xml file. Different versions of this file may allow you to have the servlets enabled or not.

The version of web.xml to use should be selected at build and deployment time. Maybe by a Maven profile or similar. Take a look at the following link for some ideas on that: https://maven.apache.org/guides/mini/guide-building-for-different-environments.html

Andres
  • 9,722
  • 4
  • 37
  • 57
  • This is definitely a possibility that I thought about, but dismissed because it would be easy to select one of multiple `web.xml` files when packaging the WAR file but I don't know if it's possible to select at run-time, or how. – Some programmer dude Mar 07 '17 at 13:18
1

If you want truly run-time control, then you may have to do a little custom coding. A filter (or, I suppose, the servlet itself) could check the value of a property and return a response with an HTTP error code (I suppose 403 would be vaguely appropriate; 404 less so, but if you want it to appear as though the servlet didn't exist in that configuration, it would work...)

Mark Adelsberger
  • 32,904
  • 2
  • 24
  • 41