欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tomcat部署war包

程序员文章站 2022-06-11 10:53:38
...

我们会把工程打成war包部署到tomcat中,然后由容器来对其进行解压,这个过程是怎么样的呢? 接下来我拉把这个面纱揭开看一下,tomcat源代码如下:[b]1.[/b]URL war = new URL("jar:" + (new File(docBase)).toURI().toURL() + "!/");
// Expand the WAR into the new document base directory
JarURLConnection juc = (JarURLConnection) war.openConnection();
juc.setUseCaches(false);
JarFile jarFile = null;
InputStream input = null;
try {
jarFile = juc.getJarFile();
Enumeration jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
String name = jarEntry.getName();
int last = name.lastIndexOf('/');
if (last >= 0) {
File parent = new File(docBase,
name.substring(0, last));
parent.mkdirs();
}
if (name.endsWith("/")) {
continue;
}
input = jarFile.getInputStream(jarEntry);

// Bugzilla 33636
File expandedFile = expand(input, docBase, name);
long lastModified = jarEntry.getTime();
if ((lastModified != -1) && (lastModified != 0) && (expandedFile != null)) {
expandedFile.setLastModified(lastModified);
}

input.close();
input = null;
}
} catch (IOException e) {
// If something went wrong, delete expanded dir to keep things
// clean
deleteDir(docBase);
throw e;
} finally {
if (input != null) {
try {
input.close();
} catch (Throwable t) {
;
}
input = null;
}
if (jarFile != null) {
try {
jarFile.close();
} catch (Throwable t) {
;
}
jarFile = null;
}
}

[b]2.[/b]熟悉tomcat的兄弟都知道,容器在启动的时候其实执行的是两个过程,第一:初始化,第二:启动过程; 而就是在启动HOST的过程中,触发一个start事件,lifecycle.fireLifecycleEvent(START_EVENT, null);
执行到HostConfig类的lifecycleEvent方法中,然后执行start()方法,deployApps方法 —》deployWARs(appBase, appBase.list()); 然后执行到StandardHost的addChild方法 启动StandardContext类的start方法,init方法, fixDocBase()方法,在这个类里会调用ExpandWar.expand(host, war, contextPath);来进行war包的解压,就到此为止,