Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure a servlet init-param in a Spring boot application

I have a Spring boot application (version 1.2.1.RELEASE) that uses Spring MVC, and I need to set the development init parameter on the JspServlet. In web.xml this would have looked like the following:

<servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        ...
        <init-param>
            <param-name>development</param-name>
            <param-value>false</param-value>
        </init-param>
         ...
</servlet>

I would have thought I could do something like this: How to configure spring-boot servlet like in web.xml?, but I don't think I can just replace new MeteorServlet() with new JspServlet() and go on my way, as I am sure JspServlet.init(ServletConfig config) needs to be called with a valid ServletConfig. And, of course if I do just try it without proper in initialization, the application gives me a null pointer exception (which occurs because the private transient JspRuntimeContext rctxt; is null):

java.lang.NullPointerException: null
    at org.apache.jasper.servlet.JspServlet.periodicEvent(JspServlet.java:361)
    at org.apache.catalina.core.StandardWrapper.backgroundProcess(StandardWrapper.java:679)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Thread.java:745)

Also, we have a really hacky way to do this by implementing a TomcatEmbeddedServletContainerFactory and providing a servlet where we override the init method and the getInitParameter method of the ServletConfig, but it is somewhat hateful, and so I am hoping there is a better way. Any guidance would be greatly appreciated.

Lastly, the code that reads this init-param is in the org.apache.jasper.EmbeddedServletOptions class and appears to only look for a ServletConfig init param so I can't specify this as a context param (I don't think) with something awesome like server.context-parameters.name=value.

String development = config.getInitParameter("development");
if (development != null) {
    if (development.equalsIgnoreCase("true")) {
        this.development = true;
    } else if (development.equalsIgnoreCase("false")) {
        this.development = false;
    } else {
        if (log.isWarnEnabled()) {
            log.warn(Localizer.getMessage("jsp.warning.development"));
        }
    }

}

And if I try to hit the application anyway, I get the following error:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:646)
at org.apache.jasper.compiler.JspUtil.makeJavaIdentifier(JspUtil.java:833)
at org.apache.jasper.compiler.JspUtil.makeJavaIdentifier(JspUtil.java:806)
at org.apache.jasper.JspCompilationContext.getServletClassName(JspCompilationContext.java:332)
at org.apache.jasper.JspCompilationContext.getClassFileName(JspCompilationContext.java:504)
at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:444)
at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:408)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:557)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:335)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:291)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration$MetricsFilter.doFilterInternal(MetricFilterAutoConfiguration.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
like image 824
MMW Avatar asked Mar 31 '15 21:03

MMW


1 Answers

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.development=true
like image 191
sylvied Avatar answered Oct 23 '22 14:10

sylvied