JavaXT

HTTP Server

The javaxt-server library is used to create high performance web applications without all the weight and complexity of a traditional J2EE app server. This library has been used in production for several years to host websites, develop lightweight desktop applications, implement distributed processing architectures, run web map servers, and much, much more.

Current Version: 2.0.11
Release Date: 9/25/2016
File Size: 147 KB
File Format: Zip
Includes: Jar File, Source Code, and Documentation

Key Features

Basic Usage

The javaxt-server library is used to build custom web servers. To implement a server, you need to create a class that extends the javaxt.http.servlet.HttpServlet class. The HttpServlet is then passed to the Server constructor, along with an address/port to bind to, and the total number of threads used to process client requests.

Here's an example of a simple command line application. The main() method is the command line interface to the application. It is used to instantiate both the http server and the servlet classes.

package com.example;
import javaxt.http.servlet.*;

public class Main {

  //Entry point for the application
    public static void main(String[] args) {

      //Start the server
        try {
            int port = 9080;
            int numThreads = 50;
            javaxt.http.Server server = new javaxt.http.Server(port, numThreads, new TestServlet());
            server.start();
        }
        catch (Exception e) {
            System.out.println("Server could not start because of an " + e.getClass());
            System.exit(1);
        }
    }

  //Custom Servlet
    private static class TestServlet extends HttpServlet {

        public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

            response.write("Hello, the time is now " + new java.util.Date());
        }
    }
}

Here's a slightly more sophisticated implementation of an HttpServlet. In this example, the server is used to serve up both static and dynamic content. Static content in this example are simply files on disk. The dynamic content in this example is simply the requested URL.

package com.example;
import javaxt.http.servlet.*;

public class TestServlet extends HttpServlet {

  //Constructor
    public TestServlet() {
    }

  //Request Processor
    public void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {

      //Get requested path
        String path = request.getURL().getPath();
        if (path.length()>1 && path.startsWith("/")) path = path.substring(1);

      //Peel off the first part of the path. We'll use this to determine whether to serve
      //static or dynamic content.
        String service = path.toLowerCase();
        if (service.contains("/")) service = service.substring(0, service.indexOf("/"));


      //If the path start with "WebServices", return some dynamic content
        if (service.equalsIgnoreCase("WebServices")){

            response.setContentType("text/plain");
            response.setStatus(200);
            response.write(request.getURL().toString());

        }
        else{ //Let's serve some static content!

          //Construct a physical file path using the url
            java.io.File file = new java.io.File("/temp/webroot/" + path);


          //If the file doesn't exist, return an error
            if (!file.exists() || file.isDirectory()){
                response.setStatus(404);
                return;
            }
            else{ //Dump the file content to the servlet output stream
                response.write(file, getContentType(file), true);
            }
        }
    }



    public String getContentType(java.io.File file){

        String fileName = file.getName();
        String fileExtension = "";
        if (fileName.contains(".")){
            fileExtension = fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
        }

        if (fileExtension.equals("css")) return "text/css";
        if (fileExtension.equals("htm")) return "text/html";
        if (fileExtension.equals("js"))  return "text/javascript";
        if (fileExtension.equals("txt")) return "text/plain";

        return "application/octet-stream";
    }
}

More Examples