[Java] [Memorandum] Java: Create a simple project while learning the structure of the configuration file.

2 minute read

What you can understand through the article

・How to create a simple project using eclipse ・How to set up a server ・Contents of the web.xml file


・Eclipse is installed

  • Apache tomcat9 must be installed ・Java: 1.8 ・OS: Mac

How to set up a server

1, start eclipse
2, set up the server

2-1, Click “No server available...” 2-2, click “Tomcat v9.0 Server” 2-3, click Finish

3, Creating a dynamic project

3-1, click on File → New → Dynamic Project. 3-2, fill in the project name (this time appropriately “tomcat_test”)

(Note) Match the "Target runtime" column to the version of the server you set up.

3-3, click Finish

4, add the project to the established server

4-1, right click on the server 4-2, click on Add and Remove 4-3, select existing project in available column 4-4, click on Add → Done

5, confirm the above settings

5-1, When the server is set up, the Servers directory is created in the Package Explorer 5-2, Open the underlying server.xml in the source display and if the following description is at the end part, the server recognizes the project.


<Context docBase="project name" path="/project name" reloadable="false" source="org.eclipse.jst.jee.server:project name"/></Host>

6, create package and class files

6-1, Right-click on the project → New → Click Class 6-2, because the package name is included by default Enter the package name you want to delete and create. 6-3, enter the class name in the name field (this time “Hello World”) → click Finish 6-4, For the time being, write the following code in the created HelloWorld.java` file


mport java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {
   private static final long serialVersionUID = 1L;

   public void doGet(HttpServletRequest request,HttpServletResponse response)
      throws IOException, ServletException {
         PrintWriter out = response.getWriter();
         out.println("<title>Hello World!</title>");
         out.println("<h1>Hello World!</h1>");

7, Routing settings for executing class files

7-1, Right-click on WEB-INF under WebContent → click New → File 7-2, enter “web.xml” in the file name and create → complete 7-3, write the following code for the time being


<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
<servlet-class>Package name.Hello World</servlet-class>




The class file created by compiling the HelloWorld.java created this time with the following code is wrapped with the name hello.

web.xml (excerpt)

```xml (excerpt)

hello Package name.Hello World


Defined that the class file (`hello` in this case) wrapped further with the code below is executed (doGet) when `/servlet/hello` is accessed

#### **`web.xml (Excerpt 2)`**
```xml (Excerpt 2)

8, start and confirm

8-1, access to http://localhost:8080/tomcat_test/servlet/helloWorld 8-2, succeed if the following screen appears

![Screenshot 2020-08-26 1.26.24.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/496032/1ee41c95-0566-de72-bb91-(9149eca99410.png)