Skip to content

Another Java Framework!

Spark is a Micro-framework that allows you to spin up a web server and rapidly develop a web application.

Did I say web 'server'?

Yes! A web-application itself can be seen as a client-server application; to present a web-page, the browser sends a request to a web-server to fetch some HTML document that represents the page. The request and response follow the HTTP communication protocol.

Apache Spark!

Spark is unfortunately in a bit of namespace collision in the Java world. The Spark framework is not to be confused with the database called Apache Spark. It's unfortunate that they share the same name and it makes searching around a little confusing at times.

To use Spark, you need to add the following to the dependencies clause of build.gradle:

1
2
3
compile 'com.sparkjava:spark-core:2.8.0'
compile 'org.slf4j:slf4j-api:2.0.0-alpha1'
compile 'org.slf4j:slf4j-simple:2.0.0-alpha1'

Run a web-server locally!

Running Spark is as easy as follows!

1
2
3
4
5
6
7
import static spark.Spark.*;

public class WebServer {
  public static void main(String[] args) {
    get("/", (req, res) -> "Welcome to CourseReVU App!");
  }
}

After running the WebServer, point your browser to http://localhost:4567/ to see the following message!

1
Welcome to CourseReVU App!

Spark vs Javalin

Spark and Javalin can both be used for server-side programming. As you have probably noticed, their syntax is similar too. In fact, Javalin originally started as a fork1 of Spark but then it evolved into a ground-up rewrite. Spark and Javalin each have their strengths. We used Javalin for developing REST API and we will use Spark to build our web server.


  1. In the Open Source community, and especially on GitHub, a fork means a copy of a repository.