Java Servlets in detail. Servlets life cycle

A servlet is a Java class that run at the server end, it is instantiated when needed and destroyed when not needed by the Servlet container. The init() and destroy() methods of Servlet interface will allow us to know when a Servlet has been initialized or destroyed by the Servlet container, but the are more methods to know about.

The Java Servlet written in this test will print out a message in the web server's log during the following events: the class loading, the instantiation, the initialization, the service and on the destroying. Thanks to this messages, and playing with the Servlet container while looking at the server's log, we will see when each life cycle event occurs. This way, we will verify when the Servlet is loaded, when is instantiated, how the service is called for every request we make, and how the instance of the Servlet is destroyed when we undeploy the application.

Using Tomcat Ant tasks

Using Tomcat Ant tasks or using Ant to manage Tomcat, whatever you prefer. In the last post, about the directory structure of a web application, I found a little tricky to update the application every time I made changes on it. To reflect the changes on the browser, I had to stop the server, manually delete the deployed application from the webapps folder, install it again and then restart the server. Very tedious. Fortunately, Tomcat 6 includes a convenient set of tasks definitions for the Ant build tool which will help us with this managing tasks. Tasks such as deploy a new web application or undeploy an existing one without having to restart the server.

Usually, to work with this capabilities, is used a web application that Tomcat 6 includes installed by default on context path /manager. Here, we will not configure this web application, instead of that, we will configure the Ant build tool to have the same advantages than the web application manager offers.

Java EE Web application structure

In this post, we are going to create a simple Java EE Web application from the scratch. The goal of this example is to show the folder structure used in the development process of a Java EE Web application. The application will be deployed on Tomcat 6, we will use Ant to automatize all the processes implied (cleanup, delete & create folders, compile, etc). The folder structure used in this example is defined by Sun Microsystems on its guidelines.

The guidelines suggest where to place each type of file generally present in a Java EE Web application. In the example we will not use all these file types, but the conventions have a suggestion for almost every existing file, such as Ant files, classes, Java source files, deployment descriptors, test files, external libraries, javadoc files, Web application ARchive (WAR), etc. Why should we follow this guidelines? Organizing your application as shown in these post and in consequence as shown in the Sun guidelines will make easier to manage and maintain the project in the future, saving time and effort. Keep this into your head, respecting the predefined folder structure and placing the files where they belong helps other developers working on the project, who will immediately know where is each resource. Do it for your fellows!

Installing Apache Tomcat on Ubuntu

This article is a guide to install and run Apache Tomcat 6 on Ubuntu 9.10. It covers version 6.0.20, but the steps are almost the same for any Tomcat 6 version. During the installation, we will create an script to help us control the server. This script will start, stop or restart the server depending on the parameters. Keeping the same folder structures used in previous posts, Tomcat 6 will be installed within the user's home folder, specifically, inside the servers folder (take a look at this post).

Apache Tomcat is an open source software implementation of the Java Servlet and JavaServer Pages technologies, Tomcat 6 implements the Servlet 2.5 version and JavaServer Pages 2.1 version. In others words, Tomcat supplies an implementation of the Java Servlet and JavaServer Pages specifications in the form of a Web container, and you, as application developer, should develop your applications in the form of components (Servlets and JSPs) and deploy them over this container. Where do I listened this before?

Java EE application tiers, containers and components

At first, the aim of this post was to talk just about the Java EE containers, but then I thought it would be a good idea to include a few words about the components hosted by these containers, and finally, I ended up wrapping all this stuff with the typical tiers of a Java EE application. Thus, the post title.

This paragraph summarizes what is being explained on the whole article. Java EE platform uses a distributed multi-tiered application model for enterprise applications, and these tiers have containers that work as a runtime environment for the different components that made up the Java EE application. What? Let's see it in detail...

Top