Download - 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
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
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
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
Introduction to Servlets
Slide 5 of 35
Portable across Web servers
Persistent
Increase the performance of a system by preventing frequent disk access
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
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
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
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
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
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
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
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.
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.
Introduction to Servlets
Slide 15 of 35
Life Cycle of a Servlet
Life cycle of a servlet:
Client (Browser)
init()
service()
destroy()
Request
Response
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.
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.
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
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
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
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
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
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
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
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
Introduction to Servlets
Slide 26 of 35
Servlets are :
Portable
Extensible
Persistent
Robust
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