Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get Tomcat HTTP Connector's maxPostSize in a JSP

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.

Is there a way to get this?

like image 206
Stephen Cathers Avatar asked Oct 08 '10 21:10

Stephen Cathers


2 Answers

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}
like image 145
BalusC Avatar answered Nov 03 '22 10:11

BalusC


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.

like image 34
Jaroslav Záruba Avatar answered Nov 03 '22 10:11

Jaroslav Záruba