background

Apache Tomcat is an open source implementation of Java Servlets, JavaServer Pages (JSP), the Java expression language, and Java’s WebSocket technology. Tomcat is commonly referred to as a Web container or Servlet container.

Mapping between Tomcat versions and corresponding specifications:

Download address

https://tomcat.apache.org/download-90.cgi
Copy the code

Download to local and unzip:

Go to the home directory:

Tomcat Directory Introduction

bin

Startup, shutdown, and other scripts. These.sh files (for Unix systems) are functional copies of these.bat files (for Windows systems). Because the Win32 command line lacks some functionality, some additional files are included here.

For example, startup.bat is used to start Tomcat in Windows, and startup.sh is used in Linux. There is also a shutdown shutdown script.

conf

Tomcat configuration files and associated DTDS. The most important file here is server.xml. It is the main configuration file for the container.

Catalina. policy: tomcat: a security policy file that controls PERMISSIONS related to the JVM. For details, see java.security.Permission.

Catalina. properties: tomcat Catalina behavior control configuration file, for example, Common ClassLoader.

Logging. properties: Tomcat log configuration file. The Logging is JDK Logging.

Server.xml: Tomcat Server configuration file (very important to me as a developer).

Context. XML: a global context configuration file that monitors and loads resource files. When the monitored file changes, it is automatically loaded.

Tomcat-user. XML: tomcat role configuration file.

Web. XML: Servlet standard web. XML deployment file, tomcat default implementation part of the configuration into:

  • Org. Apache. Catalina. Servlets. DefaultServlet.
  • org.apache.jasper.servlet.JspServlet

logs

Log files are located here by default.

Localhost is useful, so look at this file when your Tomcat doesn’t start. Such as:

  • NoClassDefFoundError
  • ClassNotFoundException

Access is the most useless.

Catalina.{date} is mainly console output, where all the logs are stored.

webapps

This is where your WebApp is located. In fact, these are all one project.

Simplify the way web deployment is done. In an online environment our application wouldn’t be here. The best way to do that is external.

lib

Tomcat holds a common class library. Such as:

  • Ecj-4.17.jar: Eclipse Java compiler
  • Jasper. Jar: JSP compiler.

work

Store files compiled at Tomcat runtime, such as JSP compiled files.

temp

Store temporary files generated at run time.

Start tomcat

Start tomcat

In Windows, you can directly start startup.bat in the bin directory. In Linux, you can start startup.sh.

Double click to activate. The console will type port 8080 and then we will access:

http://localhost:8080/
Copy the code

Page display:

This means that our Tomcat has been started successfully.

In this case, http://localhost:8080/ requests the ROOT directory.

For example: we can also http://localhost:8080/manager

The Servlet project is deployed into Tomcat

Creating a Web project

Use Maven to create a Web project. If tomcat is a Servlet container, create a Servlet class in your project and copy it into a WAR package.

The project structure is as follows:

Add the dependent

The < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Tian. Maven < / groupId > < artifactId > my - web - maven < / artifactId > <packaging>war</packaging> <version> 1.0-snapshot </version> <name>my-web-maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> </dependencies> <build> <finalName>my-web-maven</finalName> </build> </project>Copy the code

Create DemoServlet

package com.tian.maven; 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; @WebServlet(urlPatterns = "/demo") public class DemoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String msg = req.getParameter("message"); String contextPath = req.getServletContext().getContextPath(); System.out.println("contextPath=" + contextPath); resp.getWriter().println(msg); }}Copy the code

Web.xml is nothing but packaging.

<! DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc., / / DTD Web Application / 2.3 / EN "" http://java.sun.com/dtd/web-app_2_3.dtd" > < Web - app > < the display - the name > Archetype Created  Web Application</display-name> </web-app>Copy the code

Index.jsp finds nothing for that either:

<html> <body> <h2>Hello World! </h2> </body> </html>Copy the code

Run the MVN command to create a WAR package.

Copy the war package (my-web-maven folder is the same) to tomcat webapps directory:

Then go to the bin directory and double-click

Once the project is up and running, visit http://localhost:8080/

It proves that our project has been successfully launched.

Next we access the Servlet we wrote:

http://localhost:8080/demo?message=hello

Error, HTTP status 404 – not found;

Note: When accessing the contextPath, we need to treat the project name as contextPath.

http://localhost:8080/my-web-maven/demo?message=hello
Copy the code

Page output

hello

With ease, our project was successfully deployed to Tomcat.

The project in IDEA is deployed to Tomcat

Create a servlet project with the name my-servlet.

Create a new class MyServlet

Go to the Tomcat directory we just installed, go to the lib directory and select servlet-api.jar.

Click ok.

Click Apply, and then OK.

Modify MyServlet contents:

package com.tian.servlet; import javax.servlet.*; import java.io.IOException; import java.io.PrintWriter; Public class MyServlet implements Servlet {private transient ServletConfig ServletConfig; @Override public void init(ServletConfig servletConfig) throws ServletException { this.servletConfig = servletConfig; } @Override public ServletConfig getServletConfig() { return servletConfig; } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { String servletName = servletConfig.getServletName(); SetContentType ("text/ HTML "); response.setContentType("text/ HTML "); PrintWriter writer = response.getWriter(); writer.println("<html><head></head>" + "<body> Hello this is " + servletName + "</body></html>"); } @Override public String getServletInfo() { return "my first Servlet"; } @Override public void destroy() { } }Copy the code

Modify the contents of the web. XML file:

<? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" Version = "3.1" > < servlet > < servlet - name > myServlet < / servlet - name > <servlet-class>com.tian.servlet.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/demo</url-pattern> </servlet-mapping> </web-app>Copy the code

In addition, we will modify the index.jsp content, mainly for better demonstration:

<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <body> <h1> hello world </h1> </body> </html>Copy the code

The IDEA of integrated tomcat

Add our tomcat installation to our IDEA:

Go to the Tomcat configuration page:

Configure tomcat:

Go to the installation directory:

Click OK,

Then go to Deployment:

Add the servlet project we created:

Automatically adds our project:

Then click Apply, and then OK.

IDEA integrates Tomcat and connects our projects. Let’s run it:

Start tomcat

Click the green triangle:

Prove that our project has been successfully started in Tomcat.

Visit our servlet

At this point, we are ready to access our servlet.

Visit: http://localhost:8080/ and the page displays the contents of the index.jsp we modified earlier.

Access our own Servlet again:

http://localhost:8080/demo
Copy the code

We’ve successfully exported our content. That’s all for this article, a simple introduction to Tomcat.

conclusion

What tomcat is, how to install tomcat, how to start Tomcat, how to deploy our own Servlet project, how we integrate Tomcat in IDEA and how to start Tomcat.

People will not lose themselves so long as they do not lose their direction