I have two part in my application : administration and client part.
Your question is very broad, so it's almost impossible to answer.
First, the reason for heap issue might as well be that you're buffering the entire uploaded file in memory (i.e. you deserialize the uploaded image into
byte or anything like that). This is something you never do with untrusted content or reasonable size limits, as someone might upload a 16GB file and destroy your RAM completely. Instead, you can stream the uploaded bytes either to disk or to something else that works with streams.
FileUpload from Apache Commons gives you a nice and simple way to take uploads of arbitrary size and dynamically decide whether to buffer them in memory or disk based on size:
// Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setSizeThreshold(yourMaxMemorySize); factory.setRepository(yourTempDirectory); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(yourMaxRequestSize); // Parse the request List<FileItem> items = upload.parseRequest(request);
On the topic of separating applications, just make 2 WARs, no magic there. But keep in mind that whatever crashes your app due to heap problems, is likely to crash the entire JVM, so to isolate them, you'll need to deloy them into separate JVMs (i.e. separate Tomcat instances).
To share common classes, like controllers, just separate them into a JAR that both WARs depend on. A simple maven multi-module project will do, one module for each WAR, and one for each shared JAR.
The resources you can share in a few ways:
META-INF/resources: This is supported starting from Servlet 3.0. Here's a detailed guide. This approach has the benefit of being simple to deploy, just treat the resources JAR as any other dependency. The down side is that it doesn't allow for easy redeployment without restarting the app.
If in your classpath (meaning inside
WEB-INF/lib/ of your WAR) you have a JAR that contains
META-INF/resources/image.jpg, you'll be able to access it via:
See the guide for more details.
You can tell Tomcat to look for resources in an external location. The configuration is different for different Tomcat version:
<Context aliases="/images=/var/www/images" />
<Context> <Resources> <PostResources className="org.apache.catalina.webresources.DirResourceSet" base="/var/www/images" webAppMount="/images" /> </Resources> </Context>
You then put static resources into
/var/www/images/image.jpg) and access them via
See this tutorial for more examples.