2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • Which IDE are you using? Try to generate war file from it. Also see http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html. – Aleksandr M Aug 16 '13 at 17:44
  • @AleksandrM Thank you. I am using eclipse. I tried generating war files from it, but was getting 404 and 500 errors. So I am going deeper to try to understand how it works. The link you sent will not load in my browser. – CodeMed Aug 16 '13 at 17:50
  • If you are not using maven then create Dynamic Web Project in eclipse. – Aleksandr M Aug 16 '13 at 17:54
  • @AleksandrM I have a dynamic web project in eclipse. I am wondering if my 404 and 500 errors might be the result of not manually including the JRE System Library jars in the dist/WEB_INF/lib folder? – CodeMed Aug 16 '13 at 17:59
  • It is hard to tell w/o more info. – Aleksandr M Aug 16 '13 at 18:09
  • Create a subfolder for images `dist/images`, jsp's should be `/dist/views/`, all other xmls should be in `WEB-INF`. – Sachin Thapa Aug 16 '13 at 18:15
  • @SachinThapa 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. Does this change your recommendation regarding the location of the JSP files? – CodeMed Aug 16 '13 at 18:25
  • @AleksandrM I put a little more information about the location mapping for the JSP files in an edit to my original posting above. Does this change your recommendation regarding where to place the JSP files? – CodeMed Aug 16 '13 at 18:29
  • @CodeMed, I am not sure if you can change this code to `RequestDispatcher jsp = context.getRequestDispatcher("/views/jsp/home.jsp");`, infact i would create subfolders for various kind of views. WEB-INF is considered as restricted/private if your JSP pages are for public access(browser) they should be outside. – Sachin Thapa Aug 16 '13 at 18:40
  • @SachinThapa It works now. Thanks. I was able to keep the jsp folder in WEB-INF. In fact, it did not work when I moved them to a views folder. I think the issue is that the web container is accessing the jsp files. No end user will have access to the jsp files. They are simply used as request dispatcher objects by the servlets. – CodeMed Aug 16 '13 at 19:16
  • @CodeMed good to hear it worked for you. – Sachin Thapa Aug 16 '13 at 19:28
  • you should seriously consider maven+springframework. – Kevin Aug 16 '13 at 21:00
  • If you are writing an Ant build file by hand, you should be aware that you can embed elements (and other resource collections) directly in the element. There is no need to create a separate `dist` directory. Copying all those files an extra time just to add them to a .war will noticeably increase your build time. – VGR Aug 17 '13 at 14:38

2 Answers2

2

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.

splungebob
  • 5,357
  • 2
  • 22
  • 45
  • Thank you. +1 for trying to help. And credit for the answer, since you were the only one in the form of an answer, and you more or less answered my questions, along with some help from the commenters above. – CodeMed Aug 16 '13 at 19:18
  • 5.) there's no point in making JSP files public, in fact that could show an attacker valuable information, so keeping them IN WEB-INF is a good idea. – marc82ch Jan 04 '14 at 17:00
1

Look here for a good explanation:

http://www.openscope.net/2010/01/25/war-deployment-file-structure/

Specifically, only .class files go in the /classes directory, and only .jars go into the /lib directory. Beyond those and web.xml, everything else (i.e. non java stuff like html, css, js, other files) can be put in the root directory and will be accessible at the root URL of your app URL that is deployed.

Stan Lin
  • 928
  • 7
  • 6