Top Banner
Introduction to Servlets Slide 1 of 35 Objectives In this lesson, you will learn to: Identify the characteristics of a Servlet Create a Servlet Deploy a Servlet
27
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Servlet 01

Introduction to Servlets

Slide 1 of 35

Objectives

In this lesson, you will learn to:

Identify the characteristics of a Servlet

Create a Servlet

Deploy a Servlet

Page 2: Servlet 01

Introduction to Servlets

Slide 2 of 35

Need for Servlets

Servlets are required to:

Reduce the overhead on the server and network

To take care of processing data on the Web server

Page 3: Servlet 01

Introduction to Servlets

Slide 3 of 35

Servlets are:

Java programs that can be deployed on a Java enabled Web server

Used to extend the functionality of a Web server

Used to add dynamic content to Web pages

Page 4: Servlet 01

Introduction to Servlets

Slide 4 of 35

Characteristics of Servlets

Servlets are:

Efficient

Initialization code for a servlet is executed only once

Robust

Provide all powerful features of Java

Page 5: Servlet 01

Introduction to Servlets

Slide 5 of 35

Portable across Web servers

Persistent

Increase the performance of a system by preventing frequent disk access

Page 6: Servlet 01

Introduction to Servlets

Slide 6 of 35

Comparison between Servlets and Applets

Applets:

Are embedded in Web pages

Require the browser to be Java-enabled

Take a lot of time to be downloaded

Page 7: Servlet 01

Introduction to Servlets

Slide 7 of 35

Servlets:

Execute on the Web server, thus help overcome problems with download time

Do not require the browser to be Java-enabled

Page 8: Servlet 01

Introduction to Servlets

Slide 8 of 35

Comparison between Servlets and other Server-Side Scripting Technologies

Common Gateway Interface (CGI) scripts, JSP, and ASP are alternatives to servlets

CGI Scripts:

Are programs written in C, C++, or Perl

Get executed in a server

Run in separate processes for each client access

Require the interpreter to be loaded on the server

A JSP file is automatically converted to a servlet before it is executed

Page 9: Servlet 01

Introduction to Servlets

Slide 9 of 35

Comparison between Servlets and other Server- Side Scriptings

Active Server Pages (ASP):

Is a server-side scripting language developed by Microsoft

Enables a developer to combine HTML and a scripting language in the same Web page

Are not compatible with all Web servers

Page 10: Servlet 01

Introduction to Servlets

Slide 10 of 35

Working of Servlets

Client browser passes requests to a servlet using the following methods:

GET

1.Uses a query string to send additional information to the server

2.Query string is displayed in the client browser

Page 11: Servlet 01

Introduction to Servlets

Slide 11 of 35

POST

1.Sends the data as packets to the server through a separate socket connection

2.Complete transaction is invisible to the client

3.Slower compared to the GET method

Page 12: Servlet 01

Introduction to Servlets

Slide 12 of 35

The javax.servlet Package

Hierarchy of classes that are used to create a servlet

Class java.lang.Object

Class javax.servlet.GenericServlet

Class javax.servlet.HttpServlet

Interface javax.servlet.Servlet

Interface javax.servlet.ServletConfig

Interfacejavax.io.Serializable

Page 13: Servlet 01

Introduction to Servlets

Slide 13 of 35

The javax.servlet Package (Contd.)

A brief description of the classes and interfaces

Class/Interface Description

HTTPServlet class

Provides a HTTP specific implementation of the Servlet interface.

HTTPServletRequest interface

Provides methods to process requests from the clients.

Page 14: Servlet 01

Introduction to Servlets

Slide 14 of 35

HTTPServlet Response interface

Response to the client is sent in the form of a HTML page through an object of the HTTPServletResponse class.

ServletConfig class

Used to store the servlets startup configuration values and the initialization parameters.

Page 15: Servlet 01

Introduction to Servlets

Slide 15 of 35

Life Cycle of a Servlet

Life cycle of a servlet:

Client (Browser)

init()

service()

destroy()

Request

Response

Page 16: Servlet 01

Introduction to Servlets

Slide 16 of 35

Life Cycle of a Servlet (Contd.)

The following table describes few methods that are used in creating a servlet:

Method name Functionality

Servlet.init (ServletConfig config) throws ServletException

Contains all initialization code for the servlet.

Servlet. service()

Receives all requests from clients, identifies the type of the request, and dispatches them to the doGet() or doPost() methods for processing.

Page 17: Servlet 01

Introduction to Servlets

Slide 17 of 35

Life Cycle of a Servlet (Contd.)

Method name Functionality

Servlet. destroy()

Executes only once when the servlet is removed from server.

HTTPServlet Response. getWriter()

Returns a reference to a PrintWriter object.

HTTPServlet Response. setContentType (String type)

Sets the type of content that is to be sent as response to the client browser.

Page 18: Servlet 01

Introduction to Servlets

Slide 18 of 35

Deploying a Servlet

A servlet can be deployed in:

Java Web Server (JWS)

JRun

Apache

Java 2 Enterprise Edition (J2EE) server

Page 19: Servlet 01

Introduction to Servlets

Slide 19 of 35

Identify the mechanism to record thenumber of hits on a Web site

Client browsers that are used to access the Web site run on different machines

Hence, the client (browser) cannot keep track of the hit count data

This data has to be captured on the server’s side

Thus, a technique that can be used to write a server-side program is necessary to solve this problem

Page 20: Servlet 01

Introduction to Servlets

Slide 20 of 35

Identify the classes to be used

A class that extends the HttpServlet class must be coded to solve the problem

Page 21: Servlet 01

Introduction to Servlets

Slide 21 of 35

Identify the methods to be used

The init() method needs to be coded in the hitcountServlet class to initialize the hitcounter to zero

The doGet() method needs to be coded to increment the hit counter whenever a client browser makes a request for the site home page

Page 22: Servlet 01

Introduction to Servlets

Slide 22 of 35

Write and compile the servlet

Set the values of PATH and CLASSPATH variables

Code the servlet program

Compile the program by using the command javac hitcountServlet.java

Page 23: Servlet 01

Introduction to Servlets

Slide 23 of 35

Execute the servlet

Type http://127.0.0.1:8000/hitcountwebcontext/hitcounter in the address bar of the browser to execute the servlet

Page 24: Servlet 01

Introduction to Servlets

Slide 24 of 35

Problem for you….

In the URL that is provided, identify the Web context name and the alias name of the servlet

http://127.0.0.1:8000/name/execute

Page 25: Servlet 01

Introduction to Servlets

Slide 25 of 35

Summary

In this lesson, you learned that:

Servlets are server-side Java programs that can be deployed on a Web Server

The Servlet interface provides the basic framework for coding servlets

Page 26: Servlet 01

Introduction to Servlets

Slide 26 of 35

Servlets are :

Portable

Extensible

Persistent

Robust

Page 27: Servlet 01

Introduction to Servlets

Slide 27 of 35

Summary (Contd.)

The life cycle of a servlet is composed of the init(), service(), and the destroy() methods

Servlets can be deployed in the J2EE server and JWS

The deploytool command is used to deploy a servlet on a J2EE server