0% found this document useful (1 vote)
128 views

Servlet Intro

This document provides an introduction to servlets. It discusses the servlet architecture and lifecycle, comparing servlets to other server-side technologies like CGI and ASP. Servlets are presented as an efficient, convenient, powerful, portable, secure and inexpensive way to build dynamic web applications in Java. The document also introduces Java Server Pages (JSP) as a related technology that can be used with or instead of servlets.

Uploaded by

api-3802159
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
128 views

Servlet Intro

This document provides an introduction to servlets. It discusses the servlet architecture and lifecycle, comparing servlets to other server-side technologies like CGI and ASP. Servlets are presented as an efficient, convenient, powerful, portable, secure and inexpensive way to build dynamic web applications in Java. The document also introduces Java Server Pages (JSP) as a related technology that can be used with or instead of servlets.

Uploaded by

api-3802159
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Servlets:

Introduction to Servlets
Ethan Cerami
New York University

10/17/08 Intro to Servlets 1


Road Map
 Servlet Architecture Overview
 Servlets in Context
 Other options for server side development
 Advantages of Servlets
 Introduction to Java Server Pages
(JSP)
 Servlets v. JSP

10/17/08 Intro to Servlets 2


Architectural Overview

10/17/08 Intro to Servlets 3


What is a Servlet?
 Java’s answer to the Common Gateway
Interface (CGI).
 Applet: a java program that runs within
the web browser.
 Servlet: a java program that runs within
the web server.
 Rapidly becoming the standard for
building web applications.

10/17/08 Intro to Servlets 4


Life of a Servlet
 Regardless of the application, servlets
usually carry out the following routine:
2) Read any data sent by the user
 Capture data submitted by an HTML form.
3) Look up any HTTP information
 Determine the browser version, host name of
client, cookies, etc.
4) Generate the Results
 Connect to databases, connect to legacy
applications, etc.

10/17/08 Intro to Servlets 5


Life of a Servlet (cont.)
1) Format the Results
 Generate HTML on the fly
2) Set the Appropriate HTTP headers
 Tell the browser the type of document
being returned or set any cookies.
3) Send the document back to the client

10/17/08 Intro to Servlets 6


Life of a Servlet
Database

1,2
Web Web Java
Browser 6 Server 4,5 Servlet

10/17/08 Intro to Servlets 7


What can you build with Servlets?
 Search Engines
 Personalization Systems
 E-Commerce Applications
 Shopping Carts
 Product Catalogs
 Intranet Applications
 Groupware Applications: bulletin boards, file
sharing, etc.

10/17/08 Intro to Servlets 8


Servlets in Context

10/17/08 Intro to Servlets 9


Server Side Options
 There are many options for creating server
side applications.
 We will examine some of these options
briefly.
 This better enables us to understand servlets
within the broader context of web
development.
 Also enables us to better understand the
advantages and disadvantages of servlets.

10/17/08 Intro to Servlets 10


Server Side Options
 Common Gateway Interface (CGI)
 Fast CGI
 Mod Perl
 ASP
 PHP
 Cold Fusion

10/17/08 Intro to Servlets 11


Common Features
 All server side frameworks share a
common set of features:
 Read data submitted by the user
 Generate HTML dynamically based on
user input
 Determine information about the client
browser
 Access Database systems
 Exploit the HTTP protocol

10/17/08 Intro to Servlets 12


Decision Points
 When evaluating which server side
framework to use, you need to consider a
number of critical factors:
 Ease of development:
 How easily can you build new applications?
 Performance:
 How fast can the framework respond to queries?
 Scalability:
 Can the framework scale to thousands, millions of
users?
 Security:
 Are there any inherent security vulnerabilities?

10/17/08 Intro to Servlets 13


Option 1: CGI
 Represents one of the earliest, practical
methods for generating web content.
 Primarily written in the Perl
programming language.
 Unfortunately, traditional CGI programs
suffer from scalability and performance
problems.
 Let’s examine these two problems…

10/17/08 Intro to Servlets 14


CGI Architecture
1) Browser initiates request
2) Web server receives the request.
3) For each request, web server spawns a new
operating system process to execute the CGI/Perl
Program.

Create
Web Web New process
Perl/CGI
Browser Server

10/17/08 Intro to Servlets 15


CGI Architecture
 For each browser request, the web
server must spawn a new operating
system process.
Perl 1
Browser 1

Browser 2 Web
Server Perl 2
Browser N

Perl N
10/17/08 Intro to Servlets 16
CGI Architecture
 Spawning a new operating system
process for each request takes time
and memory.
 Hence, traditional CGI programs have
inherent performance and scalability
problems.
 Every other server architecture tries to
address these problems.

10/17/08 Intro to Servlets 17


Option 2: Fast CGI
 Developed by Open Market as an option for
developing faster, more scalable CGI programs.
 Fast CGI works by creating a pool of processes
for handling CGI requests.
 When a CGI request comes in, Fast CGI picks
one of the processes from the pool and assigns it
to the task.
 Without the overhead of creating new operating
system processes, FastCGI is much faster than
traditional CGI.
 For more information, see http://www.fastcgi.com

10/17/08 Intro to Servlets 18


Option 3: Mod Perl
 A module of the Apache Web Server.
 Embeds the Perl interpreter directly within the
web server.
 Perl programs are therefore precompiled.
 Because Perl is embedded within the Server,
Mod Perl does not need to create a new process
for each request.
 Like FastCGI, Mod Perl is much faster than
traditional CGI.
 For more information, see: http://perl.apache.org

10/17/08 Intro to Servlets 19


Option 4: ASP
 Active Server Pages
 Runs on Microsoft’s Web Server: Internet
Information Server (IIS)
 Programmers add ASP code directly into their
HTML pages.
 When a client requests a page, the Web
Server takes the HTML page, runs the ASP
code within the page, and returns a complete
HTML page.
 Faster than traditional CGI, but only works on
Microsoft IIS.
10/17/08 Intro to Servlets 20
Option 5: Cold Fusion
 Developed by Allaire Corporation (now owned
by Macromedia.)
 Provides excellent database access and
database tools.
 Great platform for rapid prototyping and rapid
development.
 For more information:
http://www.macromedia.com

10/17/08 Intro to Servlets 21


Option 6: PHP
 An open source project written entirely
by volunteers
 Provides simple, but powerful database
access.
 Also great for rapid development.
 For additional information:
http://www.php.net

10/17/08 Intro to Servlets 22


Advantages of Servlets

10/17/08 Intro to Servlets 23


Advantages of Servlets
 Servlets have six main advantages:
 Efficient
 Convenient
 Powerful
 Portable
 Secure
 Inexpensive

10/17/08 Intro to Servlets 24


Advantage 1: Efficient
 For each browser request, the servlet
spawns a light weight thread.
 This is faster and more efficient that
spawning a new operating system
process.
 Hence, servlets have better
performance and better scalability than
traditional CGI.

10/17/08 Intro to Servlets 25


Advantage 2: Convenient
 Servlets include built-in functionality for:
 Reading HTML form data
 Handling cookies
 Tracking user sessions
 Setting HTTP headers
 Java is object oriented

10/17/08 Intro to Servlets 26


Advantage 3: Powerful
 Servlets can talk directly to the web
servers.
 Multiple servlets can share data:
 Particularly important for maintaining
database connections.
 Includes powerful techniques for
tracking user sessions.

10/17/08 Intro to Servlets 27


Advantage 4: Portable
 One of the advantages of Java is its
portability across different operating
systems.
 Servlets have the same advantages.
 You can therefore write your servlets on
Windows, then deploy them on UNIX.
 You can also run any of your servlets
on any Java-enabled web server, with
no code changes.
10/17/08 Intro to Servlets 28
Advantage 5: Secure
 Traditional CGI programs have a number
of known security vulnerabilities.
 Hence, you usually need to include a
separate Perl/CGI module to supply the
necessary security protection.
 Java has a number of built-in security
layers.
 Hence, servlets are considered more
secure than traditional CGI programs.

10/17/08 Intro to Servlets 29


Advantage 6: Inexpensive
 You can download free servlet kits for
development use.
 You can therefore get started for free!
 Nonetheless, production strength
servlet web servers can get quite
expensive.

10/17/08 Intro to Servlets 30


Java Server Pages

10/17/08 Intro to Servlets 31


Java Server Pages
 Related to Java Servlets
 Can be used alone or in conjunction
with servlets
 Represent (yet) another method for
creating server side applications

10/17/08 Intro to Servlets 32


Servlets v. JSP
 Servlets
 code looks like a regular Java program.
 JSP
 embed Java commands directly within
HTML
 Let’s examine a Servlet program next to
a JSP program…
 Each of these prints, “Hello, World!”

10/17/08 Intro to Servlets 33


port java.io.*; A Java Servlet :
port javax.servlet.*; Looks like a regular
port javax.servlet.http.*; Java program

blic class HelloWorld extends HttpServlet {


ublic void doGet(HttpServletRequest req, HttpServletRespon
hrows ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>
out.println("<BODY>");
out.println("<BIG>Hello World</BIG>");
out.println("</BODY></HTML>");

10/17/08 Intro to Servlets 34


A JSP Page :
Looks like a regular
<html> HTML page.
<head>
<title>Hello, World JSP Example</title>
</head>
<body>
<h2> Hello, World!
The current time in milliseconds is
<%= System.currentTimeMillis() %>
</h2>
</body>
</html> Embedded Java
command to
print current time.
10/17/08 Intro to Servlets 35
Summary
 Servlet: a java program that runs within
the web server.
 Servlets have lots of advantages over
other server side scripting options.
 Servlets look like regular Java code with
some HTML.
 Java Server Pages look like HTML with
some Java.

10/17/08 Intro to Servlets 36

You might also like