Launch Servlet + Tomcat locally on Macbook Pro (text editor + terminal only)

I left it to the IDE for a long time and didn't know how to make Servlet, so I built it on my own machine and studied.

Premise

Machine: Macbook Pro OS:MacOS Mojave

Basically, I am working according to the following page, but since there were many parts for Windows, I arranged that area. https://www.javadrive.jp/servlet/

Described as "/ Users / [macbook user name] /" = "~ /".

Environment

1. Java download

Java was already in the machine, so I'll omit it. The version is below.

$java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

Since it is a personal development, Java usage is free of charge. https://java.com/ja/download/

The Oracle Technology Network license agreement for the new Oracle Java SE is very different from the previous Oracle Java license. The new license allows certain uses at no charge, such as for personal use or for development purposes, but no longer allows other uses licensed under the previous Oracle Java license. Please read the terms carefully before downloading and using this product.

2. Download tomcat

I downloaded it from the following. http://tomcat.apache.org/

Download with the latest version "9.0.30". Select Binary Distributions> Core> zip. Since it was written in the README as follows, I made it a zip instead of tar. http://us.mirrors.quenda.co/apache/tomcat/tomcat-9/v9.0.30/README.html

NOTE: The tar files in this distribution use GNU tar extensions, and must be untarred with a GNU compatible version of tar. The version of tar on Solaris and Mac OS X will not work with these files.

Unzip the zip (double-click it to unzip it) and move it to an easy-to-understand location. In my case, I did the following.

  1. Open "Applications" in Finder and create "Tomcat" directory /Applications/Tomcat
  2. Copy and paste the unzipped file under it. /Applications/Tomcat/apache-tomcat-9.0.30

Set the Tomcat environment variable "CATALINA_HOME"

This time I wanted to apply environment variables even if I opened another terminal, so I set them in ./bashrc and ./bash_profile. I didn't have ./bashrc or ./bash_profile on my machine, so I created it this time.

The place of creation is as follows. ~/.bash_profile ~/.bashrc

File creation is done in the terminal.

$cd ~/
$touch ./bash_profile
$touch ./bashrc
$ls -la
→ Here, ".bash_profile "and".OK if "bashrc" appears

Edit the contents using vim.

$vi .bash_profile
→ Press the "a" key to enter insert mode, and then enter the contents of the file.
→ When finished, press the "esc" key and click ":Press "wq". If you make a mistake, ":q!If you do, it will not be saved

$vi .bashrc
→ Same as above.

The contents of each file are as follows.

.bash_profile


# ~/.bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

.bashrc


# EXPORT
export CATALINA_HOME=/Applications/Tomcat/apache-tomcat-9.0.30

# ALIAS
alias ll='ls -la'

In the terminal, load the last set contents into the machine. (If you don't do this, nothing will happen forever)

$source ~/.bashrc
$source ~/.bash_profile

Actual work

3. Display index.html

Create a directory in any location and then prepare the contents

In my case, create below ~/study/20191222_servlet-sample → The directory created here will be the root of the application

Under the created directory, create a directory with the following structure.

[The directory you just created]

Create index.html.

The storage place is as follows. ~/study/20191222_servlet-sample/helloworld/

[The directory you just created]

index.html


<html>
  <head>
    <title>Index page exam</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>test_top page</h1>
  </body>
</html>

Application context settings

I want Tomcat to recognize the created directory, so I wrote a configuration file.

Create a directory as follows. /Applications/Tomcat/apache-tomcat-9.0.30/conf ↓ /Applications/Tomcat/apache-tomcat-9.0.30/conf/Catalina/localhost

[Applications]

Create hello.xml below. /Applications/Tomcat/apache-tomcat-9.0.30/conf/Catalina/localhost/hello.xml

[Applications]

hello.xml


<Context path="/hello" docBase="/Users/[macbook username]/study/20191222_servlet-sample/helloworld" />

-It is necessary to match the xml name and the path name. -The specified name is given after the FQDN. * This time, http: // localhost: 8080 / hello -For docBase, specify the directory name created earlier. → In this way, you can freely specify the root directory of the application in the configuration file in Tomcat.

Start Tomcat, check operation, end

Start tomcat via terminal

$cd $CATALINA_HOME/bin
$./startup.sh

Access the URL below, and if the index.html created earlier is displayed, it's OK. http://localhost:8080/hello

Exit tomcat via terminal

$cd $CATALINA_HOME/bin
$./startup.sh

4. Display of Servlet

Create HelloWorld.java file

Create HelloWorld.java in any location. In my case, create it below. ~/study/20191222_servlet

The contents are the same as https://www.javadrive.jp/servlet/context/index1.html.

Create HelloWorld.class

Compile.

cd ~/study/20191222_servlet
ls

→ Confirm that HelloWorld.java exists here

javac HelloWorld.java

→ Compile failed (there is no jar related to Servlet) When using Servlet, it is necessary to compile the jar of servlet by specifying the classpath. (Conversely, you do not have to specify anything else)

javac -classpath $CATALINA_HOME/lib/servlet-api.jar HelloWorld.java

→ Compiles successfully and finishes quickly.

Make sure you have the class files when you're done. (Check with ls command or Finder)

$ ll
total 16
drwxr-xr-x  4 hoge  staff  128 12 25 22:08 .
drwxr-xr-x  5 hoge  staff  160 12 25 21:43 ..
-rw-r--r--  1 hoge  staff  894 12 25 22:08 HelloWorld.class ← this
-rw-r--r--@ 1 hoge  staff  583 12 25 22:08 HelloWorld.java

Move HelloWorld.class

Move HelloWorld.Class under classes in the directory you just created. ~/study/20191222_servlet-sample/helloworld/classes/HelloWorld.class

[The directory you just created]

Describe the mapping setting in web.xml

Set web.xml to access the placed class file via URL. ~/study/20191222_servlet-sample/helloworld/WEB-INF/web.xml

[The directory you just created]

The contents are the same as https://www.javadrive.jp/servlet/context/index3.html.

web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <servlet>
    <servlet-name>helloworld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>helloworld</servlet-name>
    <url-pattern>/helloworld</url-pattern>
  </servlet-mapping>

</web-app>
item name Contents
servlet-name Only used for linking in xml
servlet-class Write the class file name "Hello World" created earlier
url-pattern You will be able to access by URL based on the specified value

For example, this time, the flow is as follows.

  1. Access the URL of http: // localhost: 8080 / hello / helloworld
  2. Access HelloWorld.class by setting web.xml.
  3. The html described earlier in HelloWolrd.java is expanded and responded.

Start Tomcat, check operation, end

Start tomcat via terminal

$cd $CATALINA_HOME/bin
$./startup.sh

Access the URL below, and if the html described in HelloWorld.java created earlier is displayed, it's OK. http://localhost:8080/hello/helloworld

Exit tomcat via terminal

$cd $CATALINA_HOME/bin
$./startup.sh

Impressions

-It was quite troublesome to compile, put it back, and start it. I felt it was very troublesome even though I compiled only one this time. (Will it be less troublesome once you get used to it?) It's no wonder that something was born that automatically compiles (+ deploys if necessary).

Recommended Posts

Launch Servlet + Tomcat locally on Macbook Pro (text editor + terminal only)
Deploy Java Servlet app locally on Tomcat