在使用J2EE编写Web应用程序时,经常把参数设置在一个XML文件中,在Web应用程序启动时来读取。下面就是一个实际应用中的例子。
XML文件存放在WEB-INF目录下,文件名是SysConfig.xml,其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<DocRoot>
<SysConfig>
<BaseFolder>D:/DocLib</BaseFolder>
<TempUploadFolder>D:/DocLib/temp</TempUploadFolder>
<DocFolder>D:/DocLib/docs</DocFolder>
<IndexFolder>D:/DocLib/index</IndexFolder>
<LiusConfig>D:/DocLib/Config/liusConfig.xml</LiusConfig>
<Log4jConfig>D:/DocLib/Config/log4j/log4j.properties</Log4jConfig>
<Database>D:/DocLib/database/database.mdb</Database>
</SysConfig>
</DocRoot>
为了在Web应用程序启动时读取这些配置值,编写了一个Servlet如下
package bootstrap;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import util.XMLHelper;
import util.XMLHelperException;
/**
* LoadConfig is a sevlet which load config properties from XML files named SysConfig.xml
* when the web application start.
* All these properties are stored in servlet context. The key is the same as the node name
* in XML file.
* @author jimshen
*
*/
public class LoadConfig extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
String configFile = getServletContext().getRealPath("/")
+ "WEB-INF\\SysConfig.xml";
Document doc = null;
ServletContext application = getServletContext();
try {
doc = XMLHelper.parseXMLFromFile(configFile);
Element root = doc.getDocumentElement();
NodeList nodes = root.getElementsByTagName("SysConfig");
NodeList children = nodes.item(0).getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node.getNodeType() != Node.ELEMENT_NODE)
continue;
String key=node.getNodeName();
String = node.getFirstChild().getNode().trim();
application.setAttribute(key,);
}
} catch (XMLHelperException e) {
throw new ServletException(e.toString());
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("This Servlet does not service requests!");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("This Servlet does not service requests!");
}
}
web.xml文件中将servlet配置为启动时加载
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<deion>Load Configuration Information From SysConfig.xml</deion>
<display-name>LoadConfig</display-name>
<servlet-name>LoadConfig</servlet-name>
<servlet-class>bootstrap.LoadConfig</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>LoadConfig</servlet-name>
<url-pattern>/servlet/LoadConfig</url-pattern>
</servlet-mapping>
</web-app>
没有评论:
发表评论