In order to execute a Java application, A web or application server having a servlet container is required. A web server knows only how to intercept HTTP requests and how to return contents of static web pages. It doesn’t know how to execute servlets. A servlet container extends the functionality of a web server by facilitating the execution of servlets in a sand-boxed (managed and secure) environment. Apache and IIS are web servers and Tomcat is a servlet container.
As servlet API need servlet container similarly other APIs of Java Enterprise Edition (JEE) need other runtime environments. e.g. JMS API need Messaging Server, EJB API need EJB container, JNDI API need Directory server etc. To facilitate the execution of applications which use one or more JEE APIs, a runtime environment called Application Server is introduced. An application server contains, web server, servlet container, EJB container, Messaging server, Directory server and many more features required by enterprise applications. Glassfish, Jboss, WebSphere are some well known application servers of Java.
For an analogy, a web server can be thought of as a land-line classical phone which facilitate only voice calls, a web server with servlet container can be thought of as a simple mobile which facilitate messaging in addition to voice calls and an application server can be thought of as a smart phone which facilitate many more things. If you have a dynamic web application which doesn’t use any of the above mentioned JEE APIs then only a web server having a servlet container is sufficient for you.
As web applications can be deployed on different servers hence a standard directory structure and packaging format are required to make them server independent. Servlet specification defines the directory structure and packaging format for Java web applications.
Directory structure of a java web application:[one_half]
1 – Folders marked with * are optional i.e some or all of them may not be required in each web application.
2 – Static resources can be HTML pages, CSS files, JS files, Images and other resources. Usually all these are stored in different folders.
3 – Lib folder is required only when the web application references third party libraries such as database drivers, frameworks etc.
4 – A JSP based web application doesn’t have servlet classes hence it doesn’t require classes folder.
5 – Each web application is deployed on the server as a self descriptive unit i.e. it contains the description of all its components in a standard format so that each server can manage them.
6 – Conventionally description of application components is provided in XML format through a file named web.xml.
7 – Web.xml file is called deployment descriptor. It is used by application programmers to provide the configuration information of application components to the server.
8 – In servlet 3 API, annotations are introduced to be used for providing configuration of application components to the server in place of web.xml.[/one_half] [one_half_last] [/one_half_last]
Structure of a simple web.xml:
A web application may have components other than servlets as you would come to know in the later posts. For a simple web application which has only servlets, web.xml file will have following elements:
<web-app> <servlet> <servlet-name>objName</servlet-name> <servlet-class>className</servlet-class> </servlet> <servlet-mapping> <servlet-name>objName</servlet-name> <url-pattern>servletURL</url-pattern> </servlet-mapping> </web-app> ...
For each Servlet, a <servlet> & <servlet-mapping> element are required in web.xml. <servlet> element, tells the server by what name object of a servlet class is to be created. <servlet-mapping> element, tells the server which servlet object is to be used for processing the request of which URL.