一、对应关系(Tomcat和Java对应关系)
链接
https://tomcat.apache.org/whichversion.html
二、MacBook的Java安装
MacBook版本
macOS Monterey 12.7.5
链接
https://www.oracle.com/java/technologies/downloads/#java8-mac
window
请参考链接
选择
jdk-8u421-macosx-x64.dmg,下载完成后一路next
卸载
https://www.java.com/zh-CN/uninstall/uninstall_instructions.html
window
请点击链接进行参考
MacBook卸载Java8
sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
sudo rm -fr ~/Library/Application\ Support/Oracle/Java
sduo rm -r ~/"Library/Application Support/Oracle/Java"
sudo rm -rf /Library/Java
三、Apache Tomcat安装
下载
https://tomcat.apache.org/download-90.cgi
选择下载tar.gz (pgp, sha512)
解压到某个路径下
例如
/Users/mact/Downloads/Env/Java/apache-tomcat-9.0.91
sudo chmod 755 /Users/mact/Downloads/___Env___/Java/apache-tomcat-9.0.91/bin/*.sh
cd /Users/mact/Downloads/___Env___/Java/apache-tomcat-9.0.91/bin/
./startup.sh
访问
浏览器输入 http://localhost:8080 进入
关闭
./shutdown.sh
四、IDEA 配置
- 新建Web项目
pom.xml
设置阿里源
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>demo1</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.9.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>4.0.4</version> <!-- 选择一个与 Java 8 兼容的版本 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>aliyun-public</id>
<name>Aliyun Public</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
<repository>
<id>aliyun-spring</id>
<name>Aliyun Spring</name>
<url>https://maven.aliyun.com/repository/spring</url>
</repository>
<repository>
<id>aliyun-center</id>
<name>Aliyun Central</name>
<url>https://maven.aliyun.com/repository/central</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-public</id>
<url>https://maven.aliyun.com/repository/public</url>
</pluginRepository>
</pluginRepositories>
</project>
HelloServlet
package org.example.demo1;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}