Start learning free

LESSON 149 OF 173 · BACKEND: SHIPPING IT

A Real Web Server: Tomcat in Java

Practice

So far the course ran the shop for you. On a server, you start it yourself, with a command line. There is no separate web server to install: spring-boot-starter-web puts Tomcat, a web server made for real traffic, inside the shop. When the shop starts, Spring Boot starts Tomcat, and for each request Tomcat calls the route that matches it.

mvn package builds the whole shop into one file, a jar, in the target folder: with the pom's finalName shop, that's target/shop.jar. The command starts java, then names the jar after -jar. Settings for Spring Boot come after the jar, written --name=value. For example, a blog whose pom's finalName is blog, printing only warnings in its log, could start with:

java -jar target/blog.jar --logging.level.root=WARN

Two settings matter for the shop. --server.address sets which network interfaces Tomcat listens on: 0.0.0.0 means every one, so other machines can connect (Spring Boot's default, written out), while 127.0.0.1 lets only programs on the server itself connect. --server.port sets the port: without it, Spring Boot uses 8080. There are no workers to set: Tomcat answers many requests at the same time, each on one of its threads. With the full command, the check prints:

$ java -jar target/shop.jar --server.address=0.0.0.0 --server.port=8000
java: starts Java, which runs the shop; Spring Boot starts Tomcat inside it, a web server made for real traffic
-jar target/shop.jar: runs the shop that mvn package built; the words after the jar go to Spring Boot as settings
--server.address=0.0.0.0: listens on every network interface, so other machines can connect
--server.port=8000: listens on port 8000 (Spring Boot's default is 8080)
Tomcat answers many requests at the same time, each on one of its threads (up to 200): no workers to set
For each request, Tomcat calls the route that matches it:
GET / -> 200 OK: The shop is open

Your task: the shop's ShopController is already written. Fill the array command, one string per piece, each followed by a comma: "java", then "-jar target/shop.jar", then "--server.address=0.0.0.0", and "--server.port=8000" last. The check joins the pieces with spaces.


Assignment
Follow the Coding Tutorial to write the command line that runs the shop on the server.


Hint
The program to run comes first, then the jar after -jar. Spring Boot's settings go after the jar: before it, java reads them as its own options and stops.


STEP 1 OF 4
    Ctrl + Enter to check

    Ship Wsgi in Java

    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free