Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web...

44
Web Applications and Java EE5 Dr. N. A. Joshi

Transcript of Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web...

Page 1: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Web Applicationsand

Java EE5

Dr. N. A. Joshi

Page 2: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Agenda

• Exploring the HTTP Protocol• Introducing Web Applications• Describing Web Containers• Building Web Applications

Page 3: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Exploring the HTTP Protocol

• HTTP is a communication model• Used to transfer information on the internet• Stateless protocol• Application-level protocol• HTTP facilitates

• types of requests that client can send to servers, and • types of responses that server can send to clients

Page 4: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

4

The Hyper Text Transfer Protocol (HTTP)

• HTTP is a connection-oriented, stateless, request-response protocol. • An HTTP server, or web server, runs on TCP port 80 by default. • HTTP clients, colloquially called web browsers, are processes which

implements HTTP to interacts with a web server to retrieve documents phrased in HTML, whose contents are displayed according to the documents’ markups.

Page 5: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

5

The Hyper Text Transfer Protocol (HTTP)

• In HTTP/1.0, each connection allows only one round of request-response.

• A client obtains a connection, issues a request• The server processes the request, issues a response, and closes the

connection thereafter. w eb s er v er w eb b r o w s er

r eq u es t

r es p o n s e

r eq u es t is a m es s ag e in 3 p ar ts : - < c o m m an d > < d o c u m en t ad d d r es s > < HT T P v er s io n > - an o p tio n al h ead er - o p tio n a l d a ta f o r C G I d a ta u s in g p o s t m eth o d

r es p o n s e is a m es s ag e c o n s is t in g o f 3 p ar ts : - a s ta tu s lin e o f th e f o r m at < p r o to c o l> < s ta tu s c o d e> < d es c r ip tio n > - h ead er in f o r m atio n , w h ic h m ay s p an s ev er a l lin es ; - th e d o c u m en t its e lf .

Page 6: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

6

The HyperText Transfer Protocol (HTTP)• HTTP is text-based: the request and responses

are character strings. • Each request and response is composed of

these parts, in order:1) The request/response line2) A header section3) A blank line4) The body

Page 7: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

7

A sample HTTP sessionScript started on Thu J ul 17 08:14:12 2014 9:49pm telnet www.abc.com.edu 80 Trying XX.YY.ZZ.AA... Connected to abc.com. Escape character is '̂ ]'. GET /~joshi/ HTTP/1.0 HTTP Request HTTP/1.1 200 OK HTTP response status line Date: Wed, 11 Oct 2000 04:51:18 GMT HTTP response header Server: Apache/1.3.9 (Unix) ApacheJ Serv/1.0 Last-Modified: Thu J ul 17 08:14:12 2014 GMT ETag: "1dd1e-e27-39e3492a" Accept-Ranges: bytes Content-Length: 3623 Connection: close Content-Type: text/html <HTML> document content <HEAD> <TITLE> ABC's Home Page </TITLE> </HEAD> <BODY bgcolor=#ffffff> …

Page 8: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

8

The HTTP request• A client request is sent to the server after the client has established a connection to the

server.• A request line is of the following form:

<HTTP method><space><Request-URI><space><protocol specification>\r\nwhere

• <HTTP method> is the name of a method defined for the protocol,• <Request-URI> is the URI of a web document, or, more generally, a web object, • <protocol specification> is a specification of the protocol observed by the client,

and • <space> is a space character.

• An example client request is as follows:

GET /index.html HTTP/1.0

 

Page 9: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

9

HTTP Methods in a client request

• The HTTP method in a client request is a reserved word (in uppercase) which specifies an operation of the server that the client desires.

• Some of the key client request methods are listed below:~ GET: for retrieving the contents of web object referenced by the specified URL~ HEAD: for retrieving a header from the server only, not the object itself.~ POST: used to send data to a process on the server host.~ PUT: used to request the server to store the contents enclosed with the request to the

server machine in the file location specified by the URL.~ DELETE: removes the resource identified by the request URL, from the server.~ OPTIONS: returns the HTTP methods that the server supports.~ TRACE: invokes a remote application layer associated with a request message.

Page 10: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The GET Request method

• Simplest and the most frequently used request method• Used to access the static resources, such as HTML documents and

images• It can be used retrieve dynamic information by including query

parameters in the request URL• E.g. http://abc.com?name-Joshi• Here ‘name’ is a parameter having value ‘Joshi’

• GET method contains a request Uniform Resource Identifier (URI),• which is used to retrieve information form the request.• It is (URI) is generally in the form of an entity.

Page 11: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The GET Request method

• If the request-URI refers to the data-producing process,• the produced data, not the source-code of the process is returned as the entity in the

response; unless the text happens to be the output of the process.

• What is ‘Conditional GET method’? (pg. 39)• What is ‘Partial GET method’? (pg. 39)

Page 12: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The HEAD Request method

• Sometimes, a client might only need to view the header of a response (Content-type or Content-Length).

• Then, client can use the HEAD request method to retrieve the header

• Similar to GET method, except that• the server does return a message body in the response of the HEAD method

• It can be used to retrieve the meta information of the entity requested by the user.

• Response of HEAD method is cacheable (pg. 39)

Page 13: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The POST Request method

• Commonly used for accessing dynamic resources, or• when a large amount of complex information is to be sent to the server

• The web server accepts the entity enclosed in the request as a new subordinate of the resource identified by the request-URI.

• Its sub-functionalities:• Providing annotations of existing resources• Posting a message to a bulletin board, newsgroup, mailing list, or a similar

group of articles• Providing a block of data (such as result of submitting a form, to a data-

handling process)• Extending database through an append operation

Page 14: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The POST Request method

• Sometimes, an action performed by the POST method does not result in the retrieval of a resource that can be identified by a URI.

• In such a case, either 200 (OK) or 204(No Content) response status is generated, depending on whether or not the response includes an entity that describes the result.

• The response is 201 (created) when the requested resource has been created on the origin server.

• A POST request allows the encapsulation of multi-part messages into the request body.

• E.g. we can use POST requests to upload text or binary files from the server.• lly, we can use POST requests in applets to send serializable java objects, or even

raw bytes to the web server.• POST requests offer a wider choice than GET requests in terms of the contents of a

request

Page 15: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Difference: GET & POSTParameter GET POST

Transmission of request Parameters The request parameters are transmitted as a query string appended to the request URL

The request parameters are transmitted within the body of the request

URL Size Maximum 256 characters in the URL, minus the number of characters in the actual path

POST method is not limited by the size of the URL for submitting name/value pairs. These pairs are transferred in the header and not in the URL

Purpose Used only for retrieving data Used for retrieving, as well as saving or updating data, ordering a product, or sending e-mail messages

Caching ability Often cacheable A POST request can hardly be cacheable

Type of resource Typically used to access static resources such as HTML documents and images

Used to transmit the information that is request-dependent; or when a large amount of complex information must be sent to the server

Page 16: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The PUT Request method

• The PUT request method stores an entity in the specified Request-URI• The entity is a resource residing on the web server under the

specified Request-URI.

Page 17: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The DELETE Request method

• It requests the web server to delete the resource identified by URI.• 200 (OK) response code specifies that the resource has been deleted

successfully.• 202 (Accepted) response code specifies that the resource has not yet

been deleted.• 204 (No Content) response code specifies that the resource has been

deleted but the response does not include an entity.• This method is not cacheable; this method permanently deletes the

files from the cache as well.

Page 18: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The OPTIONS Request method

Page 19: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

The TRACE Request method

Page 20: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

20

The Request Header

• “The request header fields allow the client to pass additional information about the request, and about the client itself, to the server. These fields act as request modifiers, with semantics equivalent to the parameters on a programming language method (procedure) invocation.”

• A header is composed of one or more lines, each line in the form of <keyword>: <value>\r\n

Page 21: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

21

The Request Header

Some of the keywords and values that may appear in a request header are:• Accept: content types acceptable by the client• User-Agent: specifies the type of browser• Connection: “Keep-Alive” can be specified so that the server does not immediately

close a connection after sending a response.• Host: host name of the server

An example request header is as follows: Accept: */* Connection: Keep-Alive

Host: www.someU.edu

User-Agent: Generic

Page 22: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

22

Request Body

• A request optionally ends with a request body, which contains data that needs to be transferred to the server in association with the request.

• For example, if the POST method is specified in the request line, then the body contains data to be passed to the target process. (This is an important feature and will become clearer when we discuss CGI, servlet, and SOAP.)

Page 23: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

23

Examples of a complete client request

Example1:

GET / HTTP/1.1

<blank line>

 

Example2:

HEAD / HTTP/1.1

Accept: */*

Connection: Keep-Alive

Host: somehost.com

User-Agent: Generic

<blank line>

 

Page 24: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

24

Examples of a complete client request

Example3:POST /servlet/myServer.servlet HTTP/1.0

Accept: */*

Connection: Keep-Alive

Host: somehost.com

User-Agent: Generic

<blank line>

Name=donald&[email protected]

 

 

Page 25: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

25

The HTTP Server Response

• In response to a request received from a client, the HTTP server sends to it a response.

• Like the request, an HTTP response is composed of these parts, in order:

1. The response or status line2. A header section3. A blank line4. The body

Page 26: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

26

The response status line

The status line is in the form of: <protocol><sp><status-code><sp><description>\r\n

The status code designations are as follows:100-199 Informational200-299 Client request successful300-399 Client request redirected400-499 Client request incomplete500-599 Server errors

Example 1:

HTTP/1.0 200 OK

Example 2:

HTTP/1.1 404 NOT FOUND

 

 

Page 27: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

27

HTTP Response Header

• The status line is followed by a response header. A response header is composed of one or more lines, each line in the form of

<keyword>: <value>\r\n• There are two types of response header lines:

1. Response header lines 2. Entity header lines

Page 28: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

28

HTTP Response Header

Response header lines – these header lines return information about the response, the server, and further access to the resource requested, as follows:Age: secondsLocation: URIRetry-After: date|secondsServer: stringWWW-Authenticate: scheme realm

 

Page 29: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

29

HTTP Response Header

Entity header lines – these header lines contain information about the contents of the object requested by the client, as follows:Content-EncodingContent-LengthContent-Type: type/subtype (see MIME)Expires: dateLast-Modified: date

 

Page 30: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

30

HTTP Response Header

An Example response header is as follows:Date: Mon, 30 Oct 2000 18:52:08 GMTServer: Apache/1.3.9 (Unix) ApacheJServ/1.0Last-modified: Mon, 17 June 2001 16:45:13 GMTContent-Length: 1255Connection: closeContent-Type: text/html

• The Content-Type specifies the type of the data, using the contents type designation of the MIME protocol.

• The Content-Encoding specifies the encoding scheme (such as uuencode or base64) of the data, usually for the purpose of data compression.

• The expiration date gives the date/time (specified in a format defined with HTTP)after which the web object should be considered stale

• The Last-Modifed date specifies the date that the object was last modified.

Page 31: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

31

HTTP Response Body

The body of the response follows the header and a blank line, and contains the contents of the web object requested.

HTTP/1.1 200 OK

Date: Sat, 15 Sep 2001 06:55:30 GMT

Server: Apache/1.3.9 (Unix) ApacheJServ/1.0

Last-Modified: Mon, 30 Apr 2001 23:02:36 GMT

ETag: "5b381-ec-3aedef0c"

Accept-Ranges: bytes

Content-Length: 236

Connection: close

Content-Type: text/html

 

<html>

<head>

<title>My web page </title>

</head>

<body>

Hello world!

</BODY></HTML>

Page 32: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Introducing Web Applications

• A web-application • is a server-side application, • that contains

• Web Components (Servlets, JSP pages), • HTML/XML documents, and other resources

• in a directory structure or archived format, • known as a Web Archive (WAR) file.

• It is generally located at a central-server, which provides services to various clients

• In a web server, the web components can be conveniently distributed as Java Archive (JAR) files with the .war extenstion

Page 33: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Introducing Web Applications

• Web-application are of two types:• Presentation-oriented:

• Generates dynamic content and interactive web pages containing various types of markup language (HTML, XML) in response to requests.

• Presentation-oriented applications are often clients of Service-oriented web applications.• Service-oriented:

• Implements the web service, and • is invoked by presentation-oriented web applications.

Page 34: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Introducing Web Applications

Page 35: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Introducing Web Applications

• Server-side application development requires the following:• A programming model & API:

• Specify how to develop applications• Server-side runtime support:

• Includes support for applicable network services, and • a runtime for executing the applications

• Deployment support• Includes support for installing the application on the server.• It also deploys configuring the application components such as

• Specifying initialization parameters, and specifying any database.

Page 36: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Components of a Web Application

• A web application consists of web components (Servlets, JSPs)• Earlier, web-applications used to face maintenance problems,

• which were solved by using the Model/View/Controller (MVC) paradigm for building user interfaces.

• Generally,• JSPs are used for creating a dynamic response or view.• Servlets, containing the logic for managing requests, act as the controller.• Existing business rules are the model.

Page 37: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Components of a Web Application

Page 38: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Servlet vs. CGI

• Servlets perform better than CGI; e.g.-1. Once a servlet is loaded into memory, it can run on a single lightweight

thread; while CGI scripts must be loaded in a different process for every request

2. Servlet can maintain a pool of database connections which save time in processing the requests

3. Servlets eliminate much of the complexity of retrieving parameters from an HTTP request.

4. Servlets provide uniform APIs to maintain the session data throughout web application and interact with user requests.

Page 39: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Java Server Pages

• JSP pages are text based documents that are executed as Servlets,• but allow a more natural approach to creating static content.• they are also designed to provide dynamic content.

• How it was introduced:• Servlet technology was developed as a mechanism

• to accept the requests from browsers,• retrieve the enterprise data from application tier or databases,• perform application logic on the data, and • format that data for presentation in the browser (HTML). • A servlet makes use of print statements to post the HTML data, both hard-coded tags

and dynamic content based on the enterprise data retrieved from the back-end tiers, back to the user’s browser.

Page 40: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Java Server Pages

• How it was introduced:• …• Web designers faced few difficulties using HTML in print statements.

• difficulty in previewing• difficulty in locating the appropriate section of code in a servlet when data or display

format changes (when presentation logic and content are intermixed)• Moreover, change in the servlet would require recompilation and reloading into the web

server• A mechanism to specify the mapping from a JavaBeans component to the HTML

(or XML) presentation format is provided by JSP pages.

• It clearly defines the application logic, helping content developers and web application developers work efficiently.

• Content developers need not be java experts• Web application developers need not be expert web designers

Page 41: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

JSTL & JSF

• JSP Tag Library• is a set or library used by JSP for implementing JSTL tags

• Java Server Faces• It is a framework to build user interfaces for web applications

Page 42: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Types of web components

• Web components are of two types: Presentation & front components• Presentation component:

• They generate HTML/XML responses, which determine the user interface when rendered

• When a JSP page acts as a presentation component, it may contain reusable custom tags or presentation logic.

• Front component:

Page 43: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

Types of web components

Page 44: Web Applications and Java EE5 Dr. N. A. Joshi. Agenda Exploring the HTTP Protocol Introducing Web Applications Describing Web Containers Building Web.

References

• Java Server Programming – Java EE 5 Black Book• http://www.oracle.com/technetwork/java/index.html