Programmatically get Tomcat HTTP maxPostSize connector in JSP

I am using Tomcat 6 and want to be able to automatically extract maxPostSize (defined in the HTTP connector in server.xml) programmatically from JSP so that I can find out what the maximum file upload size is.

Is there any way to get this?

+7
java jsp tomcat
source share
2 answers

Assuming you only have one Tomcat service with one connector, you can access it in Servlet:

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

ServerFactory , by the way, org.apache.catlina.ServerFactory .

Note. This is closely related to your code with the Tomcat servlet server, and your webapp cannot be reused in other servlet containers, perhaps even in different versions. Consider setting 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 with

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

or in jsp

 ${initParam.maxPostSize} 
+5
source

In Tomcat7, the ServerFactory class is missing. You can apparently get a link to the server using

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

... which is located in org.apache.openejb: tomee-loader.

+3
source

All Articles