Create A Java Web Application Using Embedded Tomcat - Heroku Dev Center
Create A Java Web Application Using Embedded Tomcat - Heroku Dev Center
10:28 Create a Java Web Application Using Embedded Tomcat | Heroku Dev Center
G Table of Contents
Prerequisites
Add a Servlet
Add a JSP
Create a Procfile
Deploy to Heroku
This tutorial will show you how to create a simple Java web application using embedded Tomcat.
Follow each step to build an app from scratch, or skip to the end get the source for this article.
Prerequisites
Basic Java knowledge, including an installed version of the JVM and Maven.
https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat 1/6
2024. 03. 10. 10:28 Create a Java Web Application Using Embedded Tomcat | Heroku Dev Center
https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat 2/6
2024. 03. 10. 10:28 Create a Java Web Application Using Embedded Tomcat | Heroku Dev Center
</plugins>
</build>
</project>
This pom.xml defines the dependencies that you’ll need to run Tomcat in an embedded mode.
The last 3 entries are only required for applications that use JSP files. If you use this technique for an application
that doesn’t use JSPs then you can just include the first 3 dependencies.
There is also a single plugin defined. The appassembler plugin generates a launch script that automatically sets
up your classpath and calls your main method (created below) to launch your application.
package launch;
import java.io.File;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
//The port that we should run on can be set into an environment variable
//Look for that variable and default to 8080 if it isn't there.
String webPort = System.getenv("PORT");
if(webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
tomcat.setPort(Integer.valueOf(webPort));
tomcat.start();
tomcat.getServer().await();
}
}
https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat 3/6
2024. 03. 10. 10:28 Create a Java Web Application Using Embedded Tomcat | Heroku Dev Center
This does just what is enough to launch the server. The sample application contains a more complete version of
code that handles temp directories and other things.
Add a Servlet
Create a file called HelloServlet.java in the src/main/java/servlet directory and put the following into it:
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(
name = "MyServlet",
urlPatterns = {"/hello"}
)
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletOutputStream out = resp.getOutputStream();
out.write("hello heroku".getBytes());
out.flush();
out.close();
}
Add a JSP
Create a file called index.jsp in the src/main/webapp directory:
<html>
<body>
<h2>Hello Heroku!</h2>
</body>
</html>
$ mvn package
And then simply run the script. On Mac and Linux, the command is:
$ sh target/bin/webapp
https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat 4/6
2024. 03. 10. 10:28 Create a Java Web Application Using Embedded Tomcat | Heroku Dev Center
C:/> target/bin/webapp.bat
That’s it. Your application should start up on port 8080. You can see the JSP at http://localhost:8080 and the
servlet and http://localhost:8080/hello
Create a Procfile
You declare how you want your application executed in Procfile in the project root. Create this file with a
single line:
web: sh target/bin/webapp
Deploy to Heroku
You can either deploy to Heroku by using the Heroku Maven plugin
(https://devcenter.heroku.com/articles/deploying-java-applications-with-the-heroku-maven-plugin) or you can
deploy using Git. The latter is described in this article.
$ git init
$ git add .
$ git commit -m "Ready to deploy"
$ heroku create
Creating high-lightning-129... done, stack is heroku-18
http://high-lightning-129.herokuapp.com/ | git@heroku.com:high-lightning-129.git
Git remote heroku added
https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat 5/6
2024. 03. 10. 10:28 Create a Java Web Application Using Embedded Tomcat | Heroku Dev Center
Congratulations! Your web app should now be up and running on Heroku. Open it in your browser with:
$ heroku open
This will show your your JSP and then you can navigate to /hello to see your servlet.
https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat 6/6