How to deploy a simple Java Servlet app on Heroku

Deploy a simple Java app on Heroku

After studying Java Servlet development recently, making some simple apps in the local environment and trying them, I wanted to give this to the server, but many blog articles and explanations are Maven and There are many articles about apps using Gradle etc., and I was in trouble because I didn't know how to do it when I just wanted to deploy a simple Servlet app, so I wrote it myself when I had a memorandum.

1. Export the Servlet application in war file format

In the Eclipse environment, right-click the dynamic project file and select Export from the menu to export it as a War file.

スクリーンショット 2020-07-18 12.29.00.png

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

2. Log in to Heroku with the CLI

It is assumed that Heroku CLI has been installed in advance. For installation here

$ heroku login

3. Create an app on Heroku

I think this can be either from the CLI or on the Heroku web application.

スクリーンショット 2020-07-18 12.37.15.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 plugin for Java

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

$ heroku plugins:install java

5. Deploy the Servlet app

Type the following command.

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

The following results will be obtained.

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/ is the deployment destination of the app.

スクリーンショット 2020-07-18 12.46.06.png

It was displayed!

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

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

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

スクリーンショット 2020-07-20 20.46.13.png

I can't seem to find the Login screen. The code of web.xml and ʻindex.jsp` which is the top screen at this time 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 in ʻindex.jsp, the path to the Servlet class was specified by the path / ShoppingCart / Login(no problem when doing it from Eclipse on the local server), but it is not local. If you want to realize access with the same path on the server, change the path as follows, so changeweb.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>

Recommended Posts

How to deploy a simple Java Servlet app on Heroku
Deploy a Java web app on Heroku
How to deploy a kotlin (java) app on AWS fargate
How to deploy on heroku
Deploy Java Servlet app locally on Tomcat
How to deploy an app that references a local jar to heroku
How to deploy to Heroku from a local docker image
[Java] How to execute tasks on a regular basis
How to deploy Laravel on CentOS 7
How to make a Java container
How to deploy Bootstrap on Rails
Deploy a war file on Heroku
How to develop and register a Sota app in Java
Deploy Rails on Docker to heroku
How to deploy a Rails application on AWS (article summary)
How to make a Java array
How to automatically operate a screen created in Java on Windows
How to check Java installed on Mac
How to make a Java calendar Summary
How to publish an application on Heroku
[Introduction to Java] How to write a Java program
How to switch Java versions on Mac
How to make a Discord bot (Java)
Deploy to Heroku [Ruby on Rails] Beginner
Java: How to send values from Servlet to Servlet
Deploy a Tomcat-based Eclipse project on Heroku
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
How to make an app with a plugin mechanism [C # and Java]
Deploy Java web app to Azure with maven
How to display a web page in Java
java I tried to break a simple block
How to save images on Heroku to S3 on AWS
Ssh login to the app server on heroku
How to convert a solidity contract to a Java contract class
Note how to rollback Mysql deployed on Heroku
CICS-Run Java application-(1) Run a simple sample app
[Rails MySQL] How to reset DB on heroku
How to build a Pytorch environment on Ubuntu
[Java] Memo on how to write the source
How to deploy a system created with Java (Wicket-Spring boot) to an on-campus server
Steps to deploy to Heroku
Memo to build a Servlet environment on AWS EC2
How to create a Java environment in just 3 seconds
How to run the SpringBoot app as a service
How to jump from Eclipse Java to a SQL file
How to deploy Java to AWS Lambda with Serverless Framework
How to use java non-standard library on IntelliJ IDEA
java: How to write a generic type list [Note]
[Java] How to play rock-paper-scissors (equivalent to paiza rank A)
How to make JavaScript work on a specific page
How to connect to ClearDB from Sequel Pro on Heroku
How to create a data URI (base64) in Java
[Java] How to get a request by HTTP communication
[Ruby On Rails] How to reset DB in Heroku
As of April 2018 How to get Java 8 on Mac
Introducing New Relic to Java apps running on Heroku
How to download a file (Servlet, HTML, Apache, Tomcat)
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java