[Note] Java: Create a simple project while learning how the configuration file works.

What you can see through the article

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

Premise

-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 a server

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

3, Create a dynamic project

3-1, Click File → New → Dynamic Project 3-2, enter project name (this time appropriately" tomcat_test ") (Caution) Match the "Target Runtime" field to the version of the server you set up. </ font> 3-3, click Finish

4, Add a project to the server you set up

4-1, Right click on the server you set up 4-2, click Add and Remove 4-3, Select the project that exists in the available field 4-4, Click Add → Done

5, Check the above settings

5-1, When you set up a server, the Servers directory is created in the Package Explorer. 5-2, Open the subordinate server.xml in the source view, and if there is the following description at the end, it means that the server recognizes the project.

Servers/server.xml


<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 on Class 6-2, Because the package name is included by default Enter the name of the package you want to delete and create. 6-3, Enter the class name in the name field (this time " Hello World ") → click Finish 6-4, Write the following code in the created HelloWorld.java filefor the time being

HelloWorld.java


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 {
         response.setContentType("text/html");
         PrintWriter out = response.getWriter();
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Hello World!</title>");
         out.println("</head>");
         out.println("<body>");
         out.println("<h1>Hello World!</h1>");
         out.println("</body>");
         out.println("</html>");
      }
}

7, Routing settings to execute class files

7-1, Right-click 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

WEBINF/web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>package name.HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>
hello
</servlet-name>
<url-pattern>
/servlet/helloWorld
</url-pattern>
</servlet-mapping>

</web-app>

Commentary

The following code wraps the class file that compiles HelloWorld.java created this time with the name hello.

web.xml(Excerpt)


<servlet-name>hello</servlet-name>
<servlet-class>package name.HelloWorld</servlet-class>
</servlet>

Defined that the class file (here hello) further wrapped in the code below is executed (doGet) when / servlet / hello is accessed.

web.xml(Excerpt 2)


<servlet-mapping>
<servlet-name>
hello
</servlet-name>
<url-pattern>
/servlet/helloWorld
</url-pattern>
</servlet-mapping>
8, start and check

8-1, Access http: // localhost: 8080 / tomcat_test / servlet / helloWorld 8-2, Success if the following screen appears

スクリーンショット 2020-08-26 1.26.24.png

Recommended Posts

[Note] Java: Create a simple project while learning how the configuration file works.
[Java] Create a temporary file
Create a Java project using Eclipse
[Java] How to create a folder
Create a simple web server with the Java standard library com.sun.net.httpserver
How to create a jar file or war file using the jar command
How to create a new Gradle + Java + Jar project in Intellij 2016.03
[Gradle] Build a Java project with a configuration different from the convention
[Java] How to use the File class
To create a Zip file while grouping database search results in Java
[Java] Create a jar file with both compressed and uncompressed with the jar command
How to find out the Java version of a compiled class file
How to read log4j configuration file in Java project summarized in jar file Memo
Java Performance Chapter 4 How the JIT Compiler Works
Create a simple bulletin board with Java + MySQL
[1st] How to create a Spring-MVC framework project
How to save a file with the specified extension under the directory specified in Java to the list
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
Create a docker image that runs a simple Java app
How to create a Java environment in just 3 seconds
How to jump from Eclipse Java to a SQL file
java: How to write a generic type list [Note]
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to create a data URI (base64) in Java
A note for Initializing Fields in the Java tutorial
[Note] Create a java environment from scratch with docker
How to convert a file to a byte array in Java
[Java] Create a filter
How to create a lightweight container image for Java apps
How to deploy a simple Java Servlet app on Heroku
How to create a form to select a date from the calendar
How to create a placeholder part to use in the IN clause
Create a method to return the tax rate in Java
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
About the behavior when doing a file map with java
Create a MOB using the Minecraft Java Mythicmobs plugin | Preparation 1
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
[Swift5] How to create a .gitignore file and the code that should be written by default