xml|生成xml|数据|数据库2.XMLServlet.java
package com.sitechasia;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
import javax.sql.RowSet;
import com.sitechasia.ejb.*;
import javax.ejb.Handle;
public class XMLServlet extends HttpServlet {
PrintWriter out;
private static ResultEdit re;
private Object ref;
boolean flag=true;
private Handle handle=null;
private static String HostIP = null;
private static String HostPort = null;
private static final String CONTENT_TYPEX = "text/xml";
private static final String CONTENT_TYPEH = "text/html";
private static final String CONTENT_XSLT = "<?xml-stylesheet type=\"text/xsl\" href=\"/xsl/treefunc.xsl\"?>";
private static final String CONTENT_XSLC = "<?xml-stylesheet type=\"text/xsl\" href=\"xmltreexsl.xsl\"?>";
/**Initialize global variables*/
public void init() throws ServletException {
}
/**Process the HTTP Get request*/
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
HttpSession session=request.getSession();
if ( session.getAttribute("ResultEditHandle")==null) {
flag=false;
}
else {
flag=true;
handle=(Handle)session.getAttribute("ResultEditHandle");
re =(ResultEdit)handle.getEJBObject();
}
if (flag){
RowSet rs = re.getRowSet();
response.setContentType(CONTENT_TYPEX);
out = response.getWriter();
out.println("<?xml version=\"1.0\" encoding=\"gb2312\" ?>");
if (request.getParameterValues("clickId")==null){
out.println(CONTENT_XSLT);
}
else {
out.println(CONTENT_XSLC);
}
out.println("<project>");
while(rs.next()){
if (rs.getString(2).equals("0")){
if(rs.getString("herf")==null){
out.println("<node>");
out.println("<id>"+rs.getString(1)+"</id>");
out.println("<layer>"+rs.getString(2)+"</layer>");
out.println("<name>"+rs.getString(3)+"</name>");
out.println("<value>"+(rs.getString(4))+"</value>");
out.println("<father>"+rs.getString(5)+"</father>");
out.println("<href></href>");
out.println("<target></target>");
childnode(rs.getString(1));
out.println("</node>");
}
else {
out.println("<node>");
out.println("<id>"+rs.getString(1)+"</id>");
out.println("<layer>"+rs.getString(2)+"</layer>");
out.println("<name>"+rs.getString(3)+"</name>");
out.println("<value>"+(rs.getString(4))+"</value>");
out.println("<father>"+rs.getString(5)+"</father>");
out.println("<href>"+rs.getString(6)+"</href>");
out.println("<target>"+rs.getString(7)+"</target>");
out.println("</node>");
}
}
}
out.println("</project>");
rs.close();
}
else {
response.setContentType(CONTENT_TYPEH);
out = response.getWriter();
out.println("<html><head><title>no EJB message</title><META HTTP-EQUIV='Refresh' CONTENT='2;url=/RefreshPost.html'/></head>");
out.println("<body>");
out.println("<p><p><p><center>Refresh...... return RefreshPost.html</center></p></p></p></body></html>");
}
}
catch(Exception e){
System.out.println("errror:"+e.getMessage());
e.printStackTrace();
}
}
/**Clean up resources
id 1
layer 2
name 3
value 4
father 5
herf 6
target 7
*/
public void destroy() {
}
public void childnode(String father){
try{
RowSet rs = re.getRowSet();
while(rs.next()){
if (rs.getString(5).equals(father)){
if(rs.getString("herf")==null){
out.println("<node>");
out.println("<id>"+rs.getString(1)+"</id>");
out.println("<layer>"+rs.getString(2)+"</layer>");
out.println("<name>"+rs.getString(3)+"</name>");
out.println("<value>"+(rs.getString(4))+"</value>");
out.println("<father>"+rs.getString(5)+"</father>");
out.println("<href></href>");
out.println("<target></target>");
childnode(rs.getString(1));
out.println("</node>");
}
else{
out.println("<node>");
out.println("<id>"+rs.getString(1)+"</id>");
out.println("<layer>"+rs.getString(2)+"</layer>");
out.println("<name>"+rs.getString(3)+"</name>");
out.println("<value>"+(rs.getString(4))+"</value>");
out.println("<father>"+rs.getString(5)+"</father>");
out.println("<href>"+rs.getString(6)+"</href>");
out.println("<target>"+rs.getString(7)+"</target>");
out.println("</node>");
}
}
}
rs.close();
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
用Java实现可保存状态的数据库生成XML树,源码来了(10)
80酷酷网 80kuku.com