Search code examples
javaeclipsejakarta-eeservletswar

Planning structure for a WAR file


I am creating my first WAR file. I have been experimenting with ant buildfile syntax, and the first part of my buildfile takes content from my Eclipse project and places it into a /dist folder, which will then be zipped up into a WAR file in subsequent steps.

I would like to structure the output so that the WAR file has all the required contents in the right places for execution by Tomcat 7 server.

The current draft of my buildfile generates a dist folder with the following structure:

dist/META-INF/MANIFEST.MF  
dist/WEB-INF/classes/myapp/package1/ (contains 12 .class files)  
dist/WEB-INF/classes/myapp/package2/ (contains 7 .class files)  
dist/WEB-INF/jsp (contains 5 .jsp files)  
dist/WEB-INF/lib (contains 16 .jar files)  
dist/WEB-INF/web.xml  
dist/image1.gif  
dist/image2.gif  
dist/image3.gif  
dist/image4.gif 

How do I need to change the structure so that the resulting WAR file can be used by Tomcat 7 server to serve up my web application to end users?

Specifically:

1.) should I include .java files in dist/WEB-INF/classes/... or just put .class files there?
2.) should I add JRE System Library JARs into dist/WEB-INF/lib? These System JARs are in my Eclipse workspace, but I do not know if I need them in the WAR file.
3.) is web.xml in the right place?
4.) should my .gif files stay in the root directory?
5.) are my .jsp files in the right place?
6.) where does myapp.xml go? myapp.xml is in the root directory of my Eclipse workspace, and seems essential when loading myapp into my localhost instance of Tomcat server. Is myapp.xml required in a WAR file? Or does the structure of the WAR file do the things that myapp.xml would otherwise do?

Please note that my servlet classes control all access to my JSP files, and each servlet instantiates its own JSP with the syntax:

RequestDispatcher jsp = context.getRequestDispatcher("/WEB-INF/jsp/home.jsp");  

All access to site content is through URLs defined by web.xml's servlet mapping.

I am trying to learn this first one correctly so that I can make subsequent WAR files well the first time.


Solution

  • I'll do my best:

    1.) should I include .java files in dist/WEB-INF/classes/... or just put .class files there?

    You should only need .class files here.

    2.) should I add JRE System Library jars into dist/WEB-INF/lib?

    Shouldn't be needed.

    3.) is web.xml in the right place?

    Looks good.

    4.) should my .gif files stay in the root directory?

    Although not necessary, I'd prefer an "image.jar" (or "resource.jar", or whatever) to keep things tidy.

    5.) are my .jsp files in the right place?

    Not sure, but I think the jsp directory would typically be up one level, out of WEB-INF and directly in your dist folder.

    6.) where does myapp.xml go?

    I don't know what this is, so I couldn't say.