SpringBoot Embedded Tomcat JSPServlet Options

2019-09-19 07:34发布

问题:

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.

回答1:

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");
                }
            });
        }
    };
}


回答2:

Or you could just add the parameters to your application.properties file as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html Look for:
server.jsp-servlet.init-parameters.*= # Init parameters used to configure the JSP servlet

For example:

server.jsp-servlet.init-parameters.modificationTestInterval=10