You are on page 1of 26

ADVANC JAVA

UNIT 3: SERVLET API AND OVERVIEW

SEMESTER: 6
PREPARED BY: PROF. NITI KHETRA
APPLICATIONS

DESK TOP APPLICATION WEB APPLICATION MOBILE APPLICATION

J2EE
J2SE J2ME
JAVA2 ENTERPRICE
JAVA2 STANDARE DEDITION JAVA2 MICRO EDEDITION
EDEDITION

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
WEB BASICS
• What is web site?

• Website means Group of web pages

• What is web page

• Collection information

Web pages are a presentation of information (your content) which can be presented in carefully chosen media most appropriate for your content.
Web pages can be static or dynamic, meaning that the content is the same each time someone visits the webpage or is taken from a database which
is updated with new content.
⚫ Examples of media that can be use in your website

⚫ Text, Pictures, Animation, Photos, Video, Sound

• How to work web site?

• Like client , server application through Internet

• What is server?

• Who give a response to the client as per request


• Web is a collection of files that reside on computers, called Web servers, that are located all over the world and are connected to each other through the
Internet.

• What is client ?

• Who send the request to server


• When you use your Internet connection to become part of the Web, your computer becomes a Web client in a worldwide client/server network.

• A Web browser is the software that you run on your computer to make it work as a web client.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
INTERNET TECHNOLOGIES
WWW ARCHITECTURE

PC/Mac/Unix
Client + Browser (IE, FireFox)

Request:
http://www.yahoo.com/default.html

Network HTTP
TCP/IP

Response:
<html>…</html>

Server
Web Server
STRUCTURE OF A UNIFORM RESOURCE LOCATORS

pathname
protocol

http://www.chicagosymphony.org/civicconcerts/index.htm

Domain name filename

http => Hypertext Transfer Protocol


www=> world wide web
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
• HTTP / HTTPS (URL, GET/POST)
• Client-side:
• HTML / XHTML (Extensible HyperText Markup Language)
• JavaScript / VBScript (client-side scripting)
• Applets / ActiveX controls
• Server-side:
• PHP
• Phython
• JSP (Java Server Pages)
• ASP (Active Server Pages)
• ASP.NET (next generation of ASP)

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
HTTP BASICS

• HTTP is a stateless protocol based on a client-server model. An HTTP


client such as Web browser, opens a connection and sends a request
message to an HTTP server asking for a resource. The server further
returns a response message with the requested resource. Once the
resource is delivered. The server closes the connection. Thus no
connection information is stored and hence HTTP is referred to as a
stateless protocol. Connections last for only one transaction. A
transaction consists of several request-response pairs.

• HTTP protocol uses port 80 for sending and receiving messages.


Port is a channel of mechanism used by protocols for sending and
receiving data

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
REQUEST, RESPONSE AND HEADERS

• The request message sent by the client is a stream of text


• Request line(method,resource name,HTTP protocol version)
• Header information
• Response consists
• Status line
• Header information

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
GET AND POST METHODS

• The HTTP request messages use GET or POST methods for transmitting
requests over the web.
• Get method is used when retrieving information such as a document, a
chart, or database query results. The information to the retrieved is passed
as a sequence of characters called query string. Hance. The data being
passed in visible to the client. Appending query string in a Uniform
Resource Locator(URL) enable page to bookmarked or emailed. However, the
length of query string is restricted to 240 to 255 characters depending on
the server.
• POST method is used when sending information such as credit card
numbers or information to be saved in the database data sent using post is
not visible to the client and there is no limit on the amount of data being
sent. Pages requested using POST method cannot be bookmarked or
emailed.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
WEB APPLICATION ARCHITECUTRE

• Presentation Logic consisting of user interface and code to build the


interface
• Application or Business Logic containing code to validate data
accepted in presentation logic and other task specific code.
• Data Access Logic containing code to operate on the database
• The word “logic” used in these components is often replaced with “tier”
or “layer”. So
• Presentation layer
• Application or Business Layer
• Data Layer

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
Data Access Logic
One-tier architecture
In One tier architecture the code related to Business Logic
presentation, business and data access logic are
all clubbed together Presentation Logic

Two-tier architecture Data Access Logic

In Two tier architecture the code related to Data


access logic is separated from the other two Business Logic
components Presentation Logic

Three-tier architecture Data Access Logic


In Three tier architecture all three components is
separated form each other.

Business Logic

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY Presentation Logic
WEB APPLICATION DIRECTORY STRUCTURE
• Static files
• All the HTML pages, JSP pages and images comprise the static file, images can be collectively stored in
an image directory. These files are accessible to the client
• WEB-INF
• The directory exist within the document root.
• Not access by client
• Web-inf directory consists of the following items:
• Classes directory : servlet classes, JavaBean classes and any other classes required by the web
application
• Lib directory : it contains .jar files required for web applications such as database drivers.
• Tags directory : This directory contain tag files
• Tag Library Descriptor files : These files provide information about what are the attributes of a tag
and the class.
• Web.xml : This file contains meta information about the web application.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
GETTING STARTED WITH SERVLETS

• Introduction to Servlets
• A server is device with provides information requested by different devices(clients) on a network. For example, a file
server gives information about different files on the network. Initially, the transfer of data over the web was carried
out by writing Common Gateway Interface(CGI) programs written in a language such as Perl. However for each
request made by the client, a new instance of CGI program had to be created, which would occupy a large amount
of memory. To overcome this servlets were introduced.
• A servlet is program written in java Which runs on a server, processes the requested information and send it to the
client. The clients of the Servlet can be any java application, a browser or any device, which can make a request
and get response for the request.
• This instance of servlet is created only once for all client requests thus saving a hung amount of memory, A servlet
once initialized, stays in the memory, so need not to loaded every time a request made.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
Basis Servlet CGI

It is thread based i.e. for every new request new thread is It is process based i.e. for every new request new
Approach
created. process is created.

The codes are written any programming


Language Used The codes are written in JAVA programming language.
language.

Since codes are written in any language, all the


Since codes are written in Java, it is object oriented and the
Object Oriented languages are not object oriented. So, the user
user will get the benefits of OOPs
will not get the benefits of OOPs

Portability It is portable. It is not portable.

It is removed from the memory after the


Persistence It remains in the memory until it is not explicitly destroyed.
completion of request.

Server Indepent It can use any of the web-server. It can use the web-server that supports it.

Data Sharing Data sharing is possible. Data sharing is not possible.

Link It links directly to the server. It does not links directly to the server.

HTTP server It can read and set HTTP servers. It can neither read nor set HTTP servers.

Construction and destruction of new process is


Cost Construction and destruction of new thread is not costly.
costly.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
BASIC OF HTTP SERVLET

• The httpServlet class provides an abstract class to create an


HTTP Servlet. The HttpServlet class extends GenericServlet
class.The HttpServlet class used to create an HTTP servlet.
Asubclass of HttpServlet class must override at least one of the
following methods: doGet(), doPost(), doPut(), doDelete(),
init(),destroy() and getServeletInfo().

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
LIFE CYCLE OF SERVLET

instantiation
• The Servlet interface defines 3 methods of servlet life cycle
• 1)init()
• 2)service() initialization
• 3)destroy()

Service

Destroy

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK Unavailable


UNIVERSITY
SERVLET HIERARCHY
• A Servlet is an object of Javax.servlet interface.
• Most of the servlets implements interface by extending from
HttpServlet. The Servlet API is contained in two package:
javax.servlet and javax.servlet.http
Javax.servlet

classes

ServletOutputStream ServletInputStream

Interface

ServletConfig ServletResponse

ServletContext ServletRequest
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
INTRODUCTION TO SESSION
TRACKING
SESSION TRACKING

• Session is the period during which the client opens a connection with
the server, makes requests and the server responds to client’s
requests. Session tracking allows the server to keep a track of
successive requests made by the same client. When a client requests
for a page, the server responds with the required page. If the same
client requests for another page, The server treats it as a new client.
• For example
• A customer wants to do shopping online, The customer may select items
form the list and put it in the cart. When the same customer clicks on a new
page, the previously selected items are lost. As a result, it is necessary to
keep a track of successive requests made by the same user. Session
Tracking allows the customer to maintain a connation with the server as
long the customer does not log out from the website.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
COOKIES

• Cookie is a small piece of information send by the web server to


the client that can be later read back by the client. A cookie
contains one or more name-value pairs, Which are exchanged in
request and response headers. The HTTP request header
contains the request made by the client, the method name, and
the version, The HTTP response header contains the data, The
size and type of file that server is sending back to client, When
the server sends a cookie, the client receives the cookie, saves
the cookie and sends it back to the server each time the client
accesses a page on the server.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
There are 2 types of cookies in servlets.
1.Non-persistent cookie
2.Persistent cookie
1.Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the browser.
2.Persistent cookie
It is valid for multiple session . It is not removed each time when user closes the browser. It is removed
only if user logout or signout.
Advantage of Cookies
Simplest technique of maintaining the state.
Cookies are maintained at client side.
Disadvantage of Cookies
It will not work if cookie is disabled from the browser.
Only textual information can be set in Cookie object.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY

You might also like