SparkJava
As noted earlier, we will be using SparkJava to build a web-server.
You need to add the following to the dependencies
clause of build.gradle
:
implementation 'com.sparkjava:spark-core:2.9.3'
implementation 'org.slf4j:slf4j-api:1.7.30'
implementation 'org.slf4j:slf4j-simple:1.7.30'
Then open the Gradle tool window and refresh the Gradle project.
Create a file WebServer.java
with the following content:
import static spark.Spark.*;
public class WebServer {
public static void main(String[] args) {
get("/", (req, res) -> "Welcome to CourseSearch UI!");
}
}
Run the application. Open your browser and visit http://localhost:4567/ to see the welcome message!
Update the code as follows:
import static spark.Spark.*;
public class WebServer {
public static void main(String[] args) {
- get("/", (req, res) -> "Welcome to CourseSearch UI!");
+ get("/", (req, res) -> "<h1>Welcome to CourseSearch UI!</h1>");
}
}
Run the application and visit http://localhost:4567/. Notice how the welcome message is displayed in a stronger tone (bigger fonts).
The <h1></h1>
which we wrapped around the welcome message is an HTML tag. HTML is short for Hyper Text Markup Language. It is essentially a text file organized into sections. Each section is contained within <tagname></tagname>
and is called an HTML element.
When a user visit our website, their browser sends a request to our web-server for content. Our web-server must send back its response in a language that the browser understands, that is, in HTML.
If you're totally new to web development, HTML might be new to you as well. Please refer to the Appendix for a quick overview and resources to learn.