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();
}
}
}
