17.8. Example: A Simple HTTP Server

In Listing 17.19 we adapt the NetworkServer class to act as an HTTP server. Rather than returning files, however, we have the server simply echo back the received input by storing all of the input lines, then transmit back an HTML file that shows the sent line. Although writing programs that output HTML seems odd, in Chapter 19 (Server-Side Java: Servlets) and Chapter 24 (JavaScript: Adding Dynamic Content to Web Pages) you'll see that this is actually common practice. Furthermore, having a program that can act as an HTTP server but returns a Web page showing the received input is a useful debugging tool when you are working with HTTP clients and servlet or JSP programming. You'll see this class used many times ...

Get Core Web Programming, Second Edition 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.