简单servlet第三篇---使用servlet读取配置文件的内容
最近在做一个公司的网站,前台js有一些动态配置的参数,就想着使用配置文件的形式来读取,用前台javascript应该也能做到,但是想练练java的技术,所以下面使用java来实现读取配置文件的内容1.首先写一个servlet , 因为我用的容器是tomcat, 所以想用servlet来做应该合适。 2.将读取配置文件的代码写到该servlet 的 init() 方法中,然后将该se
最近在做一个公司的网站,前台js有一些动态配置的参数,就想着使用配置文件的形式来读取,用前台javascript应该也能做到,但是想练练java的技术,所以下面使用java来实现读取配置文件的内容
1.首先写一个servlet , 因为我用的容器是tomcat, 所以想用servlet来做应该合适。
2.将读取配置文件的代码写到该servlet 的 init() 方法中,然后将该servlet设置为 自启动。
web.xml 配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="qaservice" version="3.0">
<display-name>edu_5ixl</display-name>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.xljy.servlet.InitServlet</servlet-class>
<init-param>
<param-name>configFile</param-name>
<param-value>WEB-INF/java-chobits.xml</param-value>
</init-param>
<init-param>
<param-name>displayMode</param-name>
<param-value>0</param-value><!-- 0在登陆窗口选模式,1直接运营开放模式 -->
</init-param>
<init-param>
<param-name>appName</param-name>
<param-value>北京心聆教育科技有限公司</param-value>
</init-param>
<init-param>
<param-name>appVersion</param-name>
<param-value>V1.0</param-value>
</init-param>
<init-param>
<param-name>appCopyrightNotice</param-name>
<param-value>版权所有@2016年05月30日 创阳科技</param-value>
</init-param>
<init-param>
<param-name>loginTimeout</param-name>
<param-value>0</param-value><!-- 分钟 ,设置为0表示永不超时-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/InitServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
其中加了参数:load-on-startup 并把参数设为1 表示容器启动的时候该servlet就要加载,也就是该servlet的init()方法就要执行
3.然后就是servlet 该怎么写了
思路是:
3.1我要将配置文件放到我的web项目的WEB-INF下,所以首先第一件事就是要找到 WEB-INF的路径,如下:
//获取项目WEB-INF的路径
webInfPath = this.getServletContext().getRealPath("WEB-INF");
if(webInfPath==null){
String tempPath = this.getServletContext().getResource("/").getPath();
webInfPath = tempPath + File.separator + "WEB-INF";
}
System.out.println("webInfPath:"+webInfPath);
File webInfFile = new File(webInfPath);
webRootPath = webInfFile.getParentFile().getAbsolutePath();
tomcatPath = webInfFile.getParentFile().getParentFile().getParentFile().getAbsolutePath();
System.out.println("webRootPath:"+webRootPath);
System.out.println("tomcatPath:"+tomcatPath);
使用servlet自带的getServletContext.getRealPath() 方法可以获取 所需要的路径,
3.2 找到 WEB-INF 的路径之后,还要找到具体是哪个文件名,因为这个文件名存放到了web.xml 中,我们可以使用 servlet自带的方法this.getInitParameter("configFile"); 来获取 , 如下:
//获取web.xml中的参数
configFile = this.getInitParameter("configFile");
displayMode = this.getInitParameter("displayMode");
appName = this.getInitParameter("appName");
appVersion = this.getInitParameter("appVersion");
System.out.println("configFile:"+configFile);
3.3获取到文件名之后,就可以使用java的xml 工具解析配置文件了,这里我用的是dom4j的包封装的一些操作xml的方法,就不做赘述了,直接上代码.
//读取configFile 文件中的内容
if(configFile==null || configFile.isEmpty()){
throw new Exception("web.xml中的RemoteService节点没有配置configFile参数");
}
File config = new File(webRootPath+ File.separator + configFile);
Element root = XMLBuilder.parseFile(config);
System.out.println("root");
System.out.println(root.asXML());
String resourceType = root.elementTextTrim("resource-type");
System.out.println("resourceType:"+resourceType);
对了 ,没有把配置文件放上来,再把配置文件放上来
<?xml version="1.0" encoding="UTF-8"?>
<remote-service>
<http-charset>
<request>UTF-8</request>
<response>UTF-8</response>
</http-charset>
<resource-type>0</resource-type>
<db-config useProxy="0">
<proxy-connection>
<proxyIP>192.168.2.210</proxyIP>
<proxyPort>15000</proxyPort>
<readPackageTimeout>10</readPackageTimeout>
<requestCharset>UTF-8</requestCharset>
<responseCharset>UTF-8</responseCharset>
</proxy-connection>
<driver>com.mysql.jdbc.Driver</driver>
<url><![CDATA[jdbc:mysql://127。0.0.1:3306/erm_db?useUnicode=true&characterEncoding=utf8]]></url>
<user>erm</user>
<password>11111111</password>
<supported-sequence>0</supported-sequence>
</db-config>
<globle-properties>
<property name="mediaWebURL">http://127.0.0.1:8080/media</property>
<property name="weixinEbale">false</property>
<property name="ossWebUrl">http://5ixledustore.oss-cn-beijing.aliyuncs.com</property>
<!-- <property name="ermRestApiUrl">http://www.ltech.com/erm/rest-api</property> -->
<property name="ermRestApiUrl">http://127.0.0.1:8080/erm/rest-api</property>
<property name="ermDomainUrl">http://127.0.0.1:8080</property>
</globle-properties>
<log4j-file>WEB-INF/log4j.properties</log4j-file>
<filter-class></filter-class>
<thread-list>
</thread-list>
<action-list>
</action-list>
</remote-service>
3.4对了,如果想要获取上述配置文件中globe-properties 节点的 参数怎么办,下面用 这两个方法
3.4.1 先定义这个一个变量:
//配置文件中的全局参数
private static Properties globeProperties = new Properties();
3.4.2 生成Properties 参数
private void createGlobeProperties(Element globeElem){
if(globeElem==null){
return;
}
Iterator<Element> it = globeElem.elementIterator("property");
while(it.hasNext()){
Element elem = it.next();
String name = elem.attributeValue("name");
String value = elem.getTextTrim();
System.out.println("name:"+name+", value:"+value);
globeProperties.setProperty(name, value);
}
}
这其中用到了遍历的类Iterator , 需要注意
3.4.3 get方法
public static Properties getProperties(){
return globeProperties;
}
怎么用呢? 对了 忘了把 声明变量的部分拿上来:
private static String webInfPath = "";
private static String webRootPath = "";
private static String tomcatPath = "";
private String configFile = "";
private String displayMode = "";
private String appName = "";
private String appVersion = "";
//配置文件中的全局参数
private static Properties globeProperties = new Properties();
看上面这些参数都是static 静态的, 用的时候在jsp中直接使用类名 InitServlet 调用即可 ,但是具体怎么用还没有写 ,明天再总结
下面把InitServlet的所有代码粘上来:
package com.xljy.servlet;
import java.io.File;
import java.util.Iterator;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.dom4j.Element;
import com.chobits.common.XMLBuilder;
public class InitServlet extends HttpServlet {
private static String webInfPath = "";
private static String webRootPath = "";
private static String tomcatPath = "";
private String configFile = "";
private String displayMode = "";
private String appName = "";
private String appVersion = "";
//配置文件中的全局参数
private static Properties globeProperties = new Properties();
@Override
public void init() throws ServletException {
System.out.println("进入InitServlet1");
try {
//获取项目WEB-INF的路径
webInfPath = this.getServletContext().getRealPath("WEB-INF");
if(webInfPath==null){
String tempPath = this.getServletContext().getResource("/").getPath();
webInfPath = tempPath + File.separator + "WEB-INF";
}
System.out.println("webInfPath:"+webInfPath);
File webInfFile = new File(webInfPath);
webRootPath = webInfFile.getParentFile().getAbsolutePath();
tomcatPath = webInfFile.getParentFile().getParentFile().getParentFile().getAbsolutePath();
System.out.println("webRootPath:"+webRootPath);
System.out.println("tomcatPath:"+tomcatPath);
//获取web.xml中的参数
configFile = this.getInitParameter("configFile");
displayMode = this.getInitParameter("displayMode");
appName = this.getInitParameter("appName");
appVersion = this.getInitParameter("appVersion");
System.out.println("configFile:"+configFile);
//读取configFile 文件中的内容
if(configFile==null || configFile.isEmpty()){
throw new Exception("web.xml中的RemoteService节点没有配置configFile参数");
}
File config = new File(webRootPath+ File.separator + configFile);
Element root = XMLBuilder.parseFile(config);
System.out.println("root");
System.out.println(root.asXML());
String resourceType = root.elementTextTrim("resource-type");
System.out.println("resourceType:"+resourceType);
//初始化globeProperties
this.createGlobeProperties(root.element("globle-properties"));
} catch (Exception e) {
// TODO: handle exception
}
}
private void createGlobeProperties(Element globeElem){
if(globeElem==null){
return;
}
Iterator<Element> it = globeElem.elementIterator("property");
while(it.hasNext()){
Element elem = it.next();
String name = elem.attributeValue("name");
String value = elem.getTextTrim();
System.out.println("name:"+name+", value:"+value);
globeProperties.setProperty(name, value);
}
}
public static Properties getProperties(){
return globeProperties;
}
}
不积跬步,无以至千里
不积小流,无以成江海
更多推荐
所有评论(0)