1 下载地址: https://tomcat.apache.org/download-90.cgi
2 下载内容
3 下载后解压到/Library目录下
4 修改目录权限
sudo chmod 755 *.sh
1 cd到tomcat安装目录下启动tomcat
cd /Library/apache-tomcat-9.0.68/bin
sudo sh ./startup.sh
2 启动后,在浏览器中输入localhost:8080即可访问到tomcat
sudo sh ./shutdown.sh
可以在conf/server.xml中修改端口号
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
将html文件拷贝到webapps目录下,然后访问http://localhost:8080/test.html,将会访问到部署到webapps中的内容
选择next,finish,最后pom.xml中的冗余配置,最终配置如下
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>Tomcat-TestartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
project>
http://localhost:8080/tomcat-demo
1 引入Servlet依赖
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
dependencies>
2 添加实现Servelet接口的子类
@WebServlet("/demo1")
public class ServletDemo implements Servlet {
public void init(ServletConfig servletConfig) throws ServletException {
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("德玛西亚必胜!");
}
public String getServletInfo() {
return null;
}
public void destroy() {
}
}
3 启动tomcat,然后访问资源
http://localhost:8080/tomcat-demo/demo1
可以看到控制台输出了在service()方法中打印的日志
1 在init(),service(), destroy()方法中添加日志
@WebServlet("/demo1")
public class ServletDemo implements Servlet {
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init()");
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service()");
}
public String getServletInfo() {
return null;
}
public void destroy() {
System.out.println("destroy()");
}
}
2 在ternimal中运行命令
mvn tomcat7:run
遇到如下报错
[ERROR] No plugin found for prefix 'tomcat7' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/yl/.m2/repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
这是因为当前的Terminal目录没有切换到真正的工程目录下
切换到工程目录下后,访问资源地址
http://localhost:8080/demo1
此时,控制台输出日志
init()
service()
按control + c退出后,输出日志
destroy()
1 doGet请求
@WebServlet("/demo2")
public class Servlet2Demo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost");
}
}
此时访问
http://localhost:8080/tomcat-demo/demo2
将会输出日志doGet
2 doPost请求
创建html文件
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/tomcat-demo/demo2" method="post">
<input name="username"/>
<input type="submit"/>
form>
body>
html>
访问html资源
http://localhost:8080/tomcat-demo/test.html
将会看到doPost()日志
1 配置多个urlPattern
@WebServlet(urlPatterns = {"/demo3", "/demo4"})
public class Servlet7 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
此时访问
http://localhost:8080/tomcat-demo/demo3
http://localhost:8080/tomcat-demo/demo4
都能匹配到
2 精确匹配
@WebServlet(urlPatterns = "/api/select")
对应的url
http://localhost:8080/tomcat-demo/api/select
3 目录匹配
@WebServlet(urlPatterns = "/api/*")
对应的url,api后可以填任意值
http://localhost:8080/tomcat-demo/api/any
4 任意匹配
@WebServlet(urlPatterns = "/*")
访问的url可以填任意内容
http://localhost:8080/tomcat-demo/test
servlet
public class Servlet3Demo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet3Demo#doGet()");
}
}
web.xml配置
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 Applicationdisplay-name>
<servlet>
<servlet-name>demo3servlet-name>
<servlet-class>com.test.tomcat.Servlet3Demoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>demo3servlet-name>
<url-pattern>/demo3url-pattern>
servlet-mapping>
web-app>
访问此接口的url
http://localhost:8080/tomcat-demo/demo3