[JAVA] Create a tomcat project using Eclipse

Introduction

Overview

Development environment

Development environment construction ①: OpenJDK 11 installation

JRE (Java Runtime Environment) is required to run Java Servlet, Eclipse, and Tomcat, so install OpenJDK. If you do not have JRE installed, you will get an error when starting Eclipse, so you need to do it before installing Eclipse.

  1. Go to the official OpenJDK website below and click the jdk.java.net/14 link in the Download section. (The link for jdk.java.net/14 changes every time OpenJDK is upgraded.) https://openjdk.java.net/

  2. Click Java SE 11 in the list of links on the left. (If you want to download the latest version of OpenJDK, you can download OpenJDK from this site.) https://jdk.java.net/14/

  3. Click Windows / x64 Java Development Kit. https://jdk.java.net/java-se-ri/11

  4. "openjdk-11 + 28_windows-x64_bin.zip" will be downloaded.

  5. When the download is complete, unzip it to any folder and add the absolute path of "jdk-11 \ bin" to your system environment variables. (Here, I unzipped it to C: \ openjdk and added "C: \ openjdk \ jdk-11 \ bin" to the system environment variables.)

  6. Start a command prompt and check the Java version. (If it is openjdk 11, it is OK.)

    >java --version
    openjdk 11 2018-09-25
    OpenJDK Runtime Environment 18.9 (build 11+28)
    OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
    

Development environment construction (2): Eclipse installation

After the OpneJDK installation is complete, download and launch Eclipse and install Tomcaat Plagin.

  1. Go to the Eclipse package installer download site and click "Windows 64-bit" in "Eclipse IDE for Java Developers". https://www.eclipse.org/downloads/packages/

  2. When you are on the download page, click the Download button.

  3. eclipse-java-2020-06-R-win32-x86_64.zip will be downloaded.

  4. After the download is complete, unzip it to any folder and double-click "eclipse.exe" to start Eclipse. (You will be required to enter the workspace folder path at startup, but leave the default settings.)

  5. Run the Eclipse main menu "Help"> "Eclipse Marketplace" to launch the "Eclipse Marketplace" dialog.

  6. In the "Search" field, enter "Tomcat" and press the "Go" button.

  7. Select "Eclipse Tomcat Plugin 9.1.4" and press the "Install" button.

  8. Select "I accept the terms of the license agreement" and press the "Finish" button.

  9. After restarting Eclipse, the Tomcat icon will appear in the menu in addition to the Eclipse main menu icon.

Development environment construction ③: TOMCAT installation

Install TOMCAT.

  1. Access the following site and click "Tomcat 9" in the "Download" item on the left. http://tomcat.apache.org/download-80.cgi

  2. Click "zip" under "Core" in "Binary Distributions". https://tomcat.apache.org/download-90.cgi

  3. "apache-tomcat-9.0.37.zip" will be downloaded.

  4. When the download is complete, unzip it to any folder. Here, unzip it to the following folder. C:\apache\apache-tomcat-9.0.37

  5. Add the following system environment variables.

-CATALINA_HOME: Path of TOMCAT installation folder ([Example] C: \ apache \ apache-tomcat-9.0.37) -JAVA_HOME: Java installation folder path ([Example] C: \ openjdk \ jdk-11) ~~~

Eclipse initial settings

Initialize Eclipse before creating a project. If you set it once at the beginning, you do not need to set it from the next time.

  1. Execute the main menu "Window"> "Preferences" to launch the "Preferences" dialog.

  2. Select General> Appearance> Colors and Fonts from the tree on the left.

  3. Select "Java Editor Text-Font" and press the "Edit" button to launch the "Font" dialog.

  4. To prevent garbled characters on Eclipse (if you write Japanese immediately after the comment "//", the characters will be garbled), change the font to MS Gothic and press the "Apply" button.

  5. Select General> Workspace from the tree on the left.

  6. Check "Other" in "Text file encoding" and select "UTF-8". (Assuming that the environment to deploy is Linux, set it to UTF-8 so that it will run on Linux.)

  7. Check "Other" in "New text file line delimiter", select "Unix", and press the "Apply" button.

  8. Select "Tomcat" from the tree on the left and check "9" for "Tomcat version".

  9. Set the Tomcat folder in "Tomcat Home". ([Example] C: \ apache \ apache-tomcat-9.0.37)

  10. Check "Server.xml" in "Context declaration mode" and set the path of Server.xml in the "Configuration file" field.

  11. Press the Apply button.

  12. Press the Apply and Close button.

Project creation

When creating a new project and setting Server.xml.

  1. Execute the main menu "File"> "New"> "Project" to launch the "New Project" dialog.

  2. Click the Java> Tomcat Project node in the tree.

  3. Enter the "Project name" and press the "Next" button.

  4. Check the path in the "Context Name" field and press the "Finish" button. (The path in the "Context name" field is the root part of the URL path when accessing the WEB server.) http: // localhost: 8080 / {path in the" context name "field}

  5. Confirm that the following line is added to "conf \ server.xml" under the Tomcat installation folder. If this line is not registered in Server.xml, the WEB screen will not be displayed even if you access the WEB server. ([Example] C: \ apache \ apache-tomcat-9.0.37 \ conf \ server.xml)

~~~
  1. By default, it is set to use port 8080. If you want to set other than port 8080, change the following part.

<Connector port = "8080" protocol = "HTTP / 1.1" ★ Change the "8080" part to the port number you want to set. connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true" /> ~~~

Servlet creation

Implements Servlet processing.

  1. Select the top node of the tree on "Package Explorer", right-click and click the pop-up menu "New"> "Class".

  2. Enter the class name in the "Nmae" field and "javax.servlet.http.HttpServlet" in the "Super class" field and press the "Finish" button. (Here, enter "SampleServlet" in the "Nmae" field, and leave the default settings except for the "Nmae" and "Super class" fields.)

  3. The "sample"> "WEB-INF / src"> "sample"> "SampleServlet.java" node is added to the tree on "Package Explorer".

    package sample;
    
    import javax.servlet.http.HttpServlet;
    
    public class SampleServlet extends HttpServlet {
    
    }
    
  4. Select the tree node "SampleServlet.java" on the "Package Explorer" and click "Source"> "Override / Inplement Methods" in the main menu to launch the "Override / Inplement Methods" dialog.

  5. Check "HttpServlet", "doGet" and "doPost" and click "OK" button to generate doGet method and doPost method. The doGet method is executed when the client sends a request, such as when the URL is accessed. Also, the doPost method is executed when a request is sent from the WEB server, such as when operating a form on the WEB screen.

    package sample;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class SampleServlet extends HttpServlet {
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		super.doGet(req, resp);
    	}
    
    	@Override
    	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		super.doPost(req, resp);
    	}
    
    }
    
  6. Add the following import process to SampleServlet.java.

    package sample;
    
    import java.io.IOException;
    

import java.io.PrintWriter; // Add

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

@SuppressWarnings ("serial") // Added public class SampleServlet extends HttpServlet { ~~~

  1. Add the following fields within the SampleServlet class of SampleServlet.java.

    public class SampleServlet extends HttpServlet {
    
    

// ↓ Add from here static final String head = "sample"; static final String msg = "

Write the text here. </ Div>"; static final String form = "
" + "

" + "

" + "
";

	static String result = "";

// ↑ Add up to here ~~~

  1. Change doGet () in SampleServlet.java to the following implementation. When the WEB server is accessed from the WEB browser, the HTML file of the WEB page is read and this is set in the response body so that it responds.

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    

//super.doGet (req, resp); // Comment out

// ↓ Add from here // Perform character encoding to prevent garbled Japanese characters resp.setContentType("text/html; charset=UTF-8");

// Create a web screen to be displayed on the client's web browser PrintWriter out = resp.getWriter(); out.println(""); out.println(head); out.println(""); out.println(msg); out.println(form); out.println(""); out.println(""); out.close(); // ↑ Add up to here } ~~~

  1. Change doPost () in SampleServlet.java to the following implementation. When data is sent from the form of the web browser to the web server, the request parameter (data sent from the client) is acquired and set in the response body to respond.

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    

//super.doPost (req, resp); // Comment out

// ↓ Add from here req.setCharacterEncoding ("UTF-8"); // Character encoding (Japanese character garbled measures) resp.setContentType ("text / html; charset = UTF-8"); // Character encoding (measures against garbled Japanese characters)

	try{

// Get the written text if the "write" button is pressed if(req.getParameter("send").equalsIgnoreCase("write")){ result += "



" + req.getParameter("name") + "
"; } }catch(Exception e){ e.printStackTrace(); result = "Write failure
" + e.getMessage (); }

// Create a web screen to be displayed on the client's web browser PrintWriter out = resp.getWriter(); out.println(""); out.println(head); out.println(""); out.println(msg); out.println(form); out.println(result); out.println(""); out.println(""); out.close(); // ↑ Add up to here } ~~~

web.xml creation

Create web.xml. web.xml is a file that configures the Servlet container.

  1. Select the tree node "WEB-INF" on "Package Explorer", right-click and click the pop-up menu "New"> "Other" to launch the "New" dialog.

  2. Click XML> XML file to launch the New XML file dialog.

  3. Enter "web.xml" in the "File name" field and press the "Finish" button to add the tree node "web.xml" on the "Package Explorer".

  4. Select the tree node "web.xml" on the "Package Explorer", right-click and click the pop-up menu "Open With"> "Generic Text Editor".

    <?xml version="1.0" encoding="UTF-8"?>
    
  5. Add the following code to web.xml.

    <?xml version="1.0" encoding="UTF-8"?>
    
    

<!-↓ Add from here-> sample sample.SampleServlet

	<servlet-mapping>
		<servlet-name>sample</servlet-name>					<!-- Servlet name -->
		<url-pattern>/servlet</url-pattern>					<!-- URL pattern -->
	</servlet-mapping>
</web-app>

<!-↑ Add up to here-> ~~~

Build

For builds, enable automatic builds so that they build automatically.

  1. Select the top node of the tree on "Package Explorer", right-click and click the pop-up menu "Reflesh" to automatically update the file structure of the tree on "Package Explorer" to the current state. ..

  2. Check "Project"> "Build Automatically" in the main menu to enable automatic build. (If the build is not executed, click "Project"> "Clean" in the main menu.)

Run

Run it when you have a build. First, use Eclipse to start a web server on Windows and access it from a web browser.

  1. Click the icon "Restart Tomcat" on the main menu.

  2. When you start a web browser and access the following URL, the GET method is sent from the client to the server, and the following web page is displayed. http://localhost:8080/sample/servlet

  3. Enter the text and press the "write" button, the client will send the POST method to the server and the following web page will be displayed.

Export project

Make a backup of your project before deleting it.

  1. Stop Tomcat.

  2. Select the top node of the tree on "Package Explorer", right-click and click the pop-up menu "Export" to launch the "Export" dialog.

  3. Select General> File System and press the Next button.

  4. Check the check box of the project.

  5. Enter the export destination path in the "To directory" field and press the "Finish" button.

Project import

If you want to restart the project, import the backed up project.

  1. Create a project according to the "Create project" item.

  2. Select the top node of the tree on "Package Explorer", right-click and click the pop-up menu "Import" to launch the "Import" dialog.

  3. Select General> File System and press the Next button.

  4. Check the check box of the project.

  5. Enter the import destination path in the "Into folder" field and press the "Finish" button.

  6. When asked "Overwrite'.classpath' in folder {project name}?", Select "Not to all".

WAR file creation

Create a WAR file (a collection of files necessary for the operation of the WEB application) to deploy (make it available).

  1. Click Project> Properties on the main menu to launch the Properties dialog.

  2. Select the tree node "Tomcat", open the "WAR Export Settings" tab and press the "Browse" button to launch the "Open" dialog.

  3. Enter the WAR file name to be saved in the "File name" field and click the "Open" button.

  4. When the path of the WAR file to be saved is reflected in the "WAR file to export" field, click the "Apply and close" button.

  5. Select the top node of the tree on "Package Explorer", right-click and execute "Tomcat project"> "Create WAR file according to project settings".

  6. A WAR file will be generated in the path set in the "WAR file to export" field.

Deployment environment

From here, prepare for deployment. Here, we will deploy to Ubuntu.

  • Ubuntu 20.04 LTS
  • OpenJDK 11
  • Tomcat 9

Deployment environment construction ①: OpenJDK 11 installation

First, install Java.

  1. Install OpenJDK 11.

    $ sudo apt-get install openjdk-11-jdk
    
  2. Check the Java version you have installed.

$ java --version openjdk 11.0.8 2020-07-14 OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04) OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing) ~~~

Deployment environment construction ②: TOMCAT installation

Then install TOMCAT.

  1. Access the following site and click "Tomcat 9" in the "Download" item on the left. http://tomcat.apache.org/download-80.cgi

  2. Click "tar.gz" under "Core" in "Binary Distributions". ⇒ "apache-tomcat-9.0.37.zip" will be downloaded. https://tomcat.apache.org/download-90.cgi

  3. When the download is complete, unzip it to any directory. Here, unzip to / opt.

    $ sudo tar zxvf apache-tomcat-9.0.37.tar.gz -C /opt
    
  4. Make sure it is unzipped to / opt.

    $ ls /opt/apache-tomcat-9.0.37/
    BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  temp  webapps  work
    

Deploy

When the deployment environment is ready, deploy it.

  1. The default setting of tomcat is root user because no user other than root user has execute permission.

    $ sudo su
    
  2. Copy the created WAR file to the folder under webapps of TOMCAT.

    # mv sample.war /opt/apache-tomcat-9.0.37/webapps
    
  3. Start TOMCAT.

    # cd /opt/apache-tomcat-9.0.37/bin
    # sh startup.sh
    
  4. When you start a web browser and access the following URL, the client sends a GET method to the server and the following web page is displayed. http: // {IP address of the deployed PC}: 8080 / sample / servlet

  5. Enter the text and press the "write" button, the client will send the POST method to the server and a web page like the one below will be displayed.

  6. Exit TOMCAT.

    # sh shutdown.sh
    

Recommended Posts