But we also can embed Tomcat inside our java application and do all stuffs through our own program. This is just a simple example showing how to embed the tomcat, start the server and put a servlet inside that.
I'm using Intellij Idea IDE. But you can use any IDE that you like. First create a new maven project and give an artifact id, group id and version that you like.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<groupId>org.wso2.training.tomcat.embed</groupId> | |
<artifactId>tomcat-embeded-sample</artifactId> | |
<version>1.0-SNAPSHOT</version> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-core</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-logging-juli</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-jasper</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jasper</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jasper-el</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jsp-api</artifactId> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.wso2.training.tomcat.embed</groupId> | |
<artifactId>tomcat-embeded-sample</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<tomcat.version>7.0.34</tomcat.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-core</artifactId> | |
<version>${tomcat.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-logging-juli</artifactId> | |
<version>${tomcat.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-jasper</artifactId> | |
<version>${tomcat.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jasper</artifactId> | |
<version>${tomcat.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jasper-el</artifactId> | |
<version>${tomcat.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jsp-api</artifactId> | |
<version>${tomcat.version}</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.wso2.training.tomcat.embed; | |
import org.apache.catalina.Context; | |
import org.apache.catalina.LifecycleException; | |
import org.apache.catalina.startup.Tomcat; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
public class EmbeddedTomcat { | |
public static void main(String[] args) throws LifecycleException { | |
Tomcat tomcat = new Tomcat(); | |
tomcat.setPort(8888); | |
Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath()); | |
tomcat.addServlet(ctx, "HelloTomcat", new HttpServlet() { | |
protected void service(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | |
PrintWriter responseWriter = resp.getWriter(); | |
responseWriter.println("You are running Tomcat in Embedded mode"); | |
responseWriter.flush(); | |
} | |
}); | |
ctx.addServletMapping("/*", "HelloTomcat"); | |
tomcat.start(); | |
tomcat.getServer().await(); | |
} | |
} |
You will see something like following image on browser.