Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Set context-param 'productionMode' dynamically from code
Hi,
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:
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>true</param-value>
</context-param>
Thanks,
David
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.
Marko Grönroos: 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.
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. :)
@WebListener
public class AppLifecyleListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext context = servletContextEvent.getServletContext();
context.setInitParameter( ... );
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// no-op
}
}
Marko Grönroos: You'll have to specify the custom servlet class in the web.xml.
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.
Cheers,
Bobby