Before you jump right into building your URL shortener, you need to learn about two concepts:

  • How to create a perfect Java-Maven project from scratch

  • How to build the simplest static website in Java possible

Let’s have a look at both steps in detail.

Maven Project Setup

As long as you have Maven installed on your machine, any directory (even empty ones!), which contains a pom.xml file, is a valid Maven project. The pom.xml file in turn contains everything that describes your project: From the name of the project, the author (you!) to more advanced stuff like any needed 3rd party libraries.

This means you now have two choices. Either you create a new directory, an empty pom.xml file and a subset of directories that Maven expects by hand. Or, you use a simple Maven command line helper tool, which does these things for you and is less error-prone.

Have a look at the following video, which will use said command line tool and create a new Maven project for you from a so-called Maven archetype. Along the way, you will also learn what to look out for and possibly change, when you are using one of the default Maven archetypes.

Then have a look at the following video, to find out how to properly import a Maven project into IntelliJ Idea and what all the different import settings are about.

Checkpoint: Maven Setup

Make sure that you have a completely clean, new Maven project setup, compiling against Java 12.

When you step into the directory and issue a

mvn clean package

You should get a similar message:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.246 s
[INFO] Finished at: 2019-07-23T12:41:01+01:00
[INFO] ------------------------------------------------------------------------

Then open up the project inside IntelliJ Idea, so you can start working on it.

Building the simplest static web site in Java

What are servlets? And servlet containers?

Web applications are, oversimplified, not much more than HTML being rendered in your browser. In the Java universe, there’s special classes that can output HTML (or text, or Json, or XML): Servlets.

You then deploy these servlets to a servlet container, which is basically a web server. Whenever a user makes a request to https://www.yoursite.com, the servlet container can accept the HTTP requests, route them to one of your servlets and an appropriate HTML (or text, or json, or xml) response will be returned back to the browser. Two of the most popular servlet containers in the Java world are called Tomcat and Jetty.

diag 69b277bab2f99f3ee2aa1a0857dde56a

How do Java web application deployments work?

Historically, after writing Java servlets you had to package them up into a .war (web application resource) file, because that is the file format that servlet containers expect and understand. A .war file is basically a renamed .zip file that contains all your compiled Java classes (servlets), as well as static web pages, images etc.

So you just had to make sure that at the end of your Java build process a .war file got built, which you could then drop into a specific directory that your servlet container was watching, for new deployments. And then after a couple of seconds or minutes, people could access your web pages.

What is an ‘embedded Tomcat’?

You will go another way in this course, though. Over the past couple of years, people realized that instead of having to install a dedicated servlet container like Tomcat, on your server, you can simply embed it into your Java application. What does that mean?

A normal Java application has an entry point, which is a class with a public static void main(String[] args) method.

public class YourApplication {
    public static void main(String[] args) {
        // do something
    }
}

Once your application is bundled into a .jar file, when you try to run said .jar file, in reality all that happens is that the main method gets executed. So, what stops you from running a Tomcat web server inside that main method, effectively bundling Tomcat with your program? Exactly, nothing.

public class YourApplication {
    public static void main(String[] args) {
        Tomcat tomcat = new Tomcat();
        tomcat.start();
                // ...
    }
}
java -jar production.jar   // will run the main method & tomcat

Hence, there are several things you need to learn. First, you need to know what Maven dependencies you need to get started with an embedded Tomcat. For that, watch the following video.

Then, you need to know how to setup and configure an embedded Tomcat inside your .jar file. Here you go:

And finally, you need to be able to write servlets that deliver static HTML to a web browser. Without HTML, no web site and no URL shortening service.

Make sure to watch all three videos before going to the exercise section!

Exercise

It is now time to practice the concepts you have learned in the previous sections. Have a look at this simple wireframe, which should be the new entry page for your URL shortening service - your exercise for today.

wireframe What you need to build

It is a simple HTML page with:

  1. A title

  2. A form, with an input field for links and a shorten button

  3. As well as a tiny text paragraph, displaying the amount of recently shortened links

You now need to do several things:

  • Create a new Maven project from scratch and configure it appropriately

  • Make sure to have the right dependencies for an embedded Tomcat added your project

  • Write a simple servlet, that displays above web page whenever you run your Java application and then open up your browser and point it to http://localhost:9000 (not 8080!)

  • Note: The text paragraph at the bottom can display static values for now, you will change them up tomorrow

Checkpoint: Project Work

  • Setup an embedded Tomcat on port 9000 and boot it up

  • Write a Servlet that displays a small, static HTML webpage that looks like the wireframe above

  • Open up a browser, go to http://localhost:9000 and you should be presented with your webpage

To solve each task, you can simply watch the corresponding videos of today and apply all that learned knowledge to your URL shortener project.

As mentioned before, if you have any questions, do not hesitate to ask, you need to have an up-and-running skeleton application to continue with this course.

So, stop reading and watching.

Time to act.

Next Steps

Once you finished your project, make sure to check-in your progress and push it to your GitHub repository. Then share the link to the respository with your instructor in your private Slack channel for code review. You are done for today.

If you would like to take your project to the next level and turn it into a fully-fleged Spring and Spring Boot version, you should apply to the course.