Java Minute Web 1: Servlets

Recently I entered Java web development world, which is level up in skill after ColdFusion development and a dig-down inside Java technology stack. Being lucky not touch Java EE internals for a decade, thanks to ColdFusion and Railo, I found Java web development to be unnesessary complex but very robust and honed technology (especially if compared to boiling goo of server side Javascript). 

For reference I picked 2 books: Java EE7 Essentials by Arun Gupta and Professional Java for Web Applications by Nicholas S. Williams

Basics of web-development are starting with Servlets, and here are some of my notes. I'm using IDEA Studio 13 and Tomcat 8 installed on Windows machine. Current J2SE JDK is 1.8 update 25.

First step is a setting up new Java EE project in IDEA:

Adding Tomcat as Run/Debug configuration:

Need to configure IDEA to have Web Facet that picks sources from /src folder and uses web descriptor from /web folder of "web-1-servlet" module. Source Java files will be compiled to Classes.

Standard JDK SE has no javax.servlet package that is required for servlet implementation. As solution we can 'borrow' the package from Tomcat installation by including Tomcat's /lib folder to build path. Compiler then find required packages in Tomcat's JAR files.

Configure Artifacts to be built per Web Facet. WAR file and/or WAR Exploded to be built with files from compiled output folder. This mean that web.xml, compiled classes and JSP files to be picked up and packed into WAR file.

Web descriptor file web.xml dictates to respond to request for /helloworld path by calling HelloWorldServlet.

Calling /helloworld path. It could be relative to your deployment path, for example /web-1-servlet/helloworld.

UPDATE 2018:

Instead of using web.xml descriptor, now it's possible to annotate methods in Java servlet class:

@WebServlet(urlPatterns = "/helloworld", name = "HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("Hello, World!");
    }

}

When deploying to Tomcat or Glassfish server, use 'custom context' to have this servlet (artifact) deployed right under root folder ("/"). Then navigate to http://localhost:8080/helloworld to see servlet running.

Please note that Glassfish5 not running under Java 9, it will gives

Exception in thread "main" java.lang.NullPointerException