㈠ jsp如何读取web.xml里的配置参数
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/j2ee</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>pass</param-name>
<param-value>32l47</param-value>
</context-param>
㈡ 在WEB工程中,jsp文件如何读取XML文件的信息
jsp中读取xml文件中内容的方法,案例如下:
1、XML文件
<?xmlversion="1.0"encoding="GB2312"?>
<!--个人履历表-->
<resume>
<personid="01">
<name>张三</name>
<birthday>03/24/1975</birthday>
<phone>1111-1111</phone>
<address>大连</address>
</person>
<personid="02">
<name>李四</name>
<birthday>9/26/1978</birthday>
<phone>2222-2222</phone>
<address>南京</address>
</person>
<personid="03">
<name>王五</name>
<birthday>11/09/1979</birthday>
<phone>3333-3333</phone>
<address>武汉</address>
</person>
<personid="04">
<name>赵六</name>
<birthday>6/04/1973</birthday>
<phone>4444-4444</phone>
<address>青岛</address>
</person>
<personid="05">
<name>陈七</name>
<birthday>12/19/1977</birthday>
<phone>5555-5555</phone>
<address>上海</address>
</person>
</resume>
2、MyDOMBean.java
packagetest;
importorg.xml.sax.*;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importjava.io.*;
.io.Serializable...{
privatestaticStringxmlStr="";
privatestaticfinalStringPATH="file:///";
publicMyDOMBean()...{
}
publicStringgetString()...{
returnxmlStr;
}
(Stringfilename)throwsException...{
xmlStr="";
DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
//设定解析的叁数
dbf.setIgnoringComments(true);
dbf.(true);
DocumentBuilderdb=dbf.newDocumentBuilder();
//导入XML文件
Documentdoc=db.parse(PATH+filename);
returndoc;
}
publicvoidtraverseTree(Nodenode)throwsException...{
if(node==null)...{
return;
}
inttype=node.getNodeType();
switch(type)...{
//操作DOCUMENT对象节点
caseNode.DOCUMENT_NODE:...{
xmlStr+="<tr>";
traverseTree(((Document)node).getDocumentElement());
break;
}
//操作XML元素节点
caseNode.ELEMENT_NODE:...{
StringelementName=node.getNodeName();
if(elementName.equals("person"))...{
xmlStr+="</tr><tr>";
}
NodeListchildNodes=node.getChildNodes();
if(childNodes!=null)...{
intlength=childNodes.getLength();
for(intloopIndex=0;loopIndex<
length;loopIndex++)
...{
traverseTree(childNodes.item(loopIndex));
}
}
break;
}
//操作XML文本节点
caseNode.TEXT_NODE:...{
Stringdata=node.getNodeValue().trim();
if((data.indexOf("")<0)&&(data.length()>0))...{
xmlStr+="<td>"+data+"</td>";
}
}
}
}
}
3、jsp文件
<html>
<head>
<title>使用DOM解析器</title>
</head>
<%...@pageimport="org.w3c.dom.*"%>
<%...@pagecontentType="text/html;charset=GB2312"%>
<bodybgcolor="#CFF1E1">
<center>
<h2>个人履历表(DOM版)</h2>
<tableborder="1"width="80%">
<tr>
<td>姓名</td>
<td>出生年月</td>
<td>电话号码</td>
<td>居住地</td>
</tr>
<jsp:useBeanid="domparser"class="test.MyDOMBean"/>
<%...
Documentdoc=domparser.getDocument(request.getRealPath("/")+"08_02.xml");
domparser.traverseTree(doc);
out.print(domparser.getString());
%>
</body>
</html>
㈢ 如何java类中如何取得在xml文件查询数据库的结果
你是使用JNDI获得数据源对象的吗?如果是:
1,、配置context.xml文件
<Context>
<Resource name="jdbc/news"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="jbit"
password="dbqn"'
driverClassName="Oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:daataName"
</Context>
以上用的是Oracle数据库的驱动和url
你可以换成SQL的
2、配置web.xml文件
<resource-ref>
<res-auth>Container</res-auth>
<res-type>javax.sql.DataSource</res-type>
<res-ref-name>jdbc/news</res-ref-name>
</resource-ref>
3、添加数据库驱动
4、在javaBean中编写代码,使用lookup()方法获得数据源对象
public class BaseDAO {
private static Connection conn = null;
public static Connection getConnection() {
try {
Context context = new InitialContext();
DataSource ds = (DataSource) context
.lookup("java:comp/env/jdbc/news");
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
注:lookup("java:comp/env/jdbc/news");
中的参数"java:comp/env/"+数据源名称的形式
如果你要的话我正好有这Struts2的项目我可以给你看看