Deploying JSP Web App on Heroku
Recently, I came across the term JavaServer Pages (JSP) on a job posting. I didn’t know what that is, so in order to get the basic idea, I built a JSP web app using Eclipse following a tutorial and deployed it on Heroku. Check out this simple web app and its source code.
After finishing the tutorial, we’ll get a completed MySQL powered JSP web app on localhost. Now, let me talk about how to deploy it on Heroku.
Create Heroku App
Let’s get started by creating a heroku app first by running the following command in terminal:
$ heroku create
Setup Database
Since Heroku doesn’t natively support MySQL, we need to install the ClearDB add-on in our app to provide MySQL support.
ClearDB is a powerful, fault tolerant database-as-a-service in the cloud for your MySQL powered applications.
Install ClearDB and create a remote database:
$ heroku addons:create cleardb:ignite --app <app_name>
Connect our app to ClearDB
Get our database URL:
$ heroku config | grep CLEARDB_DATABASE_URL
What we’ll get:
CLEARDB_DATABASE_URL => mysql://adffdadf2341:[email protected]/heroku_db?reconnect=true
It’s in this format:
mysql://DB_USERNAME:DB_PASSWORD@DB_HOST/DB_NAME?reconnect=true
Then we use these information to change the connection parameters accordingly in our app.
Copy local database to remote database:
mysqldump -h [server] -u [user] -p[password] local_DB_name | mysql -h [server] -u [user] -p[password] remote_DB_name
Deploy with Heroku CLI plugin
Install the heroku-deploy
plugin:
$ heroku plugins:install heroku-cli-deploy
Create a WAR file of our app (Eclipse):
Right click on the project > Export > WAR file
Deploy our WAR file:
$ heroku war:deploy <path_to_war_file> --app <app_name>
View our app on Heroku:
$ heroku open --app <app_name>
Learning Resources
- Installing Tomcat 8.5 on macOS 10.12 Sierra
- Servlet JSP Tutorial
- Create a Simple Java Web Application Using Servlet, JSP and JDBC
- JSP tricks to make templating easier?
- Spring MVC – How to include JS or CSS files in a JSP page
- Integrating Github With Eclipse
- Connector/J Installation
- ClearDB MySQL
- Using MySQL on Heroku
- MySQL copy/duplicate database without using mysqldump
- Heroku Deploy War/Jar