您的位置:首页 > 运维架构 > Tomcat

有关tomcat的一些小总结(一)

2017-04-24 17:42 411 查看
休假休了半个月,今天第一天回来,代码被改了很多,所有用svn(我们使用svn)check out一份 ,发现有点错误 ,就一点一点追溯,发现项目当中的资源引用了 tomcat 中的一些文件。我这么说你可能不大理解,即就是使用tomcat 中server.xml 来配置一些属性。

         一。下面就来说说tomcat中server.xml  的一些配置用法

Host的节点主要是起一个对域名解析到那个虚拟主机进行配置,其name属性即为访问的域名,默认是127.0.0.1,localhost以及本地Ip,当进行DNS指定之后,可以凭借域名对指定的虚拟主机进行访问。 里面的一些属性就不多讲了,看到单词基本上能明白什么意思。

然后就是context节点,这个节点主要是配置虚拟主机里的访问项目的,默认访问的项目是context里属性path=“”的项目,一个虚拟主机里不能同时设置两个项目为path=“”,如果这样的话,tomcat将启动不了。path主要是指定访问时的项目web名,而docBase即指定的是物理路径名称了。

下面是我本次项目中 host节点的配置:

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">

<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->

<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->

<Context docBase="I:\J2EEWorkSpace\flex\Cloud_Release\Edu_WorkSpace2\apache-tomcat-6.0.43\ims_res" path="/media" reloadable="true"/>
<Context docBase="erm" path="/erm" reloadable="true" source="org.eclipse.jst.jee.server:erm"/></Host>

  主要表现在<Context docBase="I:\J2EEWorkSpace\flex\Cloud_Release\Edu_WorkSpace2\apache-tomcat-6.0.43\ims_res" path="/media" reloadable="true"/>

说到这里 ,我又复习了一下tomcat启动时如何加载 文件的 , 比如说在我们的项目中如何加载java_chobits.xml文件的。

总体思路是这样的:

1.首先web程序 第一次会进入web.xml中,所以使用servlet来初始化一些信息。

2.创建一个 Servlet ,然后再Servlet中的init函数中做一些事情

3.可以使用Servlet容器拿到项目的绝对路径或者WEB-INF的路径 ,进而拿到tomcat的路径

4.拿到tomcat的路径之后 就可以创建一些 ins_res文件夹了 在此文件夹下就可以放一些资源文件(比如图片等等)

下面是代码,仅供参考:

package com.unis.servlet;

import java.io.File;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.dom4j.Element;

import com.chobits.common.FileUtil;
import com.chobits.common.XMLBuilder;

/**
* 本类是模仿 RemoteService 写的 目的是tmcat启动时加载文件 并且创建一些 文档夹
* @author 蒋涛
*
*/

public class InitServlet extends HttpServlet{

@Override
public void init() throws ServletException {
try {
System.out.println("-------------------进入Servlet的inie函数-------------------");
String WEBINFPath = this.getServletContext().getRealPath("WEB-INF");//获得WEB-INF的绝对路径
if(WEBINFPath==null){//如果用上述方式得不到web-inf的路径 就用下面的这种方式获取
String temp = this.getServletContext().getResource("/").getPath();
System.out.println("temp:"+temp);
WEBINFPath = temp+File.separator+"WEB-INF";
System.out.println("WEBINFPath2:"+WEBINFPath);
}
System.out.println("WEBINFPath:"+WEBINFPath);

File webinfFile = new File(WEBINFPath);
String webRootPath = webinfFile.getParentFile().getAbsolutePath();
String serverPath = webinfFile.getParentFile().getParentFile().getParentFile().getAbsolutePath();

String configFile = this.getInitParameter("configFile");
configFile = webRootPath+File.separator+configFile;
System.out.println("configFile:"+configFile);

Element root = XMLBuilder.parseFile(new File(configFile));

int resourceType = Integer.parseInt(root.elementTextTrim("resource-type"));
System.out.println("resourceType:"+resourceType);
if(resourceType==0){
FileUtil fu = new FileUtil();
String imsResPath = serverPath+File.separator+"ims_res";
fu.createDir(imsResPath);
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomcat