introduction to web applications with java technology 2- the http

30
Introduction to Web applications with Java Technology 2- The HTTP Protocol Juan M. Gimeno, Josep M. Rib´ o January, 2008

Upload: others

Post on 03-Feb-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to Web applications withJava Technology

2- The HTTP Protocol

Juan M. Gimeno, Josep M. Ribo

January, 2008

Page 2: Introduction to Web applications with Java Technology 2- The HTTP

INDEX

Contents

Introduction to web applications with Java technology

1. Introduction.

2. HTTP protocol

3. Servlets

4. Servlet container: Tomcat

5. Web application deployment

1

Page 3: Introduction to Web applications with Java Technology 2- The HTTP

INDEX

2- The HTTP protocol. Contents

• The HTTP request/response protocol

• Request syntax

– Request line– Request header– Request methods and parameter codification

• Example 1: Complaint form

• Example 2: File upload

2

Page 4: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications with Java technology. 2. HTTP protocol INDEX

The HTTP protocol

HTTP: HyperText Transfer Protocol

• It is an application-level protocol for distributed hyperme-dia information systems

• It is being used masively by internet clients to access certainweb resources (specially, web pages and web applications)

• Its development has been supported by the W3C and theIETF

• Its current stable specification is HTTP/1.1

Spec document: IETF RFC 2616

http://www.w3.org/Protocols/rfc2616/rfc2616.html

A revision of this specification is currently ongoing withthe aim of solving some problems detected in the protocol

3

Page 5: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications with Java technology. 2. HTTP protocol INDEX

2-HTTP request/response protocol

http/1.1 200Content−type=text/html

<html>...</html>

response

GET /document.html http/1.0request

(e.g., web browser)

Client Server

4

Page 6: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications with Java technology. 2. HTTP protocol INDEX

HTTP request/response protocol(2)

• Request

It is composed of:

– A resource identifier (URI)

Examples:

∗ an html file (static page)

∗ A program that generates a web page dynamically (e.g., a

servlet/jsp page)

– Parameters (e.g., customer name, e-mail and phone in an html

form)

– Additional information about the client

• Response

The most important part of the Response is a stream of data which

is interpreted by the client (the browser)

This stream of data may be:

– the contents of the html file verbatim– An image (.gif, .jpeg...)

– A string of html code which has been generated dynamically by an

application running in the server (servlet).

– A xml file (also a dynamically generated xml file)

– ...

5

Page 7: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications with Java technology. 2. HTTP protocol INDEX

Properties of the HTTP protocol

1. Stateless protocol

2. Server feedback no so dynamic and immediate

(since it must travel through the net)

3. No information on how the request has been made

Problem 1 has been solved by introducing scopes, sessions,cookies...

Problems 2 and 3 by introducing some client-side code (ja-vascript, ajax...) which improves interaction.

6

Page 8: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications with Java technology. 2. HTTP protocol. Request syntax INDEX

Request syntax

GET /josepma/index.html HTTP/1.1Host:griho.udl.esUser-Agent : Mozilla/4.5 [en]Accept : image/gif, image/jpeg, text/htmlAccept language : enAccept-Charset : iso-8859-1

1. Request line

It contains the resource which is requested

GET /josepma/index.html HTTP/1.1

2. Request header

It contains further information about the client. Thisinformation may help the server to provide a response tothe request

Host:griho.udl.esUser-Agent : Mozilla/4.5 [en]Accept : image/gif, image/jpeg, text/html...

3. Request body

It is only included in some types of requests (e.g., POSTrequests)

7

Page 9: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Request line INDEX

Request syntax: 1. Request line

Request-method<SP>Resource-identif<SP>protocol-version

Example:

GET /josepma/index.html HTTP/1.1

Components:

1. Request method

An indication of the action to be performed in the reques-ted resource

2. Resource identifier

The request resource

3. Protocol version

HTTP version used by the client

8

Page 10: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Request line INDEX

1. Request method:

• GET: Used to retrieve a resource/information from theserver. The state of the server should not changeGET parameters are sent to the server as a part ofthe request line

• POST: Used to send some information to the server.This information will be associated to the request re-source and will change the server state.Examples: A message is passed to a forum stored in theserver, a file is uploaded in the server, a server databaseis modified.POST parameters are sent to the server as a partof the request body

• Other request methods have been defined by the HTTPspecification: HEAD, CONNECT, PUT, DELETE,OPTIONS, TRACEThese methods are not covered by this tutorial since theyare not that useful in the context of web applications

9

Page 11: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Request line INDEX

2. Resource identifier:

The identifier of the requested resourceIt is expressed by means of a URI (Uniform ResourceIdentifier)

A URI may be composed of the following fields:

• Scheme: (e.g., http:)The uppermost element of the URI stringIt gives information on what kind of resource is to beaccessed (e.g., “file” −→ local file) or on with whichprotocol that resource will be accessed (e.g., http)Some schemes may be the following: http, ftp, mailto,https, file...

• Host (e.g., www.ingrid.udl.cat)The host that contains the resource that is to be acces-sed

• Port number (e.g., :8080)Port numbers are used to know which service (applica-tion) running in the server must manage that specificrequestA default port number is used if no one is given

10

Page 12: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Request line INDEX

• Path (e.g., TravelWebApp/web/index.html)The specific resource within the host

• Request parameters (e.g., ?dest=Barcelona)Parameters associated to the request and that must bemanaged by the server application

• Fragment (e.g., #welcome)A specific location within a resource

In addition, a URI may contain other fields (e.g., authority)which are not much used with the HTTP protocol

11

Page 13: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Request line INDEX

Examples of URIs (absolute URIs)

http://ingrid.udl.cat:8080/TravelApp/......web/index.html#welcome

http://ingrid.udl.cat:8080/TravelApp/......web/findTrains.jsp?dest=Barcelona

Usually, the resource identifier contains a URI relative tothe host (i.e., the host does not appear in the URI):

Example of URI (relative URIs or URI references)

images/image1.png

In these cases the host can be found at the Host elementin the request header (see below)

12

Page 14: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Request line INDEX

3. Protocol version:

The version of the HTTP protocol used by the client

The server should use the same version or a previous one

13

Page 15: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Request header INDEX

Request syntax: Request header

It contains some information that may be used by the serverto process the request.

This information may be given in terms of some predefinedrequest-header fields

Host : ingrid.udl.esUser-Agent : Mozilla/4.5 [en]Accept : image/gif, image/jpeg, text/htmlAccept language : enAccept-Charset : iso-8859-1

If the resource identifier in the request line is not an absoluteURI, the host to which the request is addressed is found inthe host field fo the request header

14

Page 16: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Request methods and parameter codification

Requests may contain parameters. Examples:

• The identifier of the product that has been selected (in ane-commerce web application)

• The city to where we want to travel (in a web applicationthat shows bus timetables from a given city).

Parameters may be sent through a request codified in twodifferent ways:• As a query string

• As a multipart data

Each request method sends the parameters in a specificplace of the request• Method GET: In the request line

• Moethod POST: In the request body

15

Page 17: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Request parameters codified as a query string

Query string: A string that codifies the request parameters

Example:?username=josep+ribo&email=josepma%40eup.udl.es&age=35

• A list of “name=value” pairs linked by ’&’

• Spaces have been substituted by ’+’

• Special characters have been substituted by ’%xx’ (xxstands for the ASCII code of the special character inhexadecimal)

Query strings may be used both in methods GET and POST.

Query strings are the most used way to codify parameters(the default option)

16

Page 18: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Request parameters codified as a querystring. GET method

A request that uses a GET method, passes its para-meters as a query string which is appended to the endof the URL in the request line

Example: Consider the following HTML form:

<form action="http://www.lleidabus.com/timetable.jsp"method="GET">

Destination: <input name="dest" type="text">Service (normal/fast): <input name="speed" type="text"><p><input type="SUBMIT"></form>

This may generate a request like the following one:

GET timetable.jsp?dest=san+sebastia&speed=fast HTTP/1.1Host: www.lleidabus.comUser-Agent: Mozilla/4.5 [en].....

17

Page 19: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Request parameters codified as a query string. GET method

The same request could have been generated by the following:

<a href="http://www.lleidabus.com/timetable.jsp?dest=san+sebastian&speed=fast">Buses to San Sebastian (fast service)

</a>

That is:

GET timetable.jsp?dest=san+sebastian&speed=fast HTTP/1.1Host: www.lleidabus.comUser-Agent: Mozilla/4.5 [en].....

18

Page 20: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Request parameters codified as a querystring. POST method

A request that uses a POST method, may pass itsparameters as a query string which is included in therequest body

Example: Consider the following HTML form:

<form action="http://www.lleidabus.com/timetable.jsp"method="POST">

Destination: <input name="dest" type="text">Service (normal/fast): <input name="speed" type="text"><p><input type="SUBMIT"></form>

This may generate a request like the following one:

POST timetable.jsp HTTP/1.1Host: www.lleidabus.comUser-Agent: Mozilla/4.5 [en]

dest=san+sebastian&speed=fast

19

Page 21: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Request parameters codified as multipart.POST method

Parameters are sent in the request body. Each parameterin a different block.Blocks are linked by means of a pseudoaleatory stringgenerated by the server

• Multipart codification is used in forms sent with the met-hod POST

• Multipart codification is used to send files to the server(upload files)

Parameters codified as multipart cannot be sent through aget method

20

Page 22: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Example: Consider the following HTML form:

<form action="http://www.lleidabus.com/timetable.jsp"method="POST" enctype="multipart/form-data">

Destination: <input name="dest" type="text">Service (normal/fast): <input name="speed" type="text"><p><input type="SUBMIT"></form>

This may generate a request like the following one:

POST timetable.jsp HTTP/1.1Host: www.lleidabus.comUser-Agent: Mozilla/4.5 [en]

------------0xKhTmLbOuNdArYContent-Disposition: form-data; name="dest"

san sebastian------------0xKhTmLbOuNdArYContent-Disposition: form-data; name="speed"

fast------------0xKhTmLbOuNdArY--

21

Page 23: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Request syntax. Req meth and parm codif INDEX

Parameter codification and request methods.Summary

codification −→ query string multipartmethod

↓GET (pars. in request line) OK KO

POST (pars. in body) OK OK

22

Page 24: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Example 1 INDEX

Example 1: Complaint form

Example location:introapweb/examples/ex2.1

• This example shows a complaint form

• When the complaint form is submitted, a request to exe-cute the servlet reqreader.jsp is sent to the web server

(more precisely, reqreader.jsp is a JSP page)

• The JSP page reqreader.jsp returns to the client allthe information contained in the request (e.g, the formparameters, the request line, the headers....)

• The form is sent with method GET

• The details of how the JSP page should be programmedwill be shown in the module Web applications. JSPpages

23

Page 25: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Example 1 INDEX

<html><h1><center><b>Formulari de reclamacions</b></center></h1><form action="reqreader.jsp" method="get"

enctype="application/x-www-form-urlencoded"><table border="0" cellpadding="3" cellspacing="0"><tr valign="top">

<td> <b>Nom:</b> </td><td><input name="nom" type="text" size=50></td></tr>

<tr valign="top"><td> <b>e-mail:</b> </td><td><input name="email" type="text" size=30></td></tr>

<tr valign="top"><td> <b>Motiu general de la reclamaci&oacute;:</b></td><td>

<select name="motiu" size=1><option value="estat"> Estat de l’habitaci&oacute;<option value="menjars"> Menjars<option value="servei"> Servei<option value="altres"> Altres

</select> </td></tr><tr valign="top">

<td> <b>Detalls de la reclamaci&oacute;:</b> </td><td><textarea name="detalls" rows=7 cols=50></textarea></td></tr>

...(cont)

24

Page 26: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Example 1 INDEX

<tr valign="top"><td> <b>&Eacute;s vost&egrave; client habitual:</b></td><td>

<input name="client" type="radio" value="1"checked> SI

<input name="client" type="radio" value="0"> NO</td>

</tr><tr valign="top">

<td colspan=2><b>Opcions addicionals:</b> <br><input name="addenda" type="checkbox" value="1">

Vol rebre per escrit la resposta a la seva queixa<input name="addenda" type="checkbox" value="2">

Reclama algun tipus d’indemnitzaci&oacute;</td></tr><tr valign="top">

<td> &nbsp; </td><td>

<input type="submit" value="Enviar"></textarea><input type="reset" value="Reiniciar"></textarea>

</td></tr><input type="hidden" name="autor" value="Pepet Cremat"></table></form></html>

25

Page 27: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Example 1 INDEX

Example 1: Complaint form

• introapweb/examples/ex2.1/formulari.html

Method: GET Codification: query string (only chance)

• introapweb/examples/ex2.1/formulari2.html

Method: POST Codification: query string

• introapweb/examples/ex2.1/formulari3.html

Method: POST Codification: multipart

readreq.jsp is an application which shows the contents of anhttp request

26

Page 28: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Example 2 INDEX

Example 2: File upload

Example location:introapweb/examples/ex2.2

Form sent with method POST and multipart codification(only chance to upload a file)

<HTML>

<p>Upload JSP</p>

<FORM ACTION="reqreader.jsp" METHOD=POSTENCTYPE="multipart/form-data">

File Name <INPUT TYPE=TEXT NAME=file_name>File to upload <INPUT TYPE=FILE NAME=file_body> <BR><INPUT TYPE=SUBMIT></FORM>

</HTML>

27

Page 29: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. Activities INDEX

Activity

1. Design a form to obtain the grades of a student in somesubjects. The form should include two text fields (dni andname) and a checkbox for each subject.

2. Use the methods GET and POST and the codificationsquery string and multipart. Find out the differences inthe http request generated by the form when using thedifferent alternatives for methods and codifications.

3. Design a form to obtain all the students that have beenenrolled in a subject. Give the name of the subject.

28

Page 30: Introduction to Web applications with Java Technology 2- The HTTP

Introduction to web applications. 2. HTTP protocol. References INDEX

References

• Specification of the http protocol:

Spec document: IETF RFC 2616

http://www.w3.org/Protocols/rfc2616/rfc2616.html

• html tutorial

http://www.2kweb.net/html-tutorial/

29