Design of an Improved HTTP Server
A Simple Network Application in Java
Prof. David Bernstein
James Madison University
Computer Science Department
bernstdh@jmu.edu
Comments on our Current Design
Handling GET Requests:
We can do very little in response to a GET request
Adding functionality will complicate the code
Handling Other Requests:
Adding support for POST requests will further complicate the code
Improving the Current Design
What's Needed?
A way to "instruct" the connection handler how to handle requests.
A Solution:
The Strategy Pattern
The Servlet Interface
javaexamples/http/v3/HttpServlet.java
An Improved HTTP Connection Handler
javaexamples/http/v3/HttpConnectionHandler.java
An Abstract Servlet
javaexamples/http/v3/AbstractHttpServlet.java
Invoking Servlets
A General Approach:
Map servlet to URLs
Different Kinds of Mappings
Map one servlet to one URL
Map one servlet to one directory
Map one servlet to files with a given extension
A Servlet Factory
javaexamples/http/v3/HttpServletFactory.java
A Default Servlet
javaexamples/http/v3/DefaultHttpServlet.java
An Example
The Motivation:
Many WWW sites use a single "template" for all of their pages
The easiest way to do this is to include all of the template information (e.g., headers, menu bars, footers) in each page
The problem with this approach is that you have the same information in multiple files which makes it difficult to change, etc...
Using a Servlet:
Have a servlet that writes the common information into each response
An Example (cont.)
javaexamples/http/v3/TemplatedHttpServlet.java
There's Always More to Learn