public class RegServiceServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public static Hashtable SYSCON = new Hashtable();
public RegServiceServlet()
{
}
@Override
public void init()
throws ServletException
{
try
{
regSystemParam();
//regFunction();
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest httpservletrequest1, HttpServletResponse httpservletresponse1)
throws IOException
{
}
@Override
public void doGet(HttpServletRequest httpservletrequest1, HttpServletResponse httpservletresponse1)
throws IOException
{
}
public void regSystemParam()
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
URL url = this.getClass().getClassLoader().getResource(
"log4j.properties");
String path = url.getPath();
path = path.substring(1,path.lastIndexOf("/"));
File tempfile = new File(path+"/SysParameter.xml");
Document document = builder.parse(tempfile);
Element element = document.getDocumentElement();
NodeList nodelist = element.getElementsByTagName("Parameter");
if(nodelist.getLength() == 0)
return;
NodeList subNodeList = null;
Node cnode = null;
Node chieldnode = null;
String temp[] = new String[2];
for(int i = 0; i < nodelist.getLength(); i++)
{
subNodeList = nodelist.item(i).getChildNodes();
for(int j = 0; j < subNodeList.getLength(); j++)
{
chieldnode = subNodeList.item(j);
if(chieldnode.getNodeName().equals("ParameterName"))
{
temp[0] = chieldnode.getChildNodes().item(0).getNodeValue();
continue;
}
if(chieldnode.getNodeName().equals("ParameterValue")){
temp[1] = chieldnode.getChildNodes().item(0).getNodeValue();
}
}
SYSCON.put(temp[0], temp[1]);
temp = new String[2];
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
{
private static final long serialVersionUID = 1L;
public static Hashtable SYSCON = new Hashtable();
public RegServiceServlet()
{
}
@Override
public void init()
throws ServletException
{
try
{
regSystemParam();
//regFunction();
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest httpservletrequest1, HttpServletResponse httpservletresponse1)
throws IOException
{
}
@Override
public void doGet(HttpServletRequest httpservletrequest1, HttpServletResponse httpservletresponse1)
throws IOException
{
}
public void regSystemParam()
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
URL url = this.getClass().getClassLoader().getResource(
"log4j.properties");
String path = url.getPath();
path = path.substring(1,path.lastIndexOf("/"));
File tempfile = new File(path+"/SysParameter.xml");
Document document = builder.parse(tempfile);
Element element = document.getDocumentElement();
NodeList nodelist = element.getElementsByTagName("Parameter");
if(nodelist.getLength() == 0)
return;
NodeList subNodeList = null;
Node cnode = null;
Node chieldnode = null;
String temp[] = new String[2];
for(int i = 0; i < nodelist.getLength(); i++)
{
subNodeList = nodelist.item(i).getChildNodes();
for(int j = 0; j < subNodeList.getLength(); j++)
{
chieldnode = subNodeList.item(j);
if(chieldnode.getNodeName().equals("ParameterName"))
{
temp[0] = chieldnode.getChildNodes().item(0).getNodeValue();
continue;
}
if(chieldnode.getNodeName().equals("ParameterValue")){
temp[1] = chieldnode.getChildNodes().item(0).getNodeValue();
}
}
SYSCON.put(temp[0], temp[1]);
temp = new String[2];
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
读取方式:如String dirWord = (String)RegServiceServlet.SYSCON.get("xml中配置名字");