HTTP/2 in the Java Platform -- Java Champions call February 2016
HTTP and Java
-
Upload
emma-harvey -
Category
Documents
-
view
23 -
download
0
description
Transcript of HTTP and Java
![Page 1: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/1.jpg)
HTTP and Java
![Page 2: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/2.jpg)
Background
• TCP/IP– Break message into packets– Give each packet a number and total number of packets in
message– Destination IP address and port numbers– Source IP address and port
• IP Address (v4 and v6)• Port Number (0 to 216-1)– Well Know (0-1023)– Registered (1024-49151)– Dynamic, private, ephemeral (all the rest)
![Page 3: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/3.jpg)
Sockets
• Internet Sockets– Listener or Server Socket– Client Sockets– Two way connection provides two way communication
• Operating specific means of– Opening– Closing– Transmitting to and from a “socket”
• Often uses a protocol like TCP/IP• In Java it is viewed as a stream
![Page 4: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/4.jpg)
Hypertext Transfer Protocol
• Application level– Provides communication between applications
• Usually a client and server (though it may be peer-to-peer)
– Underlying transport layer is TCP/IP• Request-Response Protocol• Used for sending and receiving various types of
information– Used in web-applications especially in HTML-based
applications
![Page 5: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/5.jpg)
High-level View
• An HTTP Session– Client establishes a TCP connection to a particular
port (e.g. port 80)– Server waits for message from Client– Upon receipt the server sends back a • Status (e.g. ok)• Response
– Requested Information– Error Message– Or it could be empty
![Page 6: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/6.jpg)
Message Types:HTTP Request
• Request Line– GET or POST with pathToResource
• Header Lines– Header Name – Value Pairs• Example Names: From:, User-Agent:
• Blank Line• Optional Message Body– Gets don’t have a Message Body– For example: XML Encoded information in a POST
![Page 7: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/7.jpg)
Message TypesHTTP Response
• Status Line– For example:
• HTTP/1.0 200 OK• HTTP/1.0 404 Not Found
• Header Lines– Header Name – Value Pairs– Example Names: Server:, Last-Modified:
• Blank Line• Optional Message Body
– Examples• HTML• XML Encoded Information is a response expected• Could be blank for a pure “POST”
![Page 8: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/8.jpg)
Request Methods
• GET– Should be used to retrieve information, not modify
information on the server• Not enforced and some gets don’t conform to this
suggestion, can cause problems
– Encodes parameters in URL sent to server• Appears in the request line (the first line)• Server may have URL length limits (e.g. 2083 characters)
![Page 9: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/9.jpg)
Request Methods
• POST– Should be used to change information– Information sent in message body• Can be any format (including binary)• We will use XML• Is of arbitrary size
![Page 10: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/10.jpg)
HTTP And Java
• Several “HTTP” servers written in Java– Jetty– Apache– Oracle HTTPServer• Built into Oracle’s Java 1.6 and 1.7• Import com.sun.net.httpserver.*;• We will use this one
![Page 11: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/11.jpg)
HttpServer Code• HttpServer server;• HTTPServer.create(
new InetSocketAddress(portNumber), maxNumberOfConnections);• server.setExecutor(null);
– Default thread pool manager• server.createContext(URLSuffix, handlerMethodObject)
– Handler for various suffixes in the URL– Handler “method” is a Command object that extends HttpHandler
• HttpHandler has one method: void handle(HttpExchange exchange)
• Often the handler is declared as an instance of an anonymous inner type that overrides the “void handle(HttpExchange exchange)” method
• server.start()
![Page 12: HTTP and Java](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812a9f550346895d8e5d2b/html5/thumbnails/12.jpg)
HttpExchange Code• An HttpExchange encapsulates an HTTP request received and a response to be generated in one
exchange.• exchange.sendResponseHeaders(int rCode, responseLength)
– rCode is often a constant found in HttpURLConnection• Examples
– HttpURLConnection.HTTP_INTERNAL_ERROR– HttpURLConnection.HTTP_OK,
– responseLength• 0 means an arbitrary size• -1 means no response body
• exchange.getRequestBody();– Gets the body of the request as an input stream– Used as a parameter to xmlStream.fromXML(exchange.getRequestBody());
• exchange.getResponseBody();– Gets the body of the request as an input stream– Used as a parameter to xmlStream.toXML(object, exchange.getResponseBody());