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.
SpringBoot Embedded Tomcat JSPServlet Options
Asked Answered
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");
}
});
}
};
}
It would be nice to have these jsp configurations exposed as properties. Tomcat has some recommended configuration for production settings (tomcat.apache.org/tomcat-8.0-doc/…) which is different than the defaults. So I guess this would be something most applications using jsps & Tomcat would need –
Larianna
I was able to use this approach to map HTML files to JSP too - jsp.addMapping("*.html"); Although I appreciate that sounds like bad practice :( –
Cutlor
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
you need also
server.jsp-servlet.init-parameters.development=true
–
Andrien 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");
}
});
}
};
}
It would be nice to have these jsp configurations exposed as properties. Tomcat has some recommended configuration for production settings (tomcat.apache.org/tomcat-8.0-doc/…) which is different than the defaults. So I guess this would be something most applications using jsps & Tomcat would need –
Larianna
I was able to use this approach to map HTML files to JSP too - jsp.addMapping("*.html"); Although I appreciate that sounds like bad practice :( –
Cutlor
© 2022 - 2024 — McMap. All rights reserved.