Serving HTTP Content
Socket client = serverSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader( client.getInputStream())); // before serving a response, typically you would // read the client input and process the request. PrintWriter out = new PrintWriter(client.getOutputStream()); out.println("HTTP/1.1 200"); out.println("Content-Type: text/html"); String html = "<html><head><title>Test Response" + "</title></head><body>Just a test</body></html>"; out.println("Content-length: " + html.length()); out.println(html); out.flush(); out.close(); |
In this phrase, we show an example of serving a very simple piece of HTML content via HTTP. We accept a connection with a client, create a BufferedReader to read the client’s request, ...
Get Java™ Phrasebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.