Web Application J2EE perspective

October 24 2009Not Commented

Categorized Under: Java

This tutorial is based on the following software environment.

Windows Vista Home Premium, Eclipse 3.5, JDK 1.6, Tomcat 6

A web application consists of both static and dynamic resources. Static resources include HTML, XML, images and script files. Dynamic resources include JSP’s and Servlets. In a typical web application the following elements are included.

  • JSP’s and Tag libraries
  • Servlets
  • Java classes such as Helper classes
  • JAR files including third party libraries which are used by application
  • Configuration files like web.xml
  • Static content like HTML, XML, CSS, script files, images etc

Every web application should maintain a directory structure. A web application always has root folder which is sometimes called document root. A sample directory structure is given in the below diagram.

webapplication_1

Under root folder we have one folder called WEB-INF. Under this folder we should keep configuration file called web.xml. This file consists of configuration information related to servlets, servlets mapping, welcome file list, mime mapping, filters, tag library alias etc.

Under WEB-INF we have another folder called classes. All servlets and Java classes should reside in this folder. If your classes are within package, sub folders corresponding to package should be created and classes should be kept in sub folder. In the above diagram you could see sub folder called example inside /WEB-INF/classes folder.

If you are using any third party JAR files like JDBC JAR files which are used by application you should keep it in lib folder under WEB-INF.

Any static content plus JSP’s should keep in root folder. Any resource kept under root folder except WEB-INF folder is available to public. This means user could access these resources using HTTP protocol. You are free to create sub directories under root folder for organising your static content. For eg, all images may be kept under images folder. In the above diagram you could see two folders, images and files. You may give any name to these folders unlike WEB-INF, WEB-INF/classes, WEB-INF/lib directories where you cannot change the name of those directories.

 Eclipse provides support for creating web application. Let us create a web application in Eclipse and analyze the directory structure.

If you haven’t had Eclipse IDE with Tomcat , refer the following tutorial.

Installing and Configuring Eclipse with Tomcat in Windows

 Open Eclipse. Java EE perspective is the default perspective. If you are not sure go to Window–>Open Perspective–>Other. Select Java EE and click OK.

Go to File–>New–>Dynamic Web project. Project Wizard appears.

servletexample_1

 Give the project name as WebApplication. All other options do not require modification if you are using Eclipse with Tomcat as server. If you have configured multiple servers you may change the target runtime. Click Finish button.

Go to Project Explorer and expand the project. You could find the following directory structure.

 webapplication_2

Source folder, Java Resources: src is where we create all packages and Java files including Servlets. All Java files we create in this folder are compiled and automatically places in WEB-INF/classes folder.

Any static content including JSP’s should be placed in WebContent folder. We may place HTML files, XML files, images, script files etc in this folder. You may create sub directories for grouping resources in this folder. For eg, images folder for keeping images. All resources we define in this folder are placed in application root folder.

If you want to make any changes to configuration file, open web.xml under WEB-INF folder. Any third party JAR files which is required by your application should be placed in WEB-INF/lib folder. For eg, JDBC JAR files.

Eclipse also provides functionality to package and archive web application so that you may deploy it in application server. In project explorer right click on project and select Export–>WAR file.

webapplication_3

Export wizard appears. Give the destination as full path WAR file. By default Eclipse do not export Java source files. If you want to include them, check export source file option. Press finish button.

webapplication_4

Now our WAR file is ready. Now you may deploy the same in any Java application server like Tomcat.

 cheers,

Leave a Reply

*