Web Applications

Tomcat administrators do not need to be a Java or Web developer, but an understanding of the technology will help greatly, this page introduces you the evolution of dynamic web sites starting at the beginning.

Web Application History

When web sites were first available the content was static, they merely served up HTML pages. The protocol used was Hypertext Transfer Protocol (HTTP) which was a simple stateless protocol. However with the growing desire to create fancy web content and keeping track of users habits over time dynamic content was born.

The first mechanism to serve dynamic content was Common Gateway Interface (CGI), this was a executable application (usually written in Perl) with an interface that embedded clients to access them in a standard way over HTTP. Within the URL parameters could be passed to the CGI script, the script then executed and replied but to the user, thus pages were dynamically created.

However CGI had several drawbacks

Server Side Java Servlets

Servlets are portions of logic written in Java that have a defined form and are invoked to dynamically generate content or perform some action. Servlets overcome the problems with CGI scripts

Servlets implement an interface called Servlet, which defines a standard lifecycle, three methods are used

init() Used to initialize any resources that the servlet may need i.e. Database. This method is called once for each instance of the servlet.
service() This method is called for each client request, this method controls the computation and generation of the response that is returned to the client. The response is returned to the client then the servlet waits until the next request, the type of request (GET or POST) is recorded and forwards the request to methods defined for handling these requests.
destroy() this method is called once before the the Servlet class is disposed of, this can be used to free any resources that the servlet was using.

Many vendors should stick to the specifications thus allowing servlets to run in any of the compliant containers without modification. There are three types of Web server and servlet container that can be used

The servlet container can maintain state information by either using cookies or using URL rewriting, objects are stored is a users session and are available to other servlets in subsequent client requests.

Servlets are programming resources, thus the server maps URLs to programmatic resources, it uses a logical mapping which maps the URL context path to the servlet, thus you can add more descriptive information in the URL as to what the servlet does.

There are drawbacks with servlets, there purpose is used for processing logic because hard coding textual output (including HTML tags) in code makes the application less maintainable because when text in the HTML must be changed, the servlet must be recompiled, also the HTML programmer has to know about Java thus not breaking the servlet when changes are made, to solve this problem JavaServer Pages (JSP) was created by Sun Microsystems.

JavaServer Pages (JSP)

JSP uses tag libraries, these libraries are collections of custom tags and each tag corresponds to a reusable Java module.

JSP example

<% page language="java" %>
   <title>Hello World</title>
 String message = request.getAttribute("message");
 if(message == null || message.equals("")
   message = "Hello World";

Behind the scenes, the JSP is compiled into a servlet class the first time it is invoked, the servlet is then called for each subsequent request, avoiding the parsing and compiling of the JSP everytime a user accesses the site. Like servlets, JSP's operate within a container, the only difference is that a JSP has to be compiled before the JSP is executed. Tomcat uses the Catalina servlet container to execute servlets and compiled JSP's, it also uses the Jasper compiler to compile JSP's, this combination is know as a Web container.

I am only going to explain the new modern web development model called "Model 2 Architecture and Web Frameworks", this framework outlines best practices and includes some ground rules for design web sites

JSP's are an improvement over servlets but they had a lot of Java code embedded within them, thus the web designer needed to understand HTML and Java in order to make changes. To separate the code we use tag libraries, the tag points to a Java class that contains the code that would otherwise would have been executed.

JSP tag library example

<% page language="java" %>
   <title>Hello World</title>

Note: notice the difference with the previous example above, this code is easier to read

By using tag libraries the web designer can happily change the code without affecting the Java code, plus it's easier to read the code.

JSP EL defines an easy-to-use syntax for accessing Java Beans, request/session parameters and HTTP headers from JSPs. It also has arithmetic, logical and conditional expressions thus doing away with the need for any Java scriptlet code inside a JSP.

MVC Architecture

The model 2 architecture is also know as the Model View Controller (MVC) architecture, this architecture separates the application logic and presentation of HTML content

Model (M) is the logic of the site, the rules that determine what is shown and to whom it is shown. In other words it knows the rules for getting and updating the state, for an example a shopping cart contents would be part of the Model in MVC.
View (V) is the component of this architecture it is the JSPs that displays the content that is created, it is responsible for presentation, it gets the state of the model from the Controller. It also passes information to the controller so that the model can use it (like form data).
Controller (C) designates which part of the Model is invoked and which JSP is used to render the data. It defines the structure of the web site and the page flow logic basically takes user input from the request and figures out what it means to the model. Tells the model to update itself and makes the new model state avaiable for the view (JSP).

There are two types of MVC, loose and strict