Servlet and its importance, Lecture notes of Web Design and Development

function of servlet in a website.

Typology: Lecture notes

2016/2017

Uploaded on 06/20/2017

fayyaz-ali
fayyaz-ali 🇵🇰

5

(1)

1 document

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Servlet and Sessions
What is a Servlet?
Servlet can be described in many ways, depending on the context.
Servlet is a technology i.e. used to create web application.
Servlet is an API that provides many interfaces and classes including documentations.
Servlet is an interface that must be implemented for creating any servlet.
Servlet is a class that extend the capabilities of the servers and respond to the
incoming request. It can respond to any type of requests.
Servlet is a web component that is deployed on the server to create dynamic web
page.
As Servlet Technology uses Java, web applications made using Servlet are Secured, Scalable and
Robust.
What is web application?
A web application is an application accessible from the web. A web application is composed
of web components like Servlet, JSP, Filter etc. and other components such as HTML. The
web components typically execute in Web Server and respond to HTTP request.
CGI(Commmon Gateway Interface)
CGI technology enables the web server to call an external program and pass HTTP request
information to the external program to process the request. For each request, it starts a new
process.
Web Engineering 7th semester Software eng. dpt.
Sir Syed University of Engineering and
Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Servlet and its importance and more Lecture notes Web Design and Development in PDF only on Docsity!

Servlet and Sessions

What is a Servlet?

Servlet can be described in many ways, depending on the context.

  • Servlet is a technology i.e. used to create web application.
  • Servlet is an API that provides many interfaces and classes including documentations.
  • Servlet is an interface that must be implemented for creating any servlet.
  • Servlet is a class that extend the capabilities of the servers and respond to the incoming request. It can respond to any type of requests.
  • Servlet is a web component that is deployed on the server to create dynamic web page.

As Servlet Technology uses Java, web applications made using Servlet are Secured , Scalable and Robust.

What is web application?

A web application is an application accessible from the web. A web application is composed of web components like Servlet, JSP, Filter etc. and other components such as HTML. The web components typically execute in Web Server and respond to HTTP request.

CGI(Commmon Gateway Interface)

CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.

Sir Syed University of Engineering and

Before Servlets, CGI(Common Gateway Interface) programming was used to create web applications. Here's how a CGI program works :

  • User clicks a link that has URL to a dynamic page instead of a static page.
  • (^) The URL decides which CGI program to execute.
  • Web Servers run the CGI program in seperate OS shell. The shell includes OS enviroment and the process to execute code of the CGI program.
  • The CGI response is sent back to the Web Server, which wraps the response in an HTTP response and send it back to the web browser.

Sir Syed University of Engineering and

Web Terminology

Servlet

Terminology

Description

Website: static vs dynamic

It is a collection of related web pages that may contain text, images, audio and video. HTTP It is the data communication protocol used to establish communication between client and server. HTTP Requests It is the request send by the computer to a web server that contains all sorts of potentially interesting information. Get vs Post It gives the difference between GET and POST request. Container It is used in java for dynamically generate the web pages on the server side. Server: Web vs Application

It is used to manage the network resources and for running the program or software that provides services. Content Type It is HTTP header that provides the description about what are you sending to the browser.

Static website

Static website is the basic type of website that is easy to create. You don't need web programming and database design to create a static website. Its web pages are coded in HTML.

The codes are fixed for each page so the information contained in the page does not change and it looks like a printed page.

Sir Syed University of Engineering and

Dynamic website

Dynamic website is a collection of dynamic web pages whose content changes dynamically. It accesses content from a database or Content Management System (CMS). Therefore, when you alter or update the content of the database, the content of the website is also altered or updated.

Dynamic website uses client-side scripting or server-side scripting, or both to generate dynamic content.

Client side scripting generates content at the client computer on the basis of user input. The web browser downloads the web page from the server and processes the code within the page to render information to the user.

In server side scripting, the software runs on the server and processing is completed in the server then plain pages are sent to the user.

Sir Syed University of Engineering and

  • It is a request response protocol.
  • It uses the reliable TCP connections by default on TCP port 80.
  • It is stateless means each request is considered as the new request. In other words, server doesn't recognize the user by default.

The Basic Features of HTTP (Hyper Text Transfer Protocol):

There are three fundamental features that make the HTTP a simple and powerful protocol used for communication:

  • HTTP is media independent: It refers to any type of media content can be sent by HTTP as long as both the server and the client can handle the data content.
  • HTTP is connectionless: It is a connectionless approach in which HTTP client i.e., a browser initiates the HTTP request and after the request is sends the client disconnects from server and waits for the response.
  • HTTP is stateless: The client and server are aware of each other during a current request only. Afterwards, both of them forget each other. Due to the stateless nature of protocol, neither the client nor the server can retain the information about different request across the web pages.

The Basic Architecture of HTTP (Hyper Text Transfer Protocol):

The below diagram represents the basic architecture of web application and depicts where HTTP stands:

Sir Syed University of Engineering and

HTTP is request/response protocol which is based on client/server based architecture. In this web browser, search engines, etc behaves as a HTTP clients, and the Web server like Servlet behaves as a server.

HTTP Requests

The request sends by the computer to a web server that contains all sorts of potentially interesting information is known as HTTP requests.

Sir Syed University of Engineering and

Get request can be bookmarked and is more efficient.

Post request cannot be bookmarked.

Session Tracking in Servlets

1. Session Tracking

2. Session Tracking Techniques

Session simply means a particular interval of time.

Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.

Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.

HTTP is stateless that means each request is considered as the new request. It is shown in the figure given below:

Why use Session Tracking?

To recognize the user It is used to recognize the particular user.

Session Tracking Techniques

There are four techniques used in Session tracking:

  1. Cookies
  2. Hidden Form Field
  3. URL Rewriting
  4. (^) HttpSession 1. Cookies in Servlet

Sir Syed University of Engineering and

A cookie is a small piece of information that is persisted between the multiple client requests.

A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

How Cookie works

By default, each request is considered as a new request. In cookies technique, we add cookie with response from the servlet. So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old user.

Types of Cookie

There are 2 types of cookies in servlets.

  1. (^) Non-persistent cookie
  2. Persistent cookie

Non-persistent cookie

It is valid for single session only. It is removed each time when user closes the browser.

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

  1. Simplest technique of maintaining the state.
  2. Cookies are maintained at client side.

Disadvantage of Cookies

  1. It will not work if cookie is disabled from the browser.
  2. Only textual information can be set in Cookie object.

Sir Syed University of Engineering and

no object is bound under the name.

2

public Enumeration getAttributeNames() This method returns an Enumeration of String objects containing the names of all the objects bound to this session.

3

public long getCreationTime() This method returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.

4

public String getId() This method returns a string containing the unique identifier assigned to this session.

5

public long getLastAccessedTime() This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.

6

public int getMaxInactiveInterval() This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.

public void invalidate() This method invalidates this session and unbinds any objects bound to it.

8

public boolean isNew( This method returns true if the client does not yet know about the session or if the client chooses not to join the session.

public void removeAttribute(String name) This method removes the object bound with the specified name from this session.

public void setAttribute(String name, Object value) This method binds an object to this session, using the name specified.

11

public void setMaxInactiveInterval(int interval) This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

Sir Syed University of Engineering and