Internet programming
Servlets Configuration
Prof. Dr.-Ing. Holger Vogelsang
holger.vogelsang@hs-karlsruhe.de
Dipl.-Ing. Uwe Endress
privat.uwe.endress@isb-ag.de
0721 / 756460
http://www.home.hs-karlsruhe.de/~enuw0001/
Servlets
Contents
Architecture and Protocols
Dynamic HTML pages
Servlets
Installation prerequisites
Main structure and lifecycle
Evaluation of forms and HTTP headers
Cookies, session tracking
Java-Beans
JavaServer Pages
Combination of Servlets and JavaServer Pages
Expression language
Taglibs
Applets as front ends for Servlets
Holger Vogelsang, U. Endress Internet programming 2
Servlets
Installation prerequisites (private PC)
Tomcat as the servlet environment
Tomcat is a powerful servlet environment, which can be used as a plug-in for
many web servers.
Tomcat is freely available from the Apache Group.
Tomcat can also work standalone as a small web server.
request URL
Browser response Servlet engine
read
request Servlet-URL (+ parameters)
response
Java
Bytecode
Pages, Images, …
Files (Filesystem)
Holger Vogelsang, U. Endress Internet programming 3
Servlets
Installation prerequisites (private PC)
Directories of static and dynamic parts:
Write access required
(in the described
configuration)
Holger Vogelsang, U. Endress Internet programming 4
Servlets
Installation prerequisites (private PC)
Download source: http://tomcat.apache.org (download the binary core
distribution)
Please don’t install Tomcat in a path, containing white space characters.
If Tomcat is installed on Unix or Windows by an administrator, normal users
are not allowed to write to Tomcat directories.
Tomcat consists of two parts:
Static: Program files.
Dynamic: Configuration files.
Solution:
Assign program files to administrator.
Every user has its own configuration.
Holger Vogelsang, U. Endress Internet programming 5
Servlets
Installation prerequisites (private PC, limited rights)
LinuX-Installation, when Tomcat is executed with limited rights (separation of
static and dynamic parts, Windows is similar):
The user sets the environment variable CATALINA_BASE to the directory
/home//tomcat ( is a synonym for the real user name).
Extract the contents of file tomcat_dyn.zip (download from
Internetprogramming homepage) to /home//.
All directories in /home//tomcat must be assigned to user
(owner, permission).
Create your project in /home//tomcat/webapps/.
Expert hints:
Most examples don’t work in distributed environments.
Most examples don’t care about the synchronization of parallel requests.
Holger Vogelsang, U. Endress Internet programming 6
Servlets
Installation prerequisites (private PC)
Installing Tomcat
All examples were tested using Tomcat 5.5.x (Servlet specification 2.4).
Extract the contents of the file apache-tomcat-5.5.x.zip to any directory
(here named ).
To make your life easier, take the same directory as in the software pool
(c:\Programme\ApacheGroup\jakarta-tomcat-5).
Add the following line to setclasspath.bat (setclasspath.sh)
set JAVA_HOME=
Start /bin/startup.bat
(Windows) or
/bin/startup.sh
(LinuX or other Unix).
Browse to http://localhost:8080
Holger Vogelsang, U. Endress Internet programming 7
Servlets
Installation prerequisites (private PC) – Eclipse integration
Creating a working environment for Eclipse
Goal: Integration of Tomcat as our servlet environment into Eclipse.
Advantage: Servlets can be debugged!
request URL
Browser response Servlet engine Eclipse
request Servlet-URL (+ parameters)
main
response
read
execute
Java
Bytecode Project
Files
Pages, Images, …
(Filesystem)
Holger Vogelsang, U. Endress Internet programming 8
Servlets
Installation prerequisites (private PC) – Eclipse integration
Integrating Tomcat into Eclipse 3
Extract tomcatPluginV31.zip to /plugins
(http://www.sysdeo.com/sysdeo/eclipse/tomcatplugin)
This plugin helps you to
startup and shut
down Tomcat.
Start Eclipse.
Select menu Window
Customize Perspective
Mark Tomcat Project
(a subentry in Java):
Holger Vogelsang, U. Endress Internet programming 9
Servlets
Installation prerequisites (private PC) – Eclipse integration
Switch to Command tab and ensure that Tomcat is checked.
Holger Vogelsang, U. Endress Internet programming 10
Servlets
Installation prerequisites (private PC) – Eclipse integration
Select menu Window Preferences.
Select your Tomcat version and enter the path to this version.
Holger Vogelsang, U. Endress Internet programming 11
Servlets
Installation prerequisites (private PC, limited rights) – Eclipse integration
To use Tomcat in an environment with limited rights, select your server.xml
file (overwrite default value) required for pool!
Holger Vogelsang, U. Endress Internet programming 12
Servlets
Installation prerequisites (pool rooms)
Tomcat 5.5.9 is also installed in software pool E 203.
Configuration (only required for the first project):
Terminate Eclipse.
The tomcat installation directory is (without write permission)
c:\Programme\ApacheGroup\jakarta-tomcat-5.
Create a project directory: \tomcat
is somewhere in your own work directory.
During the first access of Tomcat, the dynamic parts should automatically
be copied to n:\tomcat.
If the previous action is not executed, copy the dynamic part of Tomcat into
your own project directory by extracting the file tomcat_dyn.zip
(download from Internet programming homepage) to n:\
Holger Vogelsang, U. Endress Internet programming 13
Servlets
Installation prerequisites (pool rooms)
Start Eclipse.
Select menu Window Customize Perspective
Check entry Tomcat Project (a subentry in Java):
Holger Vogelsang, U. Endress Internet programming 14
Servlets
Installation prerequisites (pool rooms)
Switch to the Command tab and ensure that Tomcat is checked.
Holger Vogelsang, U. Endress Internet programming 15
Servlets
Installation prerequisites (pool rooms)
Select menu Window Preferences.
Select your Tomcat version and enter the path to this version.
Holger Vogelsang, U. Endress Internet programming 16
Servlets
Installation prerequisites (pool rooms)
Select your server.xml file (overwrite default value).
Enter the path to your file: n:/tomcat/conf/server.xml
Holger Vogelsang, U. Endress Internet programming 17
Servlets
Installation prerequisites (pool rooms)
Enter the path to your dynamic files as advanced options:
Path to your
dynamic files:
n:\tomcat\
Holger Vogelsang, U. Endress Internet programming 18
Servlets
Creating the first project
Create a special Tomcat project (menu File New Project).
Select Tomcat Project:
Holger Vogelsang, U. Endress Internet programming 19
Servlets
Creating the first project
Assign a name to your project and select the directory (later referred as
):
In a LinuX system with limited rights, is
/home//tomcat/webapps/
Holger Vogelsang, U. Endress Internet programming 20
Servlets
Creating the first project
Enter the URI /IP:
Holger Vogelsang, U. Endress Internet programming 21
Servlets
Creating the first project
Eclipse needs to know the location of some libraries. Right click on the project
and select Properties.
Select Java Build Path and then Libraries:
Holger Vogelsang, U. Endress Internet programming 22
Servlets
Creating the first project
Add new libraries by selecting Add External Jars….
Search for servlet_api.jar and jsp_api.jar (stored in
/common/lib)
If the libraries are already selected, to nothing.
Holger Vogelsang, U. Endress Internet programming 23
Servlets
Creating the first project
Select Öffnen and your dialog looks like this:
Select Ok.
Holger Vogelsang, U. Endress Internet programming 24
Servlets
Creating the first project
Ensure that the project is automatically compiled:
Click on the project name (select it in the project explorer tree).
Check menu entry Project Build Automatically
Holger Vogelsang, U. Endress Internet programming 25
Servlets
Creating the first project
Now you’re ready to write your first servlet:
Create a new class
HelloWorldExample
and enter the superclass
for this servlet (see
right, the details will be
explained later):
Holger Vogelsang, U. Endress Internet programming 26
Servlets
Creating the first project
Add the following code to your servlet and save it:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldExample extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
Holger Vogelsang, U. Endress Internet programming 27
Servlets
Creating the first project
Your screen will look like this:
Holger Vogelsang, U. Endress Internet programming 28
Servlets
Creating the first project
For every servlet an entry in file web.xml is required:
Open the file /WEB-INF/web.xml.
You can download the configuration file for this first project from the
Internet programming homepage (file web.xml).
The file already contains some information.
The most important information can be found between
and .
Important notice: After changing this file, Tomcat automatically recognizes
the changes, if reloadable was not set to false in server.xml.
Holger Vogelsang, U. Endress Internet programming 29
Servlets
Creating the first project
The file contains a description of the Servlet (red marked parts):
HelloWorldExample
hska.faki.HelloWorldExample
HelloWorldExample
/servlet/HelloWorldExample
Holger Vogelsang, U. Endress Internet programming 30
Servlets
Creating the first project
Description (servlet)
...: The initialization for each servlet is enclosed in
such tags. The file can contain many servlets.
Name: A logical name for this servlet.
Class-name: The name of the
servlet class. If this servlet is part of a package, the full name has to be
inserted.
Description (servlet mapping)
...: Logical path to access this
servlet.
Name: Servlet to map.
URL: URL(s) to map onto this servlet.
Holger Vogelsang, U. Endress Internet programming 31
Servlets
Creating the first project
Start Tomcat (menu Tomcat Start Tomcat):
Holger Vogelsang, U. Endress Internet programming 32
Servlets
Creating the first project
Browser to the following URL:
localhost:8080/IP/servlet/HelloWorldExample
Holger Vogelsang, U. Endress Internet programming 33
Servlets
Creating the first project
Tomcat automatically reloads modified Servlets:
Select project properties:
check
Holger Vogelsang, U. Endress Internet programming 34
Servlets
Creating the first project
Export project settings to server.xml file (right click project, then
follow screenshot):
server.xml now contains the following line (near end of file):
Don’t do this in real applications (performance killer!!!).
Holger Vogelsang, U. Endress Internet programming 35
Servlets
Creating the first project – directory structure
Project directory structure:
/: Basic directory for a web application. Contents:
*.html, *.jsp, Images, JavaScript-Code, Styles, … (or in subdirectories)
WEB-INF/web.xml: The Web Application Deployment Descriptor for your
application.
WEB-INF/classes: Any Java class files (and resources) required for your
application, including both servlet and non-servlet classes, that are not
combined into JAR files. If your classes are organized in Java packages,
you must map this to the directories under WEB-INF/classes/.
WEB-INF/lib: This directory contains JAR files that contain Java class
files (and associated resources) required for your application, such as third
party class libraries.
To use the lecture examples, extract the file IP-Tomcat.zip to your project
directory:
Working as an administrator: Extract the files to /webapps.
When working with limited rights: Extract the files to .
Holger Vogelsang, U. Endress Internet programming 36