Sunday, February 1, 2009

Web Application Structure

This blog intends to answer the following questions :-
  1. What is the structure of a simple web appliation in java using jsp pages, servlets.
  2. What are the contents of a web archive(war) file?
  3. How to create a web archive?
A web application is an application that is designed to respond to requests from web client and can be executed only on a server. In most of my application, I use the Topmcat server for running simple web applications.

Firstly a web application must has the following componenets/files/folders :-
  1. An application root folder. e.g myapp. This would be the name of your application.
  2. A folder called 'WEB-INF'. No lower case.
  3. An xml file called 'web.xml' called the deployment descriptor.
  4. A folder called 'classes'.
  5. A folder called 'src'.
  6. A folder called 'lib'.
All these folders have to be arranged in the following structure.
  1. myapp.
  2. myapp/WEB-INF.
  3. myapp/WEB-INF/web.xml.
  4. myapp/WEB-INF/src.
  5. myapp/WEB-INF/classes
  6. myapp/WEB-INF/lib.
This is all there is to the strucure of a web aplication so that it can be automatically be deployed by the container.

The usage of the different componenets is as follows :-
  1. web.xml :- This is the most important heart and soul of your web application. It contains information about the web application in the form of xml configuration. It is used to link servlets and jsps and filters to the various user requests.
  2. src :- This folder is used to contain the source code of yout applications i.e. all java files.
  3. classes :- This folder is supposed to contain al the compiled class files in the proper directory/package structure.
  4. lib :- This folder is supposed to contain all the jar files that would be required by your applications at runtime.
Apart from these files and folders you can have any number of files and folders in your application. But the only thing that you need to keep in mind is that all files and folders placed inside the WEB-INF directory are not available to the client 'directly', i.e. the client cannot access them via typing in a url in the browser.

For example, if you have a 'fun.jsp' file in WEB-INF, the client just cannot type 'http://myserver/myapp/WEB-INF/fun.jsp' to get the file. This would result in an http 404 error. To access this 'fun.jsp', this file should have an approprite mapping in the web.xml file.

All the 'files and folders' that need direct client access vial the browser should be placed outside the WEB-INF folder.

Creating a war file is simple.

Just go to your application directory. Here my application directory is 'myappp'. So I execute the following commands on my windows machine.
  1. cd myapp
  2. jar -cvf myapp.war *.*
This command creates a new web archive called 'myapp.war'. This can then be deployed by any servlet container. For how ro deploy web applications, refer to your server manual.

I suppose that this introduction should be good enough to give you a head start.

Happy Programming ;)

Signing off

No comments: