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.
In the Eclipse environment, right-click the dynamic project file and select Export from the menu to export it as a War file.
Select the export destination as the destination and export to any directory.
It is assumed that Heroku CLI has been installed in advance. For installation here
$ heroku login
I think this can be either from the CLI or on the Heroku web application.
When creating from the web, you can create an App from the New button on the upper right of the Dashboard.
If you want to deploy the Servlet application with the War file, download this CLI.
$ heroku plugins:install java
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.
It was displayed!
The reference article is the official Heroku Doc below. https://devcenter.heroku.com/articles/war-deployment
By the way, when I tried to log in to the app I uploaded to the server above, I got a 404 error.
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 change
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>
Recommended Posts