Basci Java Servlet/JSP Web Development

jetmorebrisketSoftware and s/w Development

Aug 15, 2012 (5 years and 11 months ago)


May 13th, 2003

@2003 Lucek Consulting

Basic Java Servlet/JSP Web

David Lucek

Lucek Consulting

May 13th, 2003

Download the Sample Application

Download from
, select the
downloads tab

Includes the full source

Unzip to c:

drive or $HOME/lucek

May 13th, 2003

What is a Servlet?

Java Servlets/JSP are part of the Sun’s J2EE
Enterprise Architecture

The web development part

Java Servlet

is a simple, consistent mechanism for extending the
functionality of a web server

Are precompiled Java programs that are executed on the
server side.

Require a Servlet container to run in

Latest Servlet Spec is 2.3

May 13th, 2003

What is a Java Server Page (JSP)

Java Server Pages (JSP)

A simplified, fast way to create dynamic web content

HTML or XML pages with embedded Java Code or
Java Beans

Can be a mix of template data in HTML/XML with
some dynamic content

A JSP is a complied to a Java Servlet automatically
by the Servlet container, it is then cached

Latest JSP Spec is 1.2

May 13th, 2003

Why Use Servlets?

Work well in a Heterogeneous Environments

OS and platform neutral

Work with all major web servers (IIS, Apache,etc..)

Well defined Web Architecture framework

Standard built in services such as:

Standard Approach to Authentication using declarative security
vice programmatic security

Database connection pooling

Complete support for sessions via cookies and/or URL re

Has automatic fallback to URL re

May 13th, 2003

Why Use Servlets Con’t?

Robust Object
Orientated API in Java language

Ever try to maintain a large ASP, Perl, or PHP site

Clean separation of Controller/Logic from

Efficient, scales very well

There are good free Servlet/JSP containers
and connectors

That run under both UNIX and win32

May 13th, 2003

J2EE Web Application Components

Java Servlets

Extend off of HttpServlet

JSP pages, normally for Presentation

Java Beans

Normally used as value objects, pass to data to JSPs

Tag Libraries

XML based JSP elements

Web Deployment Descriptor


May 13th, 2003

Web Deployment Descriptor


Part of the standard

Defines servlets used in the web application

Maps servlets to URLs

A servlet can map to many URLs

Defines resources available to the web app

Defines security constraints

Defines other stuff like

Welcome file list

Session timeout

Error page mapping

May 13th, 2003

J2EE Web Directory Structure 1

Top Directory is normally the context Path


Normally, the URL would be

Contains JSP and other static content plus the web

inf directory

This is a protected directory, can not point browser to any file
in this directory


unpacked web application classes, auto
added to CLASS_PATH


web application JAR files


tag library descriptor files

May 13th, 2003

J2EE Web Directory Structure 2



Would normally put any static or JSP files here

Protects them from Direct Invocation

Always best to call a JSP through a servlet first

May 13th, 2003

JSP Constructs 1

Used in JSP pages, pages that end *.jsp

Comment <%


Declaration <%! int x = 0; %>

Expression <%= expression %>

Outputs to the Response stream

Like a “printf” to the browser

Do NOT use semi
colon to terminate the line


contains Java Code

<% code fragments %>

May 13th, 2003

JSP Constructs 2

<% if (value.getName().length != 0) { %>

<H2>The value is: <%= value.getName() %></H2>

<% } else { %>

<H2>Value is empty</H2>

<% } %>

Implicit objects always available in the JSP Page


Browser’s Request Object

Use to get HTTP headers, length etc..


HttpResponse Object

May 13th, 2003

JSP Constructs 3


internal HttpSession Object



“out”, same as <%= %>


servlet configuration



JSP Directives

Are messages or instructions to the JSP container

May 13th, 2003

JSP Constructs 4

Do not produce any output

“page” directive

<%@ page import=“com.lucek.*” %>

Commonly used for importing class paths

“include” directive

<%@ include file=“header.htm” %>

Good for including static content


lists the tag library descriptor location

Required when using tab libraries

May 13th, 2003

Java Beans as Used in Web Apps

Normally used for all data transfers and business

Similar to how Java Beans are used in Swing and

But do not need the full implementation

Must have no constructor or no
arg constructor

Must have setter and getter methods for each property

JSP constructs/tags use Java Beans

May 13th, 2003

JSP Actions

JSP actions are special tags that affect the
output stream and are normally used with Java

Most commonly used:

<jsp:useBean>, <jsp:getProperty>, <jsp:setProperty>

The code below will display the lastName property of the
student bean on the output stream

<jsp:useBean id="student" scope="request"
class="com.lucek.dto.StudentValue" />

<jsp:getProperty name="student" property="lastName" />

May 13th, 2003

Servlet Container/Engine

Servlets/JSP require a Container

Apache Tomcat is the reference implementation of the
Servlet/JSP Specs

It is open source, small, install quickly,and is FREE

Latest Version is 4.1.24

Web Site:

It include a simple HTTP 1.1 server, good enough for
development and small intranets.

May 13th, 2003

Tomcat Install

Requires a JDK, get 1.4.1 and install into c:
or $HOME/jdk

Add JAVA_HOME to your environment and the
“bin” directory to your PATH

Good practice to unpack into c:
tomcat or

Add CATALINA_HOME to your environment
and the “bin” directory to your PATH

May 13th, 2003

Tomcat Directory Structure

Everything is relative to $CATALINA_HOME


Startup/shutdown scripts



main configuration file


common class and jar files used by Tomcat
and web applications

Put JDBC drivers here


class and jar files used by Tomcat internally


class and jar files for all web applications


This is where you put your web application
in a sub
directory or external context file.

May 13th, 2003

Starting Tomcat

/bin/startup.bat or

Point Browers to
, should
see default page

All the Docs are there on the default page!

Check out the examples pages, good tutorials

May 13th, 2003

Other Development Tools 1

Ant Build Tool

Standard Java Build tool

Basic on UNIX make, but much better


Install in c:
ant or $HOME/ant

Java IDE

Try NetBeans, it is nice

Tomcat is built in, but is an older version

Includes full Servlet and JSP debugging


May 13th, 2003

Other Development Tools 2


Standard Automated Unit Testing Tool



Slick Programmer’s Editor

Written in Java


May 13th, 2003

Simple Servlet Application 1

See “servletdemo” code

Mount the servletdemo, servletdemo/java/src, and
servletdemo/web in NetBeans Explorer Tab

For a Hello World Servlet look at:


To build and run

$ cd servletdemo

Setup the proper build variables in the file

$ ant all

$ ant deploy

Point your browsers at http://localhost:8080/servletdemo

May 13th, 2003

Simple Servlet Application 2

Look at the web.xml file and how the same
servlet can be mapped to many URLs

Look at which should how to
pass a Java Bean to a JSP page

Look at the different ways a bean’s value can
be obtained in the EditStudent.jsp

May 13th, 2003

Best Practices/Patterns

Always Separate out the logic from the presentation

Use servlets for the logic/controller and JSP’s for presentation

Ideally should never have Java Code in the JSP page

Have a clean separation between your data access
and controller layers (DAO)

Always use DTO or value object

Use a Model
Controller Architecture

Do not write it, use Struts


Use Unit tests

Junit Automation via Ant build tasks

May 13th, 2003

What we have not talked about

All the specific Servlet APIs

Tag libraries

Sessions, cookies

JDBC service support from the container

Container based authentication

Lots of other stuff

May 13th, 2003

Next Presentation?

Create a data driven web site using MySql and

Setup Authentication Realm with declarative

Setup JDBC connection pooling