Programmatically get Tomcat HTTP Connector's maxPostSize in a JSP

ε祈祈猫儿з 提交于 2019-12-05 04:19:02

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 is by the way org.apache.catlina.ServerFactory.

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>

Then you can access it in Servlet by

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

or in JSP by

${initParam.maxPostSize}

In Tomcat7 the ServerFactory class is gone. Apparently one should be able to obtain the Server reference using

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

...which resides in org.apache.openejb:tomee-loader.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!