Friday, April 29, 2011

Chapter 47: Introduction to Web Application Deployment

Web Applications are not like the regular stand-alone java applications. You cannot just write a java class and run it. The components of a web application like JSPs, Servlets, XMLs etc will not run properly unless they are all compiled and placed in the proper directories. There are numerous other settings that you need to do as a web application developer in order to get your code running.

In the previous many chapters, we learnt in depth about JSPs and Servlets. The next step is to learn how to deploy the various components of your web application. This is exactly what you are going to learn in this and the next few chapters.

So, lets get started!!!

Web Application and Web Archive Structure

Any Web Application has a hierarchical structure (of folders) that is used for packaging the web applications.

The application needs a reference point within the hierarchical structure. Here, let's call it TOMCAT_HOME. This represents the root of your server (Tomcat) installation, not your Web application root. Remember that we have been using Tomcat as the server of our choice as part of this SCWCD certification series.

When we talk of a Web application's context, we are referring to that Web application's root directory or path within a particular server. A special directory exists within the application hierarchy named WEB-INF. This directory contains all things related to the application that aren't in the document root of the application. It is this WEB-INF directory that is the Web application's root directory, also referred to as the context. The most important file here is web.xml, the name of the WebApp deployment descriptor.

Let us take a look at the various contents (files & folders) that will be present inside this WEB-INF directory:
1. /WEB-INF/web.xml – This is the Deployment Descriptor file
2. /WEB-INF/classes/ - This is the directory where all your java class files will be placed
3. /WEB-INF/lib/ - This is the folder where all your JAR files go.

The following is the Web application structure for a small sample Web application:
/WEB-INF/classes/com/ myCompany/util/Util.class

Although the actual paths are not on the exam, seeing where the files actually go illustrates what is going on.

For ex: my tomcat is installed in c:\dev\java\jakarta-tomcat-4.0.1. This is my TOMCAT_HOME. Remember that, this path that is referred by TOMCAT_HOME might vary from Pc to Pc depending on the installation preferences of the user.

The good thing is, Tomcat knows to look in c:\dev\java\jakarta-tomcat-4.0.1\webapps director for web applications.

From here you will configure Tomcat, or another container, with XML files. For example, Tomcat installs with an example application. In the C:\dev\java\jakarta-tomcat-4.0.1\conf or just TOMCAT_HOME\conf directory there is a server.xml file. Tomcat uses this to configure itself upon starting. There is a context element that you use to map your application's URL to the actual directory path.

The following is how you get an examples application:

< ! - - Tomcat Examples Context - - >
< Context path="/examples" docBase="examples" debug="0" reloadable="true" >

So, now that you have an examples Web application, the actual root directory is this: TOMCAT_HOME\webapps\examples. If you had the file TOMCAT_HOME\webapps\examples\hello.jsp you would call this file with http://localhost:8080/examples/hello.jsp.

Whenever you type a URL in your browsers address bar, Tomcat converts the URLs into corresponding Paths that it can understand and process. They are:

Description URL Actual Directory
installation root - C:\dev\java\jakarta-tomcat-4.0.1 This is TOMCAT_HOME.
Server root http://localhost TOMCAT_HOME\webapps
Web application http://localhost:8080/examples TOMCAT_HOME\webapps\examples
Static resource http://localhost:8080/examples/index.html TOMCAT_HOME\webapps\examples\index.html
JSP http://localhost:8080/examples/hello.jsp TOMCAT_HOME\webapps\examples\hello.jsp
Servlet http://localhost:8080/examples/servlet/HelloWorldExample TOMCAT_HOME\webapps\examples\WEB-INF\classes\HelloWorldExample.class
By now, you should have a fair idea of the folder structure of web applications.

There is a lot more to learn about deployment and packaging, which we will be looking in depth in the subsequent chapters. For now, this chapter is over.

Previous Chapter: Chapter 46 - JSP API

Next Chapter: Chapter 48 - Elements of the Deployment Descriptor

1 comment:

  1. It's been a pleasure reading your blog. I have bookmarked your website so that I can come back & read more in the future as well.


© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.


Google+ Followers