HTML Tutorials |
|
XML Tutorials |
|
Browser Scripting |
|
Server Scripting |
|
.NET (dotnet) |
|
Multimedia |
|
Web Building |
|
Java Tutorials |
|
Programming Langauges |
|
Soft Skills |
|
Database Tutorials |
|
Operating System |
|
Software Testing |
|
SAP Module |
|
Networking Programming |
|
Microsoft Office |
|
Accounting |
|
|
Produire de la réponse de serveur : En-têtes de réponse de HTTP |
Une réponse du web server se compose normalement d'une ligne d'état, d'un ou plusieurs en-têtes de réponse, d'un interligne, et du document. Le réglage des en-têtes de réponse de HTTP souvent est de pair assorti à l'arrangement les codes statut dans la ligne d'état. Par exemple, plusieurs des codes statut déplacés « par document » un en-tête de accompagnement ont endroit, et les 401 que le code (non autorisé) doit inclure un accompagnement WWW-Authentifient l'en-tête.
|
Vue d'ensemble
|
La manière la plus générale d'indiquer des en-têtes est par méthode de setHeader de HttpServletResponse, qui prend deux cordes : le nom d'en-tête et la valeur d'en-tête. Comme placer les codes statut, ceci doit être fait avant que n'importe quel contenu de document soit envoyé.
Il y a également deux méthodes spécialisées pour placer les en-têtes qui contiennent des dates (setDateHeader) et des nombres entiers (setIntHeader). Le premier vous sauve ennui de traduire une date de Java en millisecondes depuis l'époque (comme retourné par System.currentTimeMillis ou la méthode de getTime appliquée à un objet de date) dans une corde de temps de GMT. La seconde vous épargne le dérangement mineur de convertir un interne en corde.
Plutôt que de placer l'en-tête pure, vous pouvez ajouter un nouvel en-tête, au cas où un en-tête avec ce nom existerait déjà. Employer l'addHeader, l'addDateHeader, et l'addIntHeader pour ceci. S'il importe vraiment à toi si un en-tête spécifique ait été déjà placé, employer le containsHeader pour vérifier.
En conclusion, HttpServletResponse fournit également un certain nombre de méthodes de convenance pour les en-têtes communs de indication.
La méthode de setContentType place le Contenu-Type en-tête, et est employée à la majorité de servlets.
La méthode de setContentLength place l'en-tête de Contenu-Longueur, utile si le navigateur soutient les raccordements (garder-vivants) persistants de HTTP.
La méthode d'addCookie place le biscuit (il n'y a aucun setCookie correspondant, puisqu'il est normal d'avoir les lignes multiples de Placer-Biscuit).
Et, comme discuté dans la section précédente, la méthode de sendRedirect place l'en-tête d'endroit aussi bien que placer le code statut à 302.
|
En-têtes communs de réponse et leur signification
|
Header
|
Interpretation/Purpose
|
Allow
|
What request methods (GET, POST, etc.)
does the server support?
|
Content-Encoding
|
What method was used to encode the document? You need to decode it
to get the type specified by the Content-Type header. Using
gzip to compress the document can dramatically reduce download times for
HTML files, but it is only supported by Netscape on Unix and IE 4 and 5
on Windows. On the other hand, gzipping HTML files can dramatically
reduce download times, and Java's GZIPOutputStream makes it
easy. So you should explicitly check if the browser supports this by
looking at the Accept-Encoding header (i.e. via
request.getHeader("Accept-Encoding")). That way, you can return
gzipped pages to browser that know how to unzip them, but still return
regular pages to other browsers.
|
Content-Length
|
How many bytes are being sent? This information is only needed if
the browser is using a persistent (keep-alive) HTTP connection. If you
want your servlet to take advantage of this when the browser supports
it, your servlet should write the document into a
ByteArrayOutputStream, look up its size when done, put that into
the Content-Length field, then send the content via
byteArrayStream.writeTo(response.getOutputStream()).
|
Content-Type
|
What is the MIME type of the following document? Default for
servlets is text/plain, but they usually explicitly specify
text/html. Setting this header is so common that there is a
special method in HttpServletResponse for it:
setContentType
|
Date
|
What is current time (in GMT)? Use the setDateHeader
method to specify this header. That saves you the trouble of formatting
the date string properly.
|
Expires
|
At what time should content be considered out of date and thus no
longer cached?
|
Last-Modified
|
When was document last changed? Client can supply a date via an
If-Modified-Since request header. This is treated as a
conditional GET, with document only being returned if the
Last-Modified date is later than the specified date.
Otherwise a 304 (Not Modified) status line is returned. Again, use the
setDateHeader method to specify this header.
|
Location
|
Where should client go to get document? This is usually set
indirectly, along with a 302 status code, via the sendRedirect
method of HttpServletResponse.
|
Refresh
|
How soon should browser ask for an updated page (in seconds)?
Instead of just reloading current page, you can specify a specific page
to load via setHeader("Refresh", "5; URL=http://host/path").
Note that this is commonly set via <META HTTP-EQUIV="Refresh"
CONTENT="5; URL=http://host/path"> in the HEAD
section of the HTML page, rather than as an explicit header from the
server. This is because automatic reloading or forwarding is something
often desired by HTML authors who do not have CGI or servlet access. But
for servlets, setting the header directly is easier and clearer. Note
that this header means "reload this page or go to the specified URL in
N seconds." It does not mean "reload this page or go to
the specified URL every N seconds." So you have to send a
Refresh header each time, and sending a 204 (No Content)
status code stops the browser from reloading further, regardless of
whether you explicitly send the Refresh header or use
<META HTTP-EQUIV="Refresh" ...>. Note that this header is not
officially part of HTTP 1.1, but is an extension supported by both
Netscape and Internet Explorer.
|
Server
|
What server am I? Servlets don't usually set this; the Web server
itself does.
|
Set-Cookie
|
Specifies cookie associated with page. Servlets should not use
response.setHeader("Set-Cookie", ...), but instead use the
special-purpose addCookie method of
HttpServletResponse. See separate section on handling cookies.
|
WWW-Authenticate
|
What authorization type and realm should client supply in their
Authorization header? This header is required in responses
that have a 401 (Unauthorized) status line. E.g.
response.setHeader("WWW-Authenticate", "BASIC realm=\"executives\"").
Note that servlets do not usually handle this themselves, but instead
let password-protected Web pages be handled by the Web server's
specialized mechanisms (e.g. .htaccess). |
|
|
|
Keywords:
Handling the Client Request:Form Data,asp net client,asp net data,vb net data,control data,data net,java client,data repeater
|
|
HTML Quizes |
|
XML Quizes |
|
Browser Scripting Quizes |
|
Server Scripting Quizes |
|
.NET (dotnet) Quizes |
|
Multimedia Quizes |
|
Web Building Quizes |
|
Java Quizes |
|
Programming Langauges Quizes |
|
Soft Skills Quizes |
|
Database Quizes |
|
Operating System Quizes |
|
Software Testing Quizes |
|
SAP Module Quizes |
|
Networking Programming Quizes |
|
Microsoft Office Quizes |
|
Accounting Quizes |
|
|