我正在使用tomcat 6,并且希望能够从JSP内部编程地检索MaxPostSize(在Server.xml中的HTTP连接器中定义),以便我知道最大文件上传大小是什么。

有办法得到这个吗?

有帮助吗?

解决方案

假设您只有一个带有一个连接器的tomcat服务,那么您可以通过以下方式在Servlet访问它。

int maxPostSize = ServerFactory.getServer().findServices()[0].findConnectors()[0].getMaxPostSize();

ServerFactory 顺便说一句 org.apache.catlina.ServerFactory.

注意:这是您与Tomcat ServletContainer的密封关系,并且您的WebApp可能无法在其他ServletContainers上重复使用,甚至可能没有不同的版本。考虑在中配置自己的上下文参数 web.xml 具有相同的值。

<context-param>
    <param-name>maxPostSize</param-name>
    <param-value>2097152</param-value>
</context-param>

然后,您可以通过Servlet访问它

int maxPostSize = Integer.valueOf(getServletContext().getInitParameter("maxPostSize"));

或在JSP中

${initParam.maxPostSize}

其他提示

在tomcat7中,服务器因素类已消失。显然应该能够使用

org.apache.tomee.loader.TomcatHelper.getServer()

...位于org.apache.openejb:tomee-loader中。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top