Tuesday, April 27, 2010

WEB-INF & META-INF

What are WEB-INF and META-INF directories?
  • Both directories exist in a Java Web Application (J2EE) which runs on a Tomcat Server.
META-INF
  • It is automatically created while we create a war file.
  • After deploying and extracting the war file in tomcat server (webapps director), theoretically it is safe to delete.
Note:
  1. While we create a jar file, an unbrowsable META-INF directory which contains the manifest (list of contents) is also created automatically. War file is has the same nature as jar file, so that when we create a war file, we get a META-INF directory automatically.
  2. War (Web Archive) file is a compressed file. It is a collection of 1) Java Server Pages (JSP), 2) Servlets, 3) Java Classes, 4) XML files, 5) Tag Libraries, 6) Static web pages (HTML and related files).
WEB-INF
  • It is a mandatory directory (vital component) and java web application will not work without it!!!.
  • Name of the directory has to be in the capital letters separated with a hyphen.
  • This directory is created automatically while a war file of the web application is created.
  • Under normal condition, any change in this directory is not required.
  • This directory contains a hierarchy in which 1) the necessary configuration information for the web application, and 2) all the class files for the servlets and classes that are called up by the JSPs (Java Server Pages)
  • WEB-INF is a mirror META-INF, but the former doesn't include any documentations!
  • The application deployment descriptor file (web.xml) is a standard XML file under this directory.
  • WEB-INF comes under the root directory of the application.
  • Anything comes in the root directory excluding WEB-INF are publicly available and can be accessed using URL from the browser.
  • WEB-INF directory is a private area of the web application, any files under WEB-INF directory cannot be accessed directly from browser by specifying the URL like http://somesite/WEB-INF/someresource.html. Web container will not server the content of this directory.

Note:
  1. Any resources like JSPs or HTML document that don’t wish to be accessible directly from web browser, should place it under WEB-INF directory.
  2. A java web application has the directory structure shown below J2EE Web Application Directory Structure
  3. WEB-INF/web.xml : web.xml is called the web application deployment descriptor. This is a XML file that defines servlets, servlet mappings, listeners, filters, welcome files etc. Deployment descriptor is a heart of any J2EE web application, so every web application must have a web.xml deployment descriptor directly under WEB-INF folder.
  4. WEB-INF/classes : The classes directory is used to store compiled servlet and other classes of the application. If your classes are organized into packages, the directory structure must be reflected directly under WEB-INF/classes directory. The classes directory is automatically included in CLASSPATH.
  5. WEB-INF/lib : Lib directory is used to store the jar files. If application has any bundled jar files, or if application uses any third party libraries such as log4j, JDBC drivers which is packaged in jar file, than these jar files should be placed in lib directory.
  6. All unpacked classes and resources in the /WEB-INF/classes directory, plus classes and resources in JAR files under the /WEB-INF/lib directory are included in classpath and made visible to the containing web application.