0% found this document useful (0 votes)
8K views36 pages

Servlets: Introduction To Servlets

This document provides an introduction to servlets, including an overview of servlet architecture, the life cycle of a servlet, common uses of servlets, and advantages over other server-side technologies like CGI and PHP. It also introduces Java Server Pages (JSP) as a related technology that allows embedding Java code within HTML pages.

Uploaded by

Dharmendra Gupta
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 (0 votes)
8K views36 pages

Servlets: Introduction To Servlets

This document provides an introduction to servlets, including an overview of servlet architecture, the life cycle of a servlet, common uses of servlets, and advantages over other server-side technologies like CGI and PHP. It also introduces Java Server Pages (JSP) as a related technology that allows embedding Java code within HTML pages.

Uploaded by

Dharmendra Gupta
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

4/23/2013

Intro to Servlets

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

4/23/2013

Intro to Servlets

Architectural Overview

4/23/2013

Intro to Servlets

What is a Servlet?

Javas 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.

4/23/2013

Intro to Servlets

Life of a Servlet
1)

Regardless of the application, servlets usually carry out the following routine: Read any data sent by the user
Capture data submitted by an HTML form.

2)

Look up any HTTP information


Determine the browser version, host name of client, cookies, etc.

3)

Generate the Results


Connect to databases, connect to legacy applications, etc.

4/23/2013

Intro to Servlets

Life of a Servlet (cont.)


4) Format the Results
Generate HTML on the fly Tell the browser the type of document being returned or set any cookies.

5) Set the Appropriate HTTP headers

6) Send the document back to the client

4/23/2013

Intro to Servlets

Life of a Servlet
Database 3 1,2 Web Browser 6 Web Server Java Servlet

4,5

4/23/2013

Intro to Servlets

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.

4/23/2013

Intro to Servlets

Servlets in Context

4/23/2013

Intro to Servlets

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.

4/23/2013

Intro to Servlets

10

Server Side Options


Common Gateway Interface (CGI) Fast CGI Mod Perl ASP PHP Cold Fusion

4/23/2013

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
4/23/2013 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?

4/23/2013

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. Lets examine these two problems

4/23/2013

Intro to Servlets

14

CGI Architecture
1)

2)
3)

Browser initiates request Web server receives the request. For each request, web server spawns a new operating system process to execute the CGI/Perl Program. Create New process

Web Browser

Web Server

Perl/CGI

4/23/2013

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
4/23/2013 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.

4/23/2013

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
Intro to Servlets 18

4/23/2013

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

4/23/2013

Intro to Servlets

19

Option 4: ASP

Active Server Pages Runs on Microsofts 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.
Intro to Servlets 20

4/23/2013

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

4/23/2013

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

4/23/2013

Intro to Servlets

22

Advantages of Servlets

4/23/2013

Intro to Servlets

23

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

4/23/2013

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.

4/23/2013

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

4/23/2013

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.

4/23/2013

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.
Intro to Servlets 28

4/23/2013

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.
4/23/2013 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.

4/23/2013

Intro to Servlets

30

Java Server Pages

4/23/2013

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

4/23/2013

Intro to Servlets

32

Servlets v. JSP
Servlets
code looks like a regular Java program.

JSP
embed Java commands directly within HTML

Lets examine a Servlet program next to a JSP program Each of these prints, Hello, World!

4/23/2013

Intro to Servlets

33

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

A Java Servlet : Looks like a regular Java program

public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws 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>"); } }
4/23/2013 Intro to Servlets 34

<html> <head> <title>Hello, World JSP Example</title> </head> <body> <h2> Hello, World! The current time in milliseconds is <%= System.currentTimeMillis() %> </h2> </body> Embedded Java </html>
command to print current time.
4/23/2013 Intro to Servlets 35

A JSP Page : Looks like a regular HTML page.

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.

4/23/2013

Intro to Servlets

36

You might also like