Your First JSP on Apache Tomcat Server

apache tomcat tutorial

If you want to run Java Servlets or JSP pages on your web server, you probably want to install the Apache Tomcat Server. If you’re familiar with Apache, Tomcat will come as second nature to you. Tomcat is an open-source, free HTTP server available for both Linux and Windows operating systems. The best way to learn the system is to create a simple web application. This article walks you through your first JSP setup to get you familiar with the Tomcat platform.

Installation

A detailed set of instructions on how to install and configure Apache Tomcat is considered to be out of scope for this tutorial. However, the following outlines must be followed during the installation process:

The target machine on which Tomcat is to be installed must have a Java Runtime Environment available. For the purpose of this tutorial, we assume that the target operating system is Microsoft Windows (XP upwards) and that Java version 7 (32 bit) from Oracle Technologies has been installed therein.

The latest binary distribution of Apache Tomcat must be downloaded and installed. Since the target operating system is Windows, it is recommended that the distribution file ending with a .zip extension be used. For the purpose of this tutorial, the target installation folder is assumed to be C:\Tomcat.

Note that with the default set of configuration available out-of-the-box, a Tomcat installation will run on port 8080. This is in order to avoid port contention with any existing Web server already installed on the target machine.

Learn more about Apache Tomcat by taking a course at Udemy.com

Folder Structure

After installation, the contents of the folder C:\Tomcat should include the following:

 

bin – startup scripts required to start and stop Apache Tomcat.

conf – configuration files for Apache Tomcat.

lib – JAR files containing the necessary J2EE specifications and Apache Tomcat implementation

webapps – root folder containing all web applications hosted on this installation of Tomcat.

 

Understanding Web Applications Deployment

For every web application deployed on Apache Tomcat, you will see a sub-folder under the webapps folder containing the corresponding implementation. The name of the sub-folder is used as the relative path in the URL that is used to invoke the application.

For example, if the web application is placed in the folder named test under webapps, it is accessible from within a Web browser via the URL, http://localhost:8080/test. This is assuming that the web browser is launched from the same machine that hosts the Tomcat server (developer setup).

It is customary to distribute complete web applications in a single binary file that ends with the .war extension. Not surprisingly, this file is colloquially referred to as a WAR file. A WAR file is essentially a ZIP file that internally has the same folder structure and contents as the application sub-folder under the webapps directory.

It is customary to place the WAR file within the webapps folder of a Tomcat installation. Next time Tomcat is run, it automatically extracts the WAR file into a sub-folder under webapps. All subsequent behavior assumes the sub folder to be the root of a web application.

For example, given a WAR file with the name ‘test.war’ placed in the webapps folder, the Tomcat server will automatically extract the same into a sub-folder named ‘test’ under the webapps folder. Thereafter, the web application is accessible from within a Web browser via the URL, http://localhost:8080/test.

Note that if a sub-folder with the name ‘test’ already exists, the WAR file is not extracted provided the timestamp of the ‘test’ folder is older than the file ‘test.war’.

Creating Our Web Application

For the purpose of this tutorial, our Web application is named ‘myfirstwebapp’. This means that the URL to access this application is http://localhost:8080/myfirstwebapp.

Step 1:

Our first Web application will be deployed in an expanded format and not as a WAR file. To do so, create a folder named ‘myfirstwebapp’ under C:\Tomcat\webapps.

As per the J2EE specification, any web application must at least have a sub folder named WEB-INF and file named web.xml placed therein. This file is called the web application assembly descriptor and contains custom instructions for the Tomcat server to work with the corresponding web application.

Step 2:

Create the folder named ‘C:\Tomcat\webapps\myfirstwebapp\WEB-INF’. Then fire up your favourite text editor to type in the following XML code:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0"

metadata-complete="true">

 

<display-name>myfirsetwebapp</display-name>

<description>

My First Web Application.

</description>

</web-app>

Save the file as ‘C:\Tomcat\webapps\myfirstwebapp\WEB-INF\web.xml’ Note that since we will be dealing with only simple JSP for this tutorial, further instructions in the web.xml file (e.g. servlet and filter configurations) are not necessary.

Learn about Apache Hadoop by taking a course at Udemy.com

Step 3:

The JSP page for this tutorial will display on screen the HTTP details received from the browser when it invoked the URL that points to this page. Fire up your favorite text editor to type in the following:

<%@ page import="java.util.*" %>

<html>

<head><title>Echo JSP</title></head>

<body>

 

<h1>Incoming HTTP Request</h1>

<table border="0" cellpadding="3" cellspacing="0" width="100%">

<%

Enumeration eNames = request.getHeaderNames();

while (eNames.hasMoreElements()) {

String name = (String) eNames.nextElement();

String value = normalize(request.getHeader(name));

%>

<tr><td><%= name %></td><td><%= value %></td></tr>

<%

}

%>

</table>

</body>

</html>

 

<%

private String normalize (String value) {

StringBuffer line = new StringBuffer();

for (int i = 0; i < value.length(); i++) {

char c = value.charAt(i);

line.append(c);

if (c == ';') {

line.append("<br>");

}

}

return line.toString();

}

%>

Save this file under ‘C:\Tomcat\webapps\myfirstwebapp\index.jsp’. Recall that a file with the generic name of index(.htm | .html | .jsp) is returned when a web application is invoked using a relative path, e.g. http://localhost:8080/test without mention of a specific page or URL.

Now open a Web Browser and load the URL http://localhost:8080/myfirstwebapp. This will cause the ‘index.jsp’ file to load and execute on the server with the output HTML document sent to the browser for display. The page displayed on the browser shows all headers that were sent to the page as part of the HTTP request.

Learn all about Apache Hadoop by taking a course at Udemy.com

Conclusion

This article shows you how to create and run a basic JSP that is useful for debugging purposes. All relevant server-side logic along with UI data is present in the same file. In real life, you should consider a more elegant approach of a) having multiple JSP pages and b) have all execution logic moved into Java classes that are loaded and invoked from within JSP pages.