・ How to make a simple project using eclipse ・ How to set up a server ・ Contents of web.xml file
-Eclipse is installed -Apache tomcat9 must be installed -Java: 1.8 ・ OS: Mac
2-1, Click "No server available ...
"
2-2, Click " Tomcat v9.0 Server
"
2-3, click Finish
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-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-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-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-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>
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-1, Access http: // localhost: 8080 / tomcat_test / servlet / helloWorld
8-2, Success if the following screen appears
Recommended Posts