欢迎光临
我们一直在努力

用Java实现可保存状态的数据库生成XML树,源码来了(10)-JSP教程,Java与XML

建站超值云服务器,限时71元/月

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用Java实现可保存状态的数据库生成XML树,源码来了(10)-JSP教程,Java与XML
分享到: 更多 (0)

相关推荐

  • 暂无文章