Background introduction
Apache Tomcat is an open source implementation of Java Servlet, JavaServer Pages (JSP), Java Expression Language and Java's WebSocket technology. We usually call Tomcat a Web container or Servlet container.
The mapping relationship between tomcat versions and corresponding specifications:
download link
https://tomcat.apache.org/download-90.cgi
Download to local and unzip:
Enter the main directory:
Tomcat catalog 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 functions, some other files are included here.
For example: startup.bat is used to start tomcat under windows, and startup.sh is used in Linux environment. Correspondingly there is a corresponding shutdown script.
conf
Tomcat configuration file and related DTD. The most important file here is server.xml. It is the main configuration file of the container.
catalina.policy
: Tomcat: security policy file, which controls JVM related permissions. For details, please refer to java.security.Permission.
catalina.properties
: Tomcat Catalina behavior control configuration file, such as Common ClassLoader.
logging.properties
: Tomcat log configuration file. The log inside uses JDK Logging.
server.xml
: Tomcat server configuration file (for my developers is very important).
context.xml
: Global context configuration file, monitor and load resource files, and automatically load when the monitored file changes.
tomcat-user.xml
: Tomcat role configuration file.
web.xml
: Servlet standard web.xml deployment file, Tomcat implements partial configuration by default:
org.apache.catalina.servlets.DefaultServlet。
org.apache.jasper.servlet.JspServlet
logs
The log file is located here by default.
localhost
Useful, when your tomcat fails to start, read this file more. such as:
NoClassDefFoundError
ClassNotFoundException
access
The most useless.
catalina.{date}
Mainly console output, all logs are in it.
webapps
This is where your webapp is located. In fact, these are all one project.
Simplify the way of web deployment. Our applications will not be placed here in the online environment. The best way is external.
lib
Tomcat stores shared libraries. such as:
ecj-4.17.jar: eclipse Java compiler
jasper.jar: JSP compiler.
work
Store files compiled during tomcat runtime, such as files compiled by JSP.
temp
Store temporary files generated during runtime.
Start tomcat
Start tomcat
We directly start startup.bat in the bin directory under windows, which corresponds to startup.sh in the Linux environment.
Double-click to start. The console will input port 8080, and then we visit:
http://localhost:8080/
Page display:
This means that our tomcat has started successfully.
At this time, the ROOT directory is requested.http://localhost:8080/
For example: we canhttp://localhost:8080/manager
Servlet project deployed to tomcat
Create web project
Use maven to create a web project. Since tomcat is a Servlet container, create a Servlet class in the project, mark it as a war package, and copy it to tomcat for deployment.
The project structure is as follows:
Add dependency
<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 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>
<build>
<finalName>my-web-maven</finalName>
</build>
</project>
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);
}
}
web.xml has nothing, just for 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>
<display-name>Archetype Created Web Application</display-name>
</web-app>
There is nothing in index.jsp to find that:
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
Use the mvn command to make a war package.
Copy the marked war package (in fact, copy the my-web-maven folder is the same) to the webapps directory in tomcat:
Then go to the bin directory and double-click
After the project is running, visit http://localhost:8080/
Prove that our project has been launched successfully.
Next we visit the Servlet we wrote:
http://localhost:8080/demo?message=hello
Error, HTTP status 404-not found;
Note : When visiting here, we need to use the project name as the contextPath, that is, the access method should be:
http://localhost:8080/my-web-maven/demo?message=hello
Output on the page
hello
Easily get it done, so that our project can be successfully deployed to tomcat.
Deploy the project in IDEA to tomcat
Create a servlet project with the project name my-servlet.
Create a new class MyServlet
Enter the tomcat directory we just installed, enter the lib directory, and select servlet-api.jar.
Click ok.
Click Apply, and then click OK.
Modify the content of MyServlet:
package com.tian.servlet;
import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
//To implement the interface Servlet, the following methods must be rewritten
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();
//网页响应类型,浏览器将其渲染为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() {
}
}
Modify the content of the web.xml file:
<?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>
In addition, we modify the content of index.jsp, mainly for better demonstration:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h1> hello world </h1> </body> </html>
IDEA integrated tomcat
Add the tomcat we installed in our IDEA:
Come to the tomcat configuration interface:
Configure tomcat:
Enter the installation directory:
Click OK,
Then enter the deployment column:
Add the servlet project we created:
Our project is automatically added:
Then click Apply, and then click OK.
IDEA integrates tomcat and associates our projects. Let's run it below:
Start tomcat
Click the green triangle:
Prove that our project has been successfully started in tomcat.
Visit our servlet
At this point, we can come to visit our servlet.
Visit: http://localhost:8080/ The page display is the content of index.jsp we modified earlier.
Then visit our own Servlet:
http://localhost:8080/demo
We successfully output our content. Well, that's all in this article, simple tomcat entry.
to sum up
What is tomcat, how to install tomcat, how to start tomcat, how to deploy our own Servlet project, how to integrate tomcat in IDEA and how to start tomcat.
「人只要不失去方向,就不会失去自己
」