Professional Documents
Culture Documents
SERVLETS
2
Contents
What are servlets ? Features of Servlet Difference between Servlet and Applet Servlet life cycle Servlet Container Applications of Servlets
3
CGI
One of the earliest, technique for generating dynamic web content. Unfortunately, traditional CGI programs suffer from performance problems.
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 Program.
Web Browser Web Server Create New process
CGI
6
CGI Architecture
For each browser request, server must spawn a new operating system process.
CGI Browser 1 Browser 2 Browser N CGI
7
Web Server
CGI
ASP
Runs on Microsofts Web Server: Internet Information Server (IIS) Programmers add ASP code directly into their HTML pages. Faster than traditional CGI, but only works on Microsoft IIS.
10
11
What is servlet?
A Servlet is a server side software component, written in java, that dynamically extends the functionality of a server
12
13
Request
Response
Response
14
15
Servlet Container?
Servlet Container or Servlet Engine is a software that runs on a web server and translates requests from clients to objects that the Servlets understand and gets back a response from it and sends it back to the client. The container also manages the lifecycle of a Servlet.
16
It is a Java module, running a JVM, while the server may be written in other languages. It is initiated when server starts up, and continues to run until server is shut down. For servers written in Java, the container is a part of the same process.
18
Client
service ( )
19
Request
Service()
Respons e
20
request response
21
22
23
<web-app> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
24
Applications of Servlets
Search Engines E-Commerce Applications Shopping Carts Product Catalogs Intranet Applications Groupware Applications: bulletin boards, file sharing, etc.
25
27
javax.servlet
Interfaces
Servlet ServletConfig ServletContext ServletRequest ServletResponse SingleThreadModel
Classes
GenericServlet ServletInputStream ServletOutputStream
28
javax.servlet.http
Interfaces
HttpServletRequest HttpServletResponse HttpSession HttpSessionBindingListener HttpSessionContext
Classes
Cookie HttpServlet HttpSessionBindingEvent HttpUtils
29
30
31
Applet Vs Servlets
Applet is a dynamic and interactive program that runs inside the web page displayed by the java capable browser. A Servlet is a server side software component, written in java, that dynamically extends the functionality of a server. Applets get executed on client machines where as Servlets gets executed on a web server.
32
Differences
Servlets are to servers what applets are to browsers: an external program invoked at runtime. Unlike applets, however, servlets have no graphical user interface. Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server's environment or protocol. Servlets are portable.
33
34
Service()
Destroy()
Finalize()
35
Lifecycle of a Servlet
There are three important methods in the Servlet interface which actually controls the lifecycle init() service() and destroy() The Servlet Container is responsible for calling the methods and thus controlling the lifecycle.
36
public class Skeleton extends HttpServlet { public void init ( ) { //initialization code goes here } public void service ( ) { //meaningful work happens here } public void destroy ( ) { //free resources here } }
37
Lifecycle of a Servlet
Basically the life cycle is as follows
Container creates an instance of servlet. Container calls the init() method of the Servlet If there is a request then the container calls the service() method on the instance. Before destroying the instance the container calls the destroy() method.
38
39
Summary
41