Search code examples
jspspring-bootembedded-tomcat-8

SpringBoot Embedded Tomcat JSPServlet Options


What is the preferred way to set the configuration options for JSPServlet like checkInterval, keepgenerated, modificationTestInterval etc? The reason I am trying to alter it is because of some strange issues with JSP Compilations. We are using executable war packaging and setting the 'server.tomcat.basedir' property to point to a locally accessible folder. The generated jsp java source and class files shows modification date as Jan 14 1970. In windows explorer, the modification just shows up as empty. On linux, we did a touch on all the files. But as soon as the jsp file is accessed again, the modification date goes back to 1970. We doubt that this is causing the jsp files to be compiled every time it is accessed and thus slowing things down. However the recompilation only seems to happen in linux environment. Has anyone experienced this problem? Our environment : Spring Boot 1.2.2.BUILD-SNAPSHOT, Tomcat 8, JDK 1.8_025.


Solution

  • You can use an EmbeddedServletContainerCustomizer @Bean to look up the JSP servlet and configure its init parameters. For example, in your main @Configuration class:

    @Bean
    public EmbeddedServletContainerCustomizer customizer() {
        return new EmbeddedServletContainerCustomizer() {
    
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
                }
            }
    
            private void customizeTomcat(TomcatEmbeddedServletContainerFactory tomcat) {
                tomcat.addContextCustomizers(new TomcatContextCustomizer() {
    
                    @Override
                    public void customize(Context context) {
                        Wrapper jsp = (Wrapper) context.findChild("jsp");
                        jsp.addInitParameter("modificationTestInterval", "10");
                    }
                });
            }
        };
    }