Skip to content

Your First JServe Application

Joshua Segal edited this page Mar 3, 2021 · 1 revision

Installing JServe (for release 1.0) is limited to using a single JAR file.

Once that's done, you can import the HTTPServer from com.devsegal.jserve. You'll also have to setup ResponseHeaders for your requests, unless you aren't planning on sending anything back.

import com.devsegal.jserve.HTTPServer;
import com.devsegal.jserve.ResponseHeaders;

HTTPServer server = new HTTPServer(8080); 

server.route("/", "GET", (request, response) -> {

  try {
        response.setResponseHeaders(new ResponseHeaders("text/html", "close"));
      } catch(ResponseStatusNullException e) {
        e.printStackTrace();
      } 
      
  response.insertContent("Hello world!");
  response.send();
});

server.run(); 

This hello world example will listen for requests on http://localhost:8080, and respond to requests that match the the routes setup. Over here, the route is the / path, and so when it receives a GET request on /, it will respond with hello world!.