[JAVA] Web system construction (super basic) ②: AP server construction and basic operation

Purpose

For the purpose of building a Web system, check the construction and operation of a super-basic AP server. (Tomcat is used as the AP server.)

Environmental condition

--AP server - EC2:t2.micro - OS:Red Hat Enterprise Linux 8 (HVM), SSD Volume Type --Disk: General-purpose SSD (GP2) 10GB - Tomcat:Apache Tomcat 9 - Java:JDK 1.8

The security group settings are nice.

Construction procedure

ec2-Login as user

Switch to root user
$ sudo su - 

Confirmation of existence of openjdk package
# yum info java-*-openjdk

Install openjdk package
# yum install -y java-1.8.0-openjdk
Complete!Check the output of.

Java installation confirmation
# java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

Creating a dedicated user (tomcat)
# useradd -s /sbin/nologin tomcat

Download tomcat binary file
# curl -O http://ftp.riken.jp/net/apache/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz

Move tomcat binary files to installation directory
# mv apache-tomcat-9.0.30.tar.gz /opt/

Unzip the tomcat binary file
# cd /opt
# tar zxvf apache-tomcat-9.0.30.tar.gz

Change ownership of tomcat files
# chown -R tomcat:tomcat apache-tomcat-9.0.30

Create registration file for Tomcat service registration
# vi /etc/systemd/system/tomcat.service

========================================
[Unit]
Description=Apache Tomcat 9
After=network.target

[Service]
User=tomcat
Group=tomcat
Type=oneshot
PIDFile=/opt/apache-tomcat-9.0.30/tomcat.pid
RemainAfterExit=yes

ExecStart=/opt/apache-tomcat-9.0.30/bin/startup.sh
ExecStop=/opt/apache-tomcat-9.0.30/bin/shutdown.sh
ExecReStart=/opt/apache-tomcat-9.0.30/bin/shutdown.sh;/opt/apache-tomcat-9.0.30/bin/startup.sh

[Install]
WantedBy=multi-user.target
========================================

Change the authority of the created file
# chmod 755 /etc/systemd/system/tomcat.service

Tomcat service registration
# systemctl enable tomcat

Confirm that Tomcat service is stopped
# service tomcat status
   Active: inactive (dead)Check the output of.

Start the tomcat service
# service tomcat start
Redirecting to /bin/systemctl start tomcat.service

Check the status of the Tomcat service again
# service tomcat status
   Active: active (exited)Check the output of.

From the browser to the web server "Public DNS":Connect to 8080 "
Apache Tomcat/9.0.Confirm that 30 Welcome Pages are displayed.

Basic operation of AP server

Build a simple Web application using JSP and Java program and run it on the AP server. It is a simple application that inputs two numbers on the Web screen and processes and returns the result of the addition.

test.jsp


<!DOCTYPE html>
<html>
    <head>
        <title>requestForm</title>
    </head>
    <body>
        <p>please fill in the value</p>

        <%--Send text with GET method--%>
        <form action="./TestServlet">
            <p>
Please enter a number.
            </p>
            <p>
                <input type="text" name="text1"> + <input type="text" name="text2"> =
            </p>
            <input type="submit" value="Confirm">
        </form>
    </body>
</html>

TestServlet.java


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    /**
     *constructor.
     */
    public TestServlet() {
        super();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String inputText1 = "";	//Text 1 storage variable
    	String inputText2 = "";	//Text 2 storage variable
    	int num1 = 0; //Variables for digitizing text 1
    	int num2 = 0; //Variables for digitizing text 2
    	
    	inputText1 = request.getParameter("text1");
    	inputText2 = request.getParameter("text2");
    	
    	num1 = Integer.parseInt(inputText1);
    	num2 = Integer.parseInt(inputText2);
    	
    	int res = num1 + num2;
    	
		//Setting the contents to be output to the screen
        //Set that the output content is HTML
        response.setContentType("text/html");
        //UTF the character code of the output screen-Set to 8
        response.setCharacterEncoding("UTF-8");

        //Get Writer class instance to output to screen
        PrintWriter pw = response.getWriter();
        
        //Output HTML
        pw.println("<html>");
        pw.println("<head>");
        pw.println("<title>Calculation result</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<h1>Calculation result</h1>");
        pw.println("<p>" + inputText1 + " + " + inputText2 + " = " + res + "</p>");
        pw.println("</body>");
        pw.println("</html>"); 
    }
}

Prepare TestServlet.java as a Jar file including the library. (TestServlet.jar)

Preparing the environment to run the application

Create the required directories
# cd /opt/apache-tomcat-9.0.30/webapps
# mkdir -p test/WEB-INF/lib

web.Creating an xml file
# vi test/WEB-INF/web.xml

========================================
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">

	<servlet>
		<servlet-name>TestServlet</servlet-name>
    	    <servlet-class>TestServlet.TestServlet</servlet-class>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>TestServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
 
</web-app>
========================================

Deploy application

Below, place TestServlet.jar and test.jsp so that they match the directory structure.

/opt/apache-tomcat-9.0.30/webapps/test
test/
├── test.jsp
└── WEB-INF
    ├── lib
    │   └── TestServlet.jar
    └── web.xml

Operation check

Restart tomcat service
# service tomcat restart
Redirecting to /bin/systemctl start tomcat.service

From the browser to the web server "Public DNS":8080/test/test.Connect to "jsp"
Confirm that the created Web page is displayed and the processing is performed correctly.

next time, Web system construction (super basic) ③: DB server construction and basic operation is.

Recommended Posts

Web system construction (super basic) ②: AP server construction and basic operation
Web server construction commentary
One Liner Web Server
Ubuntu (18.04.3) Web server construction
Web system construction (super basic) ③: DB server construction and basic operation
Merry Christmas web server
Web system construction (super basic) ②: AP server construction and basic operation
Web server construction commentary
Linux Web server construction (Ubuntu & Apache)
Fastest and strongest web server architecture
Python basic operation 3rd: Object-oriented and class
Web server construction with Apache 2.4 (httpd 2.4.43) + PHP 7.4 on Linux ―― 4. Security (chown and firewalld)
Effective and simple Web server security measures "Linux"
Launch a web server with Python and Flask
CentOS8 server construction (network opening and package update)
Basic operation of Python Pandas Series and Dataframe (1)