JVM和TOMCAT的理解
一直没有想过这个问题,究竟TOMCAT是什么,算不算JVM?
首先JVM肯定都能明白,相当于我们的程序运行的容器,负责加载运行class文件。Tomcat呢? 也是一个容器,不同于JVM的来说,它是一个web容器即servlet的容器。那么为什么需要两个容器呢?
如果两方都是纯JAVA应用,没有web的需求,那么之间可以通过JVM的方式进行调用。
如果是web应用呢,打交道就从Java与Java之间,变成了浏览器与Java应用。HotSpot的JVM是没有提供这个功能的,所以就由tomcat来进行url的识别与处理。
在wiki上的定义:
A web container (also known as a servlet container;[1] and compare "webtainer"[2]) is
the component of a web server that interacts with Java servlets. A web container is
responsible for managing the lifecycle of servlets, mapping a URL to a particular
servlet and ensuring that the URL requester has the correct access-rights.
A web container handles requests to servlets, JavaServer Pages (JSP) files, and other
types of files that include server-side code. The Web container creates servlet
instances, loads and unloads servlets, creates and manages request and response
objects, and performs other servlet-management tasks.
A web container implements the web component contract of the Java EE architecture,
specifying a runtime environment for web components that includes security,
concurrency, lifecycle management, transaction, deployment, and other services.
一个web容器即servlet容器是一个与java servlet打交道的web服务器。一个web容器负责管理servlet的声明周期,映射URL到指定的servlet,确保url请求具有正确的权限。
web容器用来处理请求到servlet和jsp和其他服务端的文件。web容器创建servlet实例,加载和卸载servlet, 创建管理请求和返回对象,还可以执行其他的servlet管理任务。
web容器需要实现Java EE架构规定的架构,指定web组件运行的环境,包括安全、并发、声明周期管理、事务、部署和其他服务。
这里插一句tomcat的load classes的过程
- Bootstrap 加载jvm提供的$JAVA_HOME/jre/lib/ext下面的jar
- System 加载tomcat本身的jar包
- $CATALINA_HOME/bin/bootstrap.jar 使用main方法启动tomcat server
- CATALINABASE/bin/tomcat−juli.jaror CATALINA_HOME/bin/tomcat-juli.jar 加载tomcat默认的日志jar
- $CATALINA_HOME/bin/commons-daemon.jar 加载daemon jar
- Common 加载tomcat通用的jar
- unpacked classes and resources in $CATALINA_BASE/lib
- JAR files in $CATALINA_BASE/lib
- unpacked classes and resources in $CATALINA_HOME/lib
- JAR files in $CATALINA_HOME/lib
- WebappX 为每个webapp分配classloader,加载每个web应用
从这里能看得到出来,tomcat是基于java的一个应用,提供一个中间件管理系统,tomcat是程序,jvm是平台。