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.
Eclipse Pleiades All in One is compatible with Eclipse in Japanese and includes software required for Java Servlet development such as JDK and Tomcat. First, download and launch Eclipse Pleiades All in One and install Tomcaat Plagin.
Go to the following Pleiades (a tool for Japaneseizing Java apps) site and click "Eclipse 2020". ⇒ Moves to the page where the list of "Download" buttons is lined up. https://mergedoc.osdn.jp/
Click the "Download" button on the combination of "Windows 64bit", "Full Edition" and "Java". ⇒ pleiades-2020-06-java-win-64bit-jre_20200702.zip will be downloaded.
When the download is complete, unzip it to any folder and double-click eclipse \ eclipse.exe to start Eclipse. (You will be required to enter the workspace folder path at startup, but leave the default settings.)
Execute 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 Terms of Use" and press the "Finish" button.
When the Certificate Dialog "Do you trust these certificates?" Is displayed, check the certificate issuer and press the "Accept Selection" button.
After restarting Eclipse, the Tomcat icon will appear in the menu in addition to the Eclipse main menu icon.
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 "Window"> "Settings" in the main menu to start the "Settings" dialog.
First, set up Tomcat. Select "Tomcat" from the tree on the left.
Set the Tomcat folder in the "Tomcat" home. (Set "tomcat \ 9" under the folder where you unzipped pleiades-2020-06-java-win-64bit-jre_20200702.zip.)
Check "Server.xml" in "Context declaration mode" and set the path of Server.xml in the "Configuration file" field. (Set "tomcat \ 9 \ conf \ server.xml" under the folder where you unzipped pleiades-2020-06-java-win-64bit-jre_20200702.zip.)
Press the "Apply" button.
Next, assume that the environment you are deploying to is Linux, and configure it to run on Linux. Select General> Workspace from the tree on the left.
Under Text File Encoding, check Other and select UTF-8.
Under New Text File Line Delimiter, check Other and select Unix.
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 click 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 the Package Explorer, right-click and click New> Class in the pop-up menu.
Enter the class name in the "Name" field and "javax.servlet.http.HttpServlet" in the "Superclass" field, and click the "Finish" button. (Here, enter "SampleServlet" in the "Name" field, and leave the settings other than the "Name" field and "Superclass" field as default.) ⇒ "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"> "Method Override / Implementation" on the main menu to launch the "Method Override / Implementation" dialog. ..
Check "HttpServlet"> "doGet" and press the "OK" button to generate doGet () and doPost (). doGet () works when it receives a GET method. The GET method operates when the WEB server is accessed from the client's WEB browser. Also, doPost () works when it receives a POST method. The POST method works when data is sent from the client's web browser form to the web server.
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.
package sample;
import java.io.IOException;
import java.io.PrintWriter; // Add ~~~
Modify the implementation of doGet () as follows. I am creating a web page that is displayed on the client's web browser when the client accesses the web server.
@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("
"); out.println("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 click the "Finish" button. ⇒ A tree node "web.xml" is added on "Package Explorer".
Select the tree node "web.xml" on "Package Explorer", right-click and click the pop-up menu "Open Next"> "General 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 the Package Explorer, right-click and click Refresh on the pop-up menu. ⇒ The file structure of the tree on "Package Explorer" is automatically updated to the current state.
Select the top node of the tree on the Package Explorer, right-click and click the pop-up menu Build Project to build.
Run it when you can build it. 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 the browser and access the following URL, the client sends the GET method to the server and the following WEB page is displayed.
http://localhost:8080/sample/sevlet
If you want to debug, do the following:
Right-click and click the pop-up menu "Switch Breakpoints" to set a breakpoint and press F8 (Run).
Start your browser and access the following URL to break at the location where you set the breakpoint. You can also step out with the F6 key and step in with the F5 key.
http://localhost:8080/sample/sevlet
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.
Make a backup of your project before deleting it.
Stop Tomcat.
Select the top node of the tree on the 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 "Destination Directory" field and click the "Finish" button.
Delete the project and delete the settings in Server.xml.
Stop Tomcat.
Select the top node of the tree on the Package Explorer, right-click and click the pop-up menu Delete to launch the Delete Resource dialog.
Check "Delete project content from disk (irreversible)" and click the "OK" button.
When the Remove Project Context Definition from Server.xml dialog opens, press the OK button.
When importing a project, import the backed up project.
Create a project according to the "Create project" item.
Select the top node of the tree on the Package Explorer, right-click and click the pop-up menu Insport to launch the Insport dialog.
Select General> File System and press the Next button.
Check the check box of the project.
Enter the import destination path in the "From the following directory" field and click the "Finish" button.
When asked "Are you sure you want to overwrite?", Select "No All".
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 the GET method to the server and the following web page is displayed.
http: // {IP address of the deployed PC}: 8080 / sample / servlet
Exit TOMCAT.
# sh shutdown.sh
Recommended Posts