以编程方式在JSP中获取Tomcat HTTP Connector的maxPostSize [英] Programmatically get Tomcat HTTP Connector's maxPostSize in a JSP
问题描述
我正在使用Tomcat 6,并且希望能够从JSP中以编程方式检索maxPostSize(在server.xml中的HTTP连接器中定义),以便我可以知道最大文件上载大小是什么。
I am using Tomcat 6 and would like to be able to retrieve the maxPostSize (defined in the HTTP Connector in server.xml) programmatically from within a JSP so that I can know what the max file upload size is.
有没有办法得到这个?
推荐答案
假设你已经只有一个具有一个连接器的Tomcat服务,然后您可以通过以下方式在Servlet中访问它:
Assuming that you've only one Tomcat service with one connector, then you can access it in Servlet by:
int maxPostSize = ServerFactory.getServer().findServices()[0].findConnectors()[0].getMaxPostSize();
ServerFactory
顺便说一下 org.apache.catlina.ServerFactory
。
ServerFactory
is by the way org.apache.catlina.ServerFactory
.
注意:这会将您的代码紧密耦合到Tomcat servlet容器,并且您的webapp可能无法在其他servlet容器上重复使用,可能甚至没有不同的版本。考虑使用相同的值在 web.xml
中配置您自己的上下文参数。
Note: this tight-couples your code to the Tomcat servletcontainer and your webapp may not be reuseable on other servletcontainers, possibly even not different versions. Consider configuring your own context parameter in web.xml
with the same value.
<context-param>
<param-name>maxPostSize</param-name>
<param-value>2097152</param-value>
</context-param>
然后你可以在Servlet中访问它
Then you can access it in Servlet by
int maxPostSize = Integer.valueOf(getServletContext().getInitParameter("maxPostSize"));
或在JSP中
${initParam.maxPostSize}
这篇关于以编程方式在JSP中获取Tomcat HTTP Connector的maxPostSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!