[Java] How to deploy a simple Java Servlet app to Heroku

2 minute read

Deploy a simple Java app to Heroku

I recently studied Java Servlet development, made some simple applications in a local environment, tried it out, and then thought that I would like to give this to a server. There are many articles about apps that use Gradle, etc., and I didn’t know how to do it when I just wanted to deploy a simple Servlet app, so I tried writing it myself after the memorandum.

1. Export Servlet application in war file format

In the Eclipse environment, export the dynamic project file as a War file by selecting Export from the right-click menu.

![Screenshot 2020-07-18 12.29.00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/485150/82044372-9aa8-beb0-b842-(891b2cccba9e.png)

Select the export destination as the destination and export to any directory.

2. Login to Heroku with CLI

Heroku CLI is supposed to be installed in advance. For installation, click here (https://devcenter.heroku.com/articles/getting-started-with-java#set-up)

$ heroku login

3.Create an app on Heroku

This can be either CLI or from Heroku web application.

![Screenshot 2020-07-18 12.37.15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/485150/45368e84-3740-7a9b-b0ba-(3b54796e1b9a.png)

When creating from the Web, you can create an App from the New button on the upper right of the Dashboard.

4. Install Heroku Java Plugin

If you want to deploy Servlet application with War file, download this CLI.

$ heroku plugins:install java

5. Deploy the Servlet application

Type the following command.

$ heroku war:deploy <Direct path to war file> --app <Heroku app name created earlier>

You will get the following results.

8.5.57/webapps/App/ShoppingCart.war --app shoppingcartjava
 ›Warning: heroku update available from 7.35.1 to 7.42.4.
Uploading ShoppingCart.war
- ----> Packaging application...
       -app: shoppingcartjava
       -including: webapp-runner.jar
       -including: ShoppingCart.war
- ----> Creating build...
       -file: slug.tgz
       -size: 21MB
- ----> Uploading build...
       -success
- ----> Deploying...
remote:
remote: -----> heroku-deploy app detected
remote: -----> Installing JDK 1.8... done
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 72.3M
remote: -----> Launching...
remote: Released v3
remote: https://shoppingcartjava.herokuapp.com/ deployed to Heroku
remote:
- ----> Done

https://shoppingcartjava.herokuapp.com/ will be the application deployment destination.

![Screenshot 2020-07-18 12.46.06.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/485150/aabdfe4d-095e-a356-6367-(9edf913d939a.png)

It was displayed!

The reference article is the official Heroku Doc below. https://devcenter.heroku.com/articles/war-deployment

Supplement: The path set when executing from Eclipse cannot be used on a server other than the local server.

By the way, when I tried to log in the application I just uploaded to the server above, I got a 404 error.

Screenshot 2020-07-20 20.46.13.png

I can’t find the Login screen. The code of web.xml and the top screen, index.jsp, is as follows.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http ://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ShoppingCart</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Login</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>servlet.Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>ShopMain</display-name>
    <servlet-name>ShopMain</servlet-name>
    <servlet-class>servlet.ShopMain</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ShopMain</servlet-name>
    <url-pattern>/ShopMain</url-pattern>
  </servlet-mapping>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to the shopping site</h1>
<p>Login menu</p>
<form action="/ShoppingCart/Login" method="post">
<label for="userName">User name: </label> <input type="text" name="userName">
<label for="pass">Password: </label><input type="text" name="pass">
<input type="submit" value="submit">
</form>
</body>
</html>

The cause here is that the path to the servlet class is specified by the path /ShoppingCart/Login in index.jsp (No problem if done from a local server from Eclipse), but it is not local If you want to realize access with the same path in the server of, the path changes, so change the web.xml as follows or change the form action destination path of index.jsp to /Login. need to do it.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http ://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ShoppingCart</display-name>
  <welcome-file-list><welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Login</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>servlet.Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/ShoppingCart/Login</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>ShopMain</display-name>
    <servlet-name>ShopMain</servlet-name>
    <servlet-class>servlet.ShopMain</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ShopMain</servlet-name>
    <url-pattern>/ShoppingCart/ShopMain</url-pattern>
  </servlet-mapping>
</web-app>