1
Browser –
Server Communication
This chapter explains how information is sent from a browser to a server. It begins
with a description of the request from a browser and a response from a server.
Each of these has a format that is determined by the
Hypertext Transfer Protocol
[HTTP].
The chapter continues with the explanation of markup languages, with a
detailed description of the
Hypertext Markup Language
[HTML], which is used to
send formatted content from the server to the browser. One of the most important
features of HTML is its ability to easily request additional information from the
server through the use of hypertext links.
HTML forms are also covered. These are used to send data from the browser back
to the server. Information from the form must be formatted so that it can be sent
over the web. The browser and server handle encoding and decoding the data.
Simple web pages cannot process form data that is sent to them. One way to
process form data is to
use a web application and a
Java Server Page
[JSP]. In a
JSP, the
Expression Language
[EL] simplifi es access to the form data and can be
used to initialise the form elements with the form data that is sent to the page.
JSPs are processed by a program know as a servlet engine. The servlet engine
receives the request and response data from the web server and processes the
request from the browser. The servlet engine translates all JSPs into programs
known as servlets.
Servlets and JSPs must be run from a servlet engine.
Tomcat is a popular servlet
engine. NetBeans is a development environment that is tailored for web develop-
ment. NetBeans is packaged with Tomcat.
1.1 Hypertext
Transfer
Protocol
Whenever someone accesses a web page on the Internet, there is communication
between two computers. On one computer there is a software program know as a
browser, on the other is a software program known as a web server. The browser
sends a request to the server and the server sends a response to the browser. The
request contains the name of the page that is being
requested and information
about the browser that is making the request. The response contains the page that
was requested (if it is available), information about the page and information
about the server sending the page – see Figure 1.1.
1
2
Web Development with Java
Browser
Server
GET /index .html HTTP/1.1
[Request Headers ]
[Blank Line ]
[Optional Content ]
Request
Response
200 OK HTTP /1.1
[Response Headers ]
[Blank Line ]
...
Figure 1.1
The request and response have specifi c formats, as specifi ed by the HTTP protocol.
When the browser makes the request, it mentions the protocol that it is using:
HTTP/1.1. When the server sends the response, it also identifi es the protocol it is
using: HTTP/1.1.
A protocol is not a language; it is a set of rules that must be fol-
lowed. For instance, one rule in HTTP is that the fi rst line of a request will contain
the type of request, the address of the page on the server and the version of the
protocol that the browser is using. Another rule is that the fi rst line of the response
will contain a numeric code indicating
the success of the request, a sentence
describing the code and the version of the protocol that the server is using.
Protocols are used in many places, not just with computers. When the leaders
of two countries meet, they must decide on a common protocol in order to com-
municate. Do they bow or shake hands when they meet? Do they eat with chop-
sticks or silverware? It is the
same situation for computers, in order for the browser
and server to communicate, they must decide on a common protocol.
1.1.1 Request
Format
The request from the browser has the following format in HTTP:
1. The fi rst line contains the type of request, the name of the requested page and
the protocol that is being used.
2. Subsequent lines contain information about the browser and the request.
3. A blank line indicates the end of the request headers.
4. In a POST request, there can be additional information
sent after the blank
line.
1.1.2 Response
Format
The response from the server has the following format in HTTP:
1. The fi rst line contains the status code, a brief description of the status code and
the protocol being used.
2. Subsequent lines contain information about the server and the response.
Browser – Server Communication
3
3. A blank line indicates the end of the response headers.
4. In a successful response, the content of the page will be sent after the blank
line.
1.1.3
Content
Type
The server must also identify the type of information that is being sent. This is
known as the
Do'stlaringiz bilan baham: