[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet

Overview

When I tried to develop a WEB application with Java, I looked it up and found that I think there will be a lot of articles on web application development using Java EE and Spring Boot. (* Probably the current trend is a matter of course.)

Automatic deployment by combining the above technologies and Maven (or Gradle) I think there are many people who want to try it.

However, when I tried to build a development environment by combining the above, A lot of errors occur in unexpected places, Even though the server is running, the response status of "404 Not Found" is returned, On the contrary, I think that there will be many addictive points.

So, first of all, using pure "Apache Tomcat" and "Java Servlet" I thought about developing a web application and displaying the screen.

You can use JavaEE or Spring Boot, try using Maven, and then extend it.

Development environment

-Eclipse IDE for Enterprise Java Developers -Apache Tomcat v8.0

Creating a dynamic web project

(1) Create a dynamic WEB project from Eclipse by following the steps below. "File"-> "New"-> "Dynamic WEB Project"

(2) You can name the project as you like, as you see this article. Here, let's call it "hello world".

(3) Regarding the target runtime, I think that it is "Apache Tomcat v7.0" by default. "Apache Tomcat v7.0" does not support Java EE 7, so Here, let's download and install "Apache Tomcat v8.0" from the new runtime.  01.png   ④ If you advance the screen, you will see the following screen. Here, select "Download and install".

※Caution※ After this, I specified the directory and the download and installation started. Create a new folder (apache-tomcat-8.0) in the eclipse directory in advance, Make sure to download & install there. If you don't do this, the folder structure will be messed up.  02.png

⑤ When the installation of Apache Tomcat is completed, the "Finish" button will be activated. Click the "Finish" button to complete the project creation.

Source code creation

① Open the "hellow old" package, right-click the "Java resource-> src" directory, and click Click "New-> Class". At that time, please change the name to "Hello World".   05.png

③ Click the Finish button and you can confirm that "HelloWorld.java" is created.

HelloWorld.java


package helloworld;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *A class that displays "Hello World" on the screen
 */
@WebServlet("/helloworld01")
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().write("Hello World!");
	}
}

④ Right-click the "hello world" package and select "Export-> WAR File". Please create "helloworld.war". (* The output destination can be anywhere.)

Create server (Apache Tomcat v8.0) & start

(1) From here, let's create a new "Apache Tomcat v8.0" server using Eclipse. I think there is a "server" tag in Eclipse. Please click here. 06.png

② Click Create New Server, set the server type to "Apache Tomcat v8.0", and ** * * Please do not change the settings and just "quit". * *** (Please do not worry about the selection of configured resources after clicking "Next" here. I think that setting it will interfere with the understanding of this manual. )

③ I'd like to start the created server, but please get acquainted with the settings a little more. Right-click on the created server and select "Properties" to move to the screen below.  image.png

④ Here, I think that the location is the initial value ** "Works Space Metadata" **, so Please switch the location and switch the location to "/ *** / Tomcat of localhost" as above. (Then apply and close)

⑤ After that, double-click the server and refer to the image below to make the settings. 07.png

⑥ After this, the server is finally started! Right-click on the server and press "Start". (* Please apply when a message prompting you to save the server changes appears)

(7) When the server startup is complete, try typing the following URL on your browser. It is OK if the management screen of Apache Tomcat is displayed.  http://localhost:8080/  09.png

Deploy WAR file

① After that, it is a flow to deploy the created WAR file to Apache Tomcat. To deploy, click the ** "Manager App" ** button on the right side of the above screen. The user authentication screen is displayed and I don't think I can proceed. These settings are set as follows from the directory where "Apache Tomcat v8.0" is installed. -Tomcat-users.xml (* in the conf directory)

First, let's open the above file.

(2) I think there is the following description on the 33rd to 39th lines.

[Before setting]

<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->

③ <-and-> are commented out, so delete them. Overwrite all the remaining parts with the following [After setting]. (Because there are various restrictions ...)

[After setting]

  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-status"/>
  <role rolename="manager-script"/>
  <role rolename="manager-jmx"/>
  <role rolename="admin-gui"/>
  <role rolename="admin-script"/>
  <user username="tomcat" password="s3cret" roles="tomcat"/>
  <user username="both" password="s3cret" roles="tomcat,role1"/>
  <user username="role1" password="s3cret" roles="role1"/>
  <user username="manager" password="s3cret" roles="manager-gui,manager-status,manager-script,manager-jmx"/>
  <user username="admin" password="s3cret" roles="admin-gui,admin-script"/>

④ After rewriting after setting, save "tomcat-users.xml".

(5) Now that the user settings have been completed, right-click the server and click "Resume" (* restart the server).

(4) When the Apache Tomcat management screen opens, click ** "Manager App" ** and authenticate with the following. User: manager Password: s3cret

⑤ I think that the page transition will be done correctly. [Supplement] With this setting, the following buttons can be authenticated by the following users. Please take a look at other pages at another time. (* Password is fixed to s3cret) -"Server Status" button ⇒ Use user "manager" -"Manager App" button ⇒ Use user "manager" -"Host Manager" button ⇒ Use user "admin"

⑥ When the "Manager App" screen opens, from the following in the middle Select "helloworld.war". 10.png

⑦ Click the "Deploy" button to complete the deployment! Now, if you look at the following URL on your browser ...  http://localhost:8080/helloworld/helloworld01 11.png

You can safely display "Hello World!"!

At the end

This time, I tried to create a server application by scratch. It didn't work because a server error occurred or the screen displayed "404 Not Found".

Of course, if you look it up, you will find solutions for each. Regarding the points I was addicted to this time, I couldn't find a site that was all in one site, so This article was created.

I hope it will be helpful for those who are addicted to the cause of similar errors.

Recommended Posts

[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
Web application development memo with MVN, Tomcat, JSP / Servlet with VScode
Comparison of WEB application development with Rails and Java Servlet + JSP
Roughly the flow of web application development with Rails.
Java web application development environment construction with VS Code (struts2)
Start web application development with Spring Boot
Creating a java web application development environment with docker for mac part1
The story that the Servlet could not be loaded in the Java Web application
Create a java web application development environment with docker for mac part2
[Java / PostgreSQL] Connect the WEB application to the database
Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation
[Tutorial] Download Eclipse → Run the application with Java (Pleiades)
Java desktop with the same front end as the WEB.
Web application development environment construction in Java (for inexperienced people)
The first WEB application with Spring Boot-Making a Pomodoro timer-
Until you create a Web application with Servlet / JSP (Part 1)
Try developing a containerized Java web application with Eclipse + Codewind
[Personal development] 8 things I did after publishing the web application
Web application development article summary
Web application built with docker (1)
Of the three Java embedded web servers, Tomcat, Jetty, and Undertow, which one worked with GraalVM?
AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-
Spring5 MVC Web application development with Visual Studio Code Environment construction (Installation of JDK11, Maven, Tomcat, Visual Studio Code)
Create a simple web server with the Java standard library com.sun.net.httpserver
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
Environment construction for Servlet application development
Install Java and Tomcat with Ansible
Prepare Java development environment with Atom
About the current development environment (Java 8)
Build a web application with Javalin
Follow the link with Selenium (Java)
Html5 development with Java using TeaVM
Web application creation with Nodejs with Docker
Run an application made with Java8 with Java6
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 1/3 [Preparation]
Spring Boot2 Web application development with Visual Studio Code SQL Server connection
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Deploy the WEB application by Spring Boot to Tomcat server as WAR
Spring5 MVC Web application development with Visual Studio Code Maven template creation
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
Introduction to Java development environment & Spring Boot application created with VS Code
What did I prepare for when I entered an Android application development project while developing the Web in Java?