7.2. CGI vs. Servlets/JSPs

Servlets/JSPs are executed in the same Java Virtual Machine process in separate threads.
This leads to very optimized and efficient system utilization.

CGI scripts on the other hand also utilize the system, but very inefficiently.
Many CPU cycles are spent on starting/scheduling new processes, re-loading the interpreter, and re-parsing the scripts.
Sharing of data is achieved through some external system, like RDBMS, file system, or another process.

7.3. Tomcat Web Container

Apache Tomcat (6.x) is a free and open source Servlet (2.5) and JSP (2.1) Container

Embedded in JBoss AS as deploy/jbossweb.sar

JBoss AS configuration for Tomcat integration in each application are located in META-INF/jboss-web.xml

While most of the settings in this file can be left as default, the JSP servlet has a number of options (http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html) that we may wish to change before putting JBoss in production. These include: development, modificationTestInterval, checkInterval, classdebuginfo, suppressSmap, mappedfile, trimSpaces, and genStrAsCharArray.

Note

While the deployment descriptors are usually the responsibility of developers and application assemblers, this web.xml file is global to the entire web container and as such it is owned by the administrators.

7.6. Defining and Mapping Servlets

In WEB-INF/web.xml file:

Define a servlet with <servlet> where <servlet-name> points to a fully qualified <servlet-class>

Optionally initialize with <init-param>-s

Optionally <load-on-startup>

Map a defined servlet with <servlet-mapping> where <url-pattern> points to previously specified
<servlet-name>

The <location> is relative to the root of the application and it must start with a slash (/) character.

Note

Browsers such as Microsoft?s Internet Explorer and Google?s Chrome do not show server error pages (instead they show friendly error pages)
unless those pages are either styled (e.g. contain references to style sheets, images, etc.) or exceed 512 bytes

For example, to share images across multiple applications, create a directory deploy/ROOT.war/images/ and place your images there.
A request for http://localhost:8080/images/cool.png would be served from ROOT.war/images/cool.png

7.12. Virtual Hosting with Tomcat

Add hosts and aliases to server.xml:

<Host name="myhost.com">
<Alias>www.myhost.com</Alias>
</Host>

Register applications for virtual hosts in their WEB-INF/jboss-web.xml files:

7.14. Lab: Tomcat

Define a HTTP 404 error handler for the ROOT.war web application and test it (You can find help here)

A simple 404.html error document could look like:

<html>
<head><title>File Not Found</title></head>
<body>
<h2>The file you requested has not been found</h2>
<p><a href="http://google.com">Search for it?</a></p>
</body>
</html>

Note

Internet Explorer will not show the error document (it will use its own error page) if the content is smaller than 512 bytes. Either add more content to your error page or use a different browser, like Firefox.

Configure two virtual hosts

Use localhost and the name of your machine or

Invent new hostnames and add them to hosts file

The location of this file is dependent on the operating system:

/etc/hosts (on most UNIX-type operating systems)

C:\Windows\System32\drivers\etc\hosts (on Windows)

Alternatively, use vhost1.marakana.com and vhost2.marakana.com which simply point back to 127.0.0.1.
In fact, www.vhost1.marakana.com and www.vhost2.marakana.com also point to the same loop-back address.

Deploy some static content and test that you get retrieve it through your browser