Set context-param 'productionMode' dynamically from code


I’d like to switch between debug and production mode based on the target deployed location, is there a way to avoid specifying this in web.xml and instead do this from code?

At the moment this is hard-coded in web.xml:

[font=Courier New]

Vaadin production mode




You could extend ApplicationServlet and override the checkProductionMode() somehow. Unfortunately, it’s private so you can’t just override it but also need to override init() in the same class. Yes, it’s a bit hairy. Well, it could be easier to let the method do whatever it wants to do and instead override isProductionMode(), which is public.

You’ll have to specify the custom servlet class in the web.xml.

I’m not sure how the code would know which way to go, but extending AbstractApplicationServlet like this should work. Override init() and set the param to what you want before calling super.init(). You may also be able to do it in an app lifecycle listener before the servlet ever gets created. If you have more than one servlet in your app, then it’s a more general solution. Of course I haven’t tested this yet. :slight_smile:

public class AppLifecyleListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext context = servletContextEvent.getServletContext();
        context.setInitParameter( ... );

    public void contextDestroyed(ServletContextEvent sce) {
        // no-op

Or use the @WebServlet annotation. I know I push that in every blog/sample I do, but in “real” apps I usually have a web.xml file so I can see everything in one place.