Professional Documents
Culture Documents
: DAW
HTTP
Reference
HTTP
1
HTTP overview HTTP message
HTTP message: Header section HTTP Request message: 1st line, headers, body
• Principal HTTP Methods for HTTP request
• HTTP uses same format for requests and – GET
responses. – POST
• One line for each header.
– Header-Name: value • HTTP request message:
– ASCII (human-readable format)
request line
l
• No case-sensitive (GET, POST,
HEAD commands)
GET /somedir/page.html HTTP/1.1
– Header1: value 1, value 2 Host: www.someschool.edu
User-agent: Mozilla/4.0
– HEADER1: value 1, value 2 header Connection: close
lines
Accept-language:fr
Carriage return,
line feed (extra carriage return, line feed)
indicates end
of message
Prof. Ing. Carmen Vaca Prof. Ing. Carmen Vaca
ESPOL - FIEC ESPOL - FIEC
2
Parameters for HTTP Request HTTP and HTML Forms
Request Message Headers HTTP Response message: 1st line, headers, body
3
HTTP response status codes (common) Common HTTP 1.1 Response Headers
• 200 OK
– Everything worked, here’s the data
• Cache-Control (1.1) and Pragma (1.0)
• 301 Moved Permanently
– URI was moved, but here’s the new address for your records – A no-cache value prevents browsers from caching page.
• 302 Moved temporarily • Content-Disposition
– URL temporarily out of service, keep the old one but use this one
for now – Lets you request that the browser ask the user to save the
response to disk in a file of the given name
Content-Disposition: attachment; filename=file-
• 400 Bad Request name
– There is a syntax error in your request
• 403 Forbidden
• Content-Encoding
– You can’t do this, and we won’t tell you why – The way document is encoded. See earlier compression
• 404 Not Found example
– No such document • Content-Length
• 408 Request Time-out, 504 Gateway Time-out
– Request took too long to fulfill for some reason
– The number of bytes in the response.
• Content-Type
– The MIME type of the document being returned.
– Use setContentType to set this header.
• Expires
– The time at which document should be considered out-of-
date and thus should no longer be cached.
– Use setDateHeader to set this header.
• Last-Modified
– The time document was last changed.
4
A Servlet That Redirects Users
HTTP Request/Response (together) to Browser-Specific Pages
• Another one
public class WrongDestination extends HttpServlet {
public void doGet(HttpServletRequest request,
GET /index.html HTTP/1.0 HTTP/1.0 200 OK HttpServletResponse response)
Host: www.anyhost.com Last-Modified: Mon, 20 Dec 1999 … throws ServletException, IOException {
Date: Tue
Tue, 11 Jan 2002 … String
g userAgent
g = request.getHeader("User-Agent");
q g ( g );
User-Agent : Mozilla/4.5 [en] (WinNT; I)
if ((userAgent != null) &&
Status: 200
Accept : image/gif, image/jpeg, */* (userAgent.indexOf("MSIE") != -1)) {
Content-Type: text/html
Accept-language : en response.sendRedirect("http://www.sun.com");
Servlet-Engine: Tomcat Web Server } else {
Accept-charset : iso-8859-1, *, utf-8 Content-Length: 59 response.sendRedirect("http://www.microsoft.com");
}
<html> }
… }
</html>
Prof. Ing. Carmen Vaca Prof. Ing. Carmen Vaca
ESPOL - FIEC ESPOL - FIEC
Problems Cookies
• State information
Compras: item seleccionados
• Alternatives – Example?
– Cookies Preferencias del usuario
– HTTP-Authentication
– Hidden fields • It can be send with headers HTTP, so that the
client can store it.
5
Cookies (cont.) Cookies (cont.)
• Any future requests should transmit the current • Sintaxis del header HTTP Set-Cookie
state. Set-Cookie: NAME=VALUE; expires=DATE;
path=PATH; domain=DOMAIN_NAME; secure
• Set-Cookie header
• response.setStatus(int statusCode)
– Use a constant for the code, not an explicit int.
– Constants are in HttpServletResponse
– Names derived from standard message.
Desarrollo de aplicaciones web E.g., SC_OK, SC_NOT_FOUND, etc.
6
Setting Response Headers Common MIME Types
Type Meaning
• response.setHeader(String headerName, application/msword Microsoft Word document
String headerValue) application/octet-stream Unrecognized or binary data
application/pdf Acrobat (.pdf) file
– Sets an arbitrary header application/postscript PostScript file
application/vnd.ms-excel Excel spreadsheet
application/vnd.ms-powerpoint Powerpoint presentation
application/x-gzip Gzip archive
application/x-java-archive JAR file
application/x-java-vm Java bytecode (.class) file
application/zip Zip archive
audio/basic Sound file in .au or .snd format
audio/x-aiff
di / iff AIFF sound d file
fil
audio/x-wav Microsoft Windows sound file
audio/midi MIDI sound file
text/css HTML cascading style sheet
text/html HTML document
text/plain Plain text
text/xml XML document
image/gif GIF image
image/jpeg JPEG image
image/png PNG image
image/tiff TIFF image
video/mpeg MPEG video clip
video/quicktime QuickTime video clip
7
Same Origin policy Cache control
• R
Referencia
f i
• http://www.javaworld.com/javaworld/javatips/jw-
javatip94.html
• Pueden usar ASP.Net
– Article ASP.Net 2.0: Export GridView to Excel
By Dipal Choksi November 06, 2006