CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

请问:如何通过Applet浏览服务器端的文件和目录?

楼主robber(海盗)2000-10-11 15:00:00 在 Java / J2SE / 基础类 提问

不通过服务端的程序发送给Applet。 问题点数:120、回复次数:2Top

1 楼skyyoung(路人甲)回复于 2000-10-11 17:40:00 得分 120

Directory   listing   on   the   Web   server   in   a   Java   Applet  
   
  There   is   no   way   for   an   Applet   to   retrieve   a   directory   contents   without   the   help   of   server   side   process(servlet/CGI).   But   here   a   way   to   do   it   with   some   help   from   Javascript.    
  In   a   browser,   loading   a   URL   with   no   file   specified   will   return   a   directory   listing   under   the   following   conditions   :    
   
  The   directory   doesn't   contain   a   default   page   like   index.html,   default.htm   or   default.html.    
  The   web   server   allows   directory   exploration.    
  The   listing   is   actually   a   real   HTML   page   build   on   the   fly.   Our   applet   will   extract   the   links   in   this   page   and   present   them   in   a   List.   Links   in   a   page   are   listed   in   the   document   property   called   links.   This   property   can   be   easily   transform   into   a   String   array   by   a   Javascript   function.   Then   the   array   is   passed   to   a   Java   method.   The   Applet   is   very   simple   and   can   be   customized   to   display   more   useful   descriptions   or   filter   some   entries.    
   
  We   have   3   frames,   2   visibles   and   1   invisible.   The   invisible   one   will   contains   the   directory   listing.   Frame   visible   #1   is   for   the   Applet,   by   doubleclicking   on   a   line   in   the   List,   the   corresponding   images   will   be   displayed   in   frame   visble   #2.    
   
  Frames   definitions  
  Note   that   the   2   visibles   frames   are   initially   loaded   with   a   "blank.html"   to   allow   the   third   frame   (the   invisible   one)   to   be   loaded   with   the   directory   content   (here   "../images").    
   
  browse.html    
   
  <HTML><HEAD>  
  <SCRIPT>  
  function   reload_master()   {  
          window.master.location.href   =   "./selector.html";  
  }  
  </SCRIPT></HEAD>  
  <FRAMESET   ROWS="35%,65%,*"   onLoad="reload_master()">  
  <FRAME   SRC="blank.html"   NAME="master">  
  <FRAME   SRC="blank.html"   NAME="detail">  
  <FRAME   SRC="../images"   NAME="contents"   NORESIZE>  
  </FRAMESET>  
  </HTML>  
     
   
   
   
  blank.html    
   
  <HTML<<HEAD><TITLE&glt;  
  </TITLE></HEAD><BODY>  
  </BODY></HTML>  
   
     
   
   
   
  When   all   pages   are   loaded,   the   selector.html   page   is   loaded   into   the   first   visible   frame.   That   page   contains   the   Applet.   During   layout   time,   Javascript   extracts   links   to   the   an   Array.   Via   the   BODY   onLoad   event   handler,   we   trigger   a   Javascript   function   to   transfer   the   Array   to   the   Applet.    
   
  selector.html    
   
  <HTML><HEAD>  
  <SCRIPT>  
  var   LinksLength   =    
          parent.contents.window.document.links.length  
  var   AllTheLinksAsArray   =   new   Array()  
  //   start   at   the   second   link   because    
  //   we   dont   want   the   root   directory  
  for   (var   i   =   1;   i   <   LinksLength   ;   i++)   {  
          s   =   parent.contents.window.document.links[i];  
          AllTheLinksAsArray[i]   =   s;  
          }  
   
  function   putLinksIntoApplet()   {  
          AllTheLinksAsString   =   AllTheLinksAsArray.join("|");  
          document.Selector.insertData(AllTheLinksAsString);  
          }  
  </SCRIPT></HEAD>  
  <BODY   onLoad   ="putLinksIntoApplet()">  
  <P   ALIGN="center">Doubleclick   to   view    
  <APPLET    
        CODE=Selector.class    
        HEIGHT=100    
        WIDTH=400  
        NAME=Selector>  
  <PARAM   NAME="targetFrame"   VALUE="detail">    
  </APPLET>  
  </BODY>  
  </HTML>  
     
   
   
   
  Selector.java    
   
  import   java.awt.*;  
  import   java.awt.event.*;  
  import   java.applet.*;  
  import   java.util.*;  
  import   java.net.*;  
     
    public   class   Selector   extends   Applet    
          implements   ActionListener   {  
        java.awt.List   l;  
        boolean   okToDisplay   =   false;  
        String   targetFrame   =   "";  
         
        public   void   init()   {  
            setLayout(new   BorderLayout());  
            add(l   =   new   java.awt.List(5),   "Center");  
            l.addActionListener(this);  
            targetFrame   =   getParameter("targetFrame");  
            }  
   
        public   void   actionPerformed(ActionEvent   ae)   {  
            if   (okToDisplay)   {  
                  try   {  
                      URL   urlToDisplay   =   new   URL(ae.getActionCommand());  
                      getAppletContext().showDocument(urlToDisplay,   targetFrame);  
                      }  
                  catch   (Exception   e)   {   e.printStackTrace();   }  
                  }  
            }  
     
        public   void   insertData(String   arrayAsAString)   {  
        int   i   =   0;  
        String   s;  
        StringTokenizer   st   =    
              new   StringTokenizer(arrayAsAString,   "|");  
     
        while(st.hasMoreTokens()){  
            s   =   st.nextToken();  
            l.add(s);   //   or   l.addItem(s);  
              System.out.println(s);  
            }  
        okToDisplay     =   true;  
        }  
    }  
   
     
   
  Top

2 楼robber(海盗)回复于 2000-10-12 09:03:00 得分 0

skyyoung的回答非常精彩Top

相关问题

  • asp客户端能否实现对服务器目录浏览功能
  • 浏览目录
  • 目录浏览
  • 浏览器与服务器的通讯。
  • 在applet中使用别的开发包,是用的浏览器所在本机的还是服务器的?
  • 把IIS下的HTTP虚拟目录指向服务器的一个文件夹,把属性设为“浏览”,网上浏览虚拟目录,即可列出所有文件,请问IIS是调用了哪个ASP网页
  • 如何建NETMEETING的目录服务器?
  • 请问一下, 我用applet和服务器socket通信, 是不是不能用IE浏览运行我的applet程序?
  • 我用applet通过rmi访问本机的服务器程序。。为什么用appletviewer能用?用浏览器却不行?
  • Web服务器应用程序浏览的问题

关键词

得分解答快速导航

  • 帖主:robber
  • skyyoung

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo