Introduction to Servlets
Lets
see some basic terms that are used in Web Architecture and their
meaning/functionality :
- Resource: Any file(word document, pdf, sound file, movie) or an HTML page or any element that is hosted or available on server
- Client: It can be a program or browser that starts with the request process to get a resource from server/web as response and show the result. E.g. Mozilla Browser
- Server: Its takes the request from the client, finds the resource and send back the response to client. Server can be a hardware component like machine or a software component like web server
How Client
& Server know/talk to each other —>HTTP and HTML
HTML:
HTML tells browser how to display content to the user. HTML stands for Hyper
Text Markup Language. So each browser should know HTML.
HTTP:
Is a protocol that clients and servers use to communicate on web, this allows
for simple request and response conversations. So its HTTP Request and HTTP
Response.
GET
/select/page.jsp?color=white&taste=salty HTTP/1.1
Host:
www.server.com
User-Agent:
Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4) Gecko/ 20030624
Netscape/7.1
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/
plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1
Accept-Language:
en-us,en;q=0.5
Accept-Encoding:
gzip,deflate
Acceot-Charset:
IS0-8859-1.utf-8:0=0.7.*:a=0.7
Keep-Alive:
300
Connection:
keep-alive
Note: The server uses HTTP to send HTML
to client.
<html>
<head>
<title>A Login
Page</title>
</head>
<body>
----------
</body>
</html> - These days online shopping is on full swing. Such sites maintain a database this would include items for sale, prices, availability, orders and so forth. It wishes to make this information accessible to customers via web pages. The contents of those web pages must be dynamically generated to reflect the latest information in the database.
- Lets see the way how above scenario is achieved before servlets are introduced: In order to achieve above scenario, web server takes helps of CGI (Common Gateway Interface). This CGI creates a separate process that prepares the dynamic pages by reading data from database. Now the fetched data is passed to client. Thus CGI helps in reading data from HTTP Request and write data to HTTP response.
- This CGI programs are written in C, C++, Perl languages. CGI suffered serious problem of
- performance in terms of processors and memory resources to create a separate process for each client process. It was also expensive to open and close database connections for each client request. One more major issue is CGI is platform dependent.
- The techniques that overcomes all these drawbacks is S e r v I e t s.