You are on page 1of 41

1

SERVLETS
2

Contents
What are servlets ? Features of Servlet Difference between Servlet and Applet Servlet life cycle Servlet Container Applications of Servlets
3

Competing server-side technologies


CGI Fast CGI Mod Perl ASP Server-side JavaScript PHP
4

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.

What are servlets?


Servlets are first technology of Sun Micro Systems towards server-side application development. Servlets are used to extend the functionality of a servers.
9

Request Servlet program Response

10

A Common Req-Res Model


Request Servlet program Response

11

What is servlet?
A Servlet is a server side software component, written in java, that dynamically extends the functionality of a server

12

What can Servlets do?


Process user input passed by an HTML form and return an appropriate response Provide user authentication Interact with server resources such as database, other applications Forward request from one server to another for load balancing purposes

13

Client and Servlet Interaction


Web Server Servlet Container

Request Client Response

Request

Servlet Request Servlet Servlet Class

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

What are Servlet engines?


It is a software module in servers that handles requests for servlets and JSP files. It creates Servlet instances, loads and unloads servlets, creates and manages request and response objects, and performs other tasks for managing servlets effectively.
17

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

Generic and HTTP Servlets


GenericServlet request response Server

Client

service ( )

Browser request response

HTTPServlet HTTP Server doGet ( ) service ( ) doPost( )

19

GenericServlet usage scenario


server GenericServlet Subclass

Request

Service()

Respons e

20

HttpServlet usage scenario


request response POS T Service() doPost() GE T HttpServlet subclass doGet()

request response

21

Working with Tomcat 5.


1. Create a new directory in <tomcat home dir>/webapps 2. Create a directory named WEB-INF in the directory created in step 1. 3. Create a directory named classes in the directory created in step 2. 4. Store the .class files of servlet programs in the directory created in step 3. 5. Store web.xml (deployment descriptor) in the directory created in step 2. 6. In the browser enter the following URL: http://host:8080/<app dir>/<alias>/<servlet name>

22

The web.xml configuration file


<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Steve's attempt at servlet loading</display-name> <description> This is going to work</description> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> this name is arbitrary, but <servlet-mapping> these have to match <servlet-name>FirstServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>

name of your servlet class

URL used to invoke this class

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

Working with Weblogic


1. Create a new domain 2. Store .class files of servlet prgs in classes directory 1. Store the deployment descriptor (web.xml) in WEB-INF 3. Start the server 4. Enter the following URL in the browser http://host:7001/<url pattern>/<servlet class name>
26

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

Static Web Content

30

Dynamic Web Content

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

Write Once, Run Anywhere

34

Servlet Life Cycle


Constructor Init()

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

The service() Method


Each time the server recieves a request for a servlet , the server spawns a new thread and calls service(). The service method checks the HTTP request type(get,post) and calls doGet,doPost as appropriate.

39

Difference between doGet & doPost


doGet() : This method is limited with 2k of data to be sent, doPost() method doesnt have this limitation. A request string for doGet() looks like the following: http://www.allapplabs.com/svt1?p1=v1&p 2=v2&...&pN=vN , where as doPost() method call doesn't need a long text tail after a servlet name in a request .
40

Summary

41

You might also like