Java Servlet is a program that generates dynamic web pages in a web application implemented in Java. The WEB application is a server / client system, and as shown in the figure below, the client sends a request to the server, and the server returns a response to the client to operate.
Java Servlet does not work alone, but with software called a Servlet container. Tomcat corresponds to this Servlet container. When you send a request from your web browser to your web server, Tomcat receives this request. The Servlet generates a dynamic web page according to the type of request, and Tomcat sends this dynamic web page to the web browser as a response. The web browser displays this dynamic web page.
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.
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/
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/
Click Windows / x64 Java Development Kit. https://jdk.java.net/java-se-ri/11
"openjdk-11 + 28_windows-x64_bin.zip" will be downloaded.
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.)
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)
After the OpneJDK installation is complete, download and launch Eclipse and install Tomcaat Plagin.
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/
When you are on the download page, click the Download button.
eclipse-java-2020-06-R-win32-x86_64.zip will be downloaded.
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.)
Run the Eclipse main menu "Help"> "Eclipse Marketplace" to launch the "Eclipse Marketplace" dialog.
In the "Search" field, enter "Tomcat" and press the "Go" button.
Select "Eclipse Tomcat Plugin 9.1.4" and press the "Install" button.
Select "I accept the terms of the license agreement" and press the "Finish" button.
After restarting Eclipse, the Tomcat icon will appear in the menu in addition to the Eclipse main menu icon.
Install TOMCAT.
Access the following site and click "Tomcat 9" in the "Download" item on the left. http://tomcat.apache.org/download-80.cgi
Click "zip" under "Core" in "Binary Distributions". https://tomcat.apache.org/download-90.cgi
"apache-tomcat-9.0.37.zip" will be downloaded.
When the download is complete, unzip it to any folder. Here, unzip it to the following folder. C:\apache\apache-tomcat-9.0.37
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) ~~~
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.
Execute the main menu "Window"> "Preferences" to launch the "Preferences" dialog.
Select General> Appearance> Colors and Fonts from the tree on the left.
Select "Java Editor Text-Font" and press the "Edit" button to launch the "Font" dialog.
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.
Select General> Workspace from the tree on the left.
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.)
Check "Other" in "New text file line delimiter", select "Unix", and press the "Apply" button.
Select "Tomcat" from the tree on the left and check "9" for "Tomcat version".
Set the Tomcat folder in "Tomcat Home". ([Example] C: \ apache \ apache-tomcat-9.0.37)
Check "Server.xml" in "Context declaration mode" and set the path of Server.xml in the "Configuration file" field.
Press the Apply button.
Press the Apply and Close button.
When creating a new project and setting Server.xml.
Execute the main menu "File"> "New"> "Project" to launch the "New Project" dialog.
Click the Java> Tomcat Project node in the tree.
Enter the "Project name" and press the "Next" button.
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}
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)
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" /> ~~~
Implements Servlet processing.
Select the top node of the tree on "Package Explorer", right-click and click the pop-up menu "New"> "Class".
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.)
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 {
}
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.
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);
}
}
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 { ~~~
Add the following fields within the SampleServlet class of SampleServlet.java.
public class SampleServlet extends HttpServlet {
// ↓ Add from here static final String head = "
static String result = "";
// ↑ Add up to here ~~~
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 } ~~~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 += "
// 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 } ~~~Create web.xml. web.xml is a file that configures the Servlet container.
Select the tree node "WEB-INF" on "Package Explorer", right-click and click the pop-up menu "New"> "Other" to launch the "New" dialog.
Click XML> XML file to launch the New XML file dialog.
Enter "web.xml" in the "File name" field and press the "Finish" button to add the tree node "web.xml" on the "Package Explorer".
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"?>
Add the following code to web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!-↓ Add from here->
<servlet-mapping>
<servlet-name>sample</servlet-name> <!-- Servlet name -->
<url-pattern>/servlet</url-pattern> <!-- URL pattern -->
</servlet-mapping>
</web-app>
<!-↑ Add up to here-> ~~~
For builds, enable automatic builds so that they build automatically.
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. ..
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 it when you have a build. First, use Eclipse to start a web server on Windows and access it from a web browser.
Click the icon "Restart Tomcat" on the main menu.
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
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.
Make a backup of your project before deleting it.
Stop Tomcat.
Select the top node of the tree on "Package Explorer", right-click and click the pop-up menu "Export" to launch the "Export" dialog.
Select General> File System and press the Next button.
Check the check box of the project.
Enter the export destination path in the "To directory" field and press the "Finish" button.
If you want to restart the project, import the backed up project.
Create a project according to the "Create project" item.
Select the top node of the tree on "Package Explorer", right-click and click the pop-up menu "Import" to launch the "Import" dialog.
Select General> File System and press the Next button.
Check the check box of the project.
Enter the import destination path in the "Into folder" field and press the "Finish" button.
When asked "Overwrite'.classpath' in folder {project name}?", Select "Not to all".
Create a WAR file (a collection of files necessary for the operation of the WEB application) to deploy (make it available).
Click Project> Properties on the main menu to launch the Properties dialog.
Select the tree node "Tomcat", open the "WAR Export Settings" tab and press the "Browse" button to launch the "Open" dialog.
Enter the WAR file name to be saved in the "File name" field and click the "Open" button.
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.
Select the top node of the tree on "Package Explorer", right-click and execute "Tomcat project"> "Create WAR file according to project settings".
A WAR file will be generated in the path set in the "WAR file to export" field.
From here, prepare for deployment. Here, we will deploy to Ubuntu.
First, install Java.
Install OpenJDK 11.
$ sudo apt-get install openjdk-11-jdk
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) ~~~
Then install TOMCAT.
Access the following site and click "Tomcat 9" in the "Download" item on the left. http://tomcat.apache.org/download-80.cgi
Click "tar.gz" under "Core" in "Binary Distributions". ⇒ "apache-tomcat-9.0.37.zip" will be downloaded. https://tomcat.apache.org/download-90.cgi
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
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
When the deployment environment is ready, deploy it.
The default setting of tomcat is root user because no user other than root user has execute permission.
$ sudo su
Copy the created WAR file to the folder under webapps of TOMCAT.
# mv sample.war /opt/apache-tomcat-9.0.37/webapps
Start TOMCAT.
# cd /opt/apache-tomcat-9.0.37/bin
# sh startup.sh
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
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.
Exit TOMCAT.
# sh shutdown.sh
Recommended Posts