O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required