ServletContainerInitializer example!

A standalone jar, a runnable war!

I have a hiddenservlet application, everything related to it is as follows..

Directory Layout

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>hiddenservlet</artifactId>
    <packaging>jar</packaging>
 
    <version>1.0-SNAPSHOT</version>
    <name>hiddenServlet Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>hiddenservlet</finalName>
    </build>
</project>

javax.servlet.ServletContainerInitializer
biz.tugay.hiddenservlet.HiddenServletContainerInitializer

HiddenServlet.java
package biz.tugay.hiddenservlet;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 11/6/2016
 * Time: 11:28 AM
 */
 
@WebServlet(urlPatterns = "/hidden")
public class HiddenServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("hiddenMessage", "This is the hidden message!");
        req.getRequestDispatcher("/hidden.jsp").forward(req, resp);
    }
}

HiddenServletContainerInitializer.java
package biz.tugay.hiddenservlet;
 
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Set;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 11/6/2016
 * Time: 11:29 AM
 */
public class HiddenServletContainerInitializer implements ServletContainerInitializer {
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
        ctx.addServlet("hiddenServlet", HiddenServlet.class);
    }
}

All I do is to clean install this project, so it ends up in my local Maven repository.

Now, the mysteryapp..

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>mysteryapp</artifactId>
 
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <name>mysteryapp Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>biz.tugay</groupId>
            <artifactId>hiddenservlet</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>mysteryapp</finalName>
    </build>
</project>

web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
               http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

hidden.jsp
<html>
<body>
    <h2>Hello World! Hello from Hidden Servlet!</h2>
    <div>
        Here is your hidden message: ${hiddenMessage}
    </div>
</body>
</html>

In Action


Here are some useful links: 1, 2, 3!