各位高手:
    我想在JSP页面中嵌入一个用Applet做成的树形菜单,当点中树形菜单中某一个叶的时候,树形菜单的右边就链接到相对应的JSP页面或者Servlet,请问这可以怎么实现!(如何在Applet中链接JSP或者Servlet)

解决方案 »

  1.   

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.StringTokenizer;
    import java.net.*;public class Navigator extends Applet implements ActionListener, ItemListener {  String[][] urlArray;   //网址数组
      String[] target;       //打开网页的方式的变量
      Button[] button;       //按钮数组
      List[] list;           //列表框
      Dimension largeButtonSize;
      Component prevList;    //列表框变量
      Object prevSource;     //按钮变量,标识导航条是否打开
      int rows, smallButtonHeight, listHeightSize;
      boolean startSwitch = true;  public void init() {
        String bcString, fcString, fontName, fontSize, fontStyle;
        Color buttonColor, fontColor;
        int token = 0, fontSizeInt, fontStyleInt;    //根据rows定义各个数据的容量
        rows = Integer.parseInt(getParameter("rows"));
        urlArray = new String[rows][];
        target = new String[rows];
        button = new Button[rows];
        list = new List[rows];    //根据参数设置字符的字体,大小与风格
        fontName = getParameter("fontName");
        if (fontName == null) fontName = "SansSerif";
        fontStyle = getParameter("fontStyle");
        if (fontStyle == null) fontStyleInt = 1;
          else fontStyleInt = Integer.parseInt(fontStyle);
        fontSize = getParameter("fontSize");
        if (fontSize == null) fontSizeInt = 12;
          else fontSizeInt = Integer.parseInt(fontSize);    //设置按钮颜色
        bcString = getParameter("buttonColor");
        if (bcString == null) buttonColor = new Color (0);
          else buttonColor = new Color (Integer.parseInt(bcString));
        //设置字体颜色
        fcString = getParameter("fontColor");
        if (fcString == null) fontColor = new Color (16777215);
          else fontColor = new Color (Integer.parseInt(fcString));    //设置Applet的布局
        this.setLayout(new GridLayout(rows, 1));    for (int i = 0; i < rows; i++) {
          //读取Menu变量的数值
          StringTokenizer reader = new StringTokenizer (getParameter(
                                                        "menu" + i),",");
          urlArray[i] = new String[reader.countTokens()];      //创建按钮的代码
          button[i] = new Button (reader.nextToken());
          button[i].setFont(new Font (fontName, fontStyleInt, fontSizeInt));
          button[i].setBackground(buttonColor);
          button[i].setForeground(fontColor);
          this.add(button[i]);
          button[i].addActionListener(this);      //取得网页打开方式的数据
          target[i] = reader.nextToken();      //创建列表框
          list[i] = new List();
          while (reader.hasMoreTokens()) {
            list[i].add(reader.nextToken());
            //i定义哪一个列表框,token表示网址在列表框的位置
            urlArray[i][token++] = reader.nextToken();
          }
          list[i].addItemListener(this);
          token = 0;
        }
      }  //该事件在init()事件之后发生和当浏览器重新获得焦点时发生
      public void start() {
        if (startSwitch) {
          //取得按钮的最大长度
          largeButtonSize = button[0].getSize();
          //取得按钮的最小长度
          smallButtonHeight = (int)(largeButtonSize.height / 2);
          //根据最大,最小长度计算按钮的高度
          listHeightSize = (largeButtonSize.height * rows) -
                           (smallButtonHeight * rows);
          startSwitch = false;
        } else {
          //删除所有按钮
          this.removeAll();
          //向Applet重新加入按钮
          for (int i = 0; i < rows; i++) {
            this.add(button[i]);
          }
          prevSource = null;//将按钮变量定义为空值
        }
      }  //处理按钮的单击事件
      public void actionPerformed(ActionEvent evt) {
        int currentHeightSize = 0;
        boolean oneElementList = false;    Object source = evt.getSource();
        //将前一个列表框设为不可视
        if (prevList != null)
          prevList.setVisible(false);    //确定哪一个按钮被击中
        for (int i = 0; i < rows; i++) {
          //导航条有一个网址的执行代码
          if (source == button[i] && list[i].getItemCount() == 1) {
            //根据最大的长度与高度重新设置按钮的大小
            if (!largeButtonSize.equals(button[0].getSize())) {
              for (int j = 0; j < rows; j++) {
                button[j].setBounds(0, currentHeightSize, largeButtonSize.
                                    width, largeButtonSize.height);
                currentHeightSize += largeButtonSize.height;
              }
            }
            oneElementList = true;
            try {
              //打开网页
              AppletContext context = getAppletContext();
              URL u = new URL(urlArray[i][0]);
              context.showDocument(u, target[i]);
            }
            catch(Exception e) {
              showStatus("Error " + e);
            }
            break;
          }
        }    //导航条多于一个网址的执行代码
        if (!oneElementList) {
          //导航条处于打开状态的执行代码
          if (prevSource == source) {
            for (int i = 0; i < rows; i++) {
              button[i].setBounds(0, currentHeightSize, largeButtonSize.width,
                                  largeButtonSize.height);
              currentHeightSize += largeButtonSize.height;
            }
            prevSource = null;
          }
          //导航条处于关闭状态的执行代码
          else {
            prevSource = source;
            for (int i = 0; i < rows; i++) {
              button[i].setBounds(0, currentHeightSize, largeButtonSize.
                                  width, smallButtonHeight);
              currentHeightSize += smallButtonHeight;
              //显示相关的列表框
              if (source == button[i]) {
                prevList = list[i];
                this.add(list[i]);
                list[i].setBounds(0, currentHeightSize, largeButtonSize.
                                  width, listHeightSize);
                list[i].setVisible(true);
                currentHeightSize += listHeightSize;
              }
            }
          }
        }
      }  //列表框的选择事件
      public void itemStateChanged(ItemEvent evt) {
        List source = (List) evt.getSource();
        int listIndex = source.getSelectedIndex();
        //处理列表框的选择事件
        if (evt.getStateChange() == ItemEvent.SELECTED) {
          for (int i = 0; i < rows; i++) {
            //应用下面语句找到选择项
            if (source == list[i]) {
              try {
                //使用context类的showDocument方法打开联接
                AppletContext context = getAppletContext();
                URL u = new URL(urlArray[i][listIndex]);
                context.showDocument(u, target[i]);
              }
              catch(Exception e) {
                showStatus("Error " + e);
              }
              break;
            }
          }
        }
      }
    }
      

  2.   

    下面是引用该applet的网页文件:
    <applet width="200" height="500" code="Navigator.class">
      <param name="buttonColor" value="952768">
      <param name="fontColor" value="16777215">
      <!--导航1 main指打开新的窗口,其它联接都在该窗口打开-->
      <param name="menu0" value="Java资源网址,main,
              主页, http://java.sun.com,
              JDK下载网址, http://java.sun.com/j2se/1.4/download.html,
              J2EE下载网址, http://java.sun.com/j2ee/download.html">
      <!--导航2 _parent指在当前窗口打开连接-->
      <param name="menu1" value="Borland资源网址,_parent,
              主页, http://www.borland.com,
              Jbuilder下载网址, http://www.borland.com/products/downloads
                               /download_jbuilder.html,">
      <!--导航3-->
      <param name="menu2" value="Mysql资源网址,_parent,
              Mysql官方网站,http://www.mysql.org,
              Mysql数据库下载网址,http://www.mysql.com/downloads/index.html,
              MyOdbc下载网址,http://www.mysql.com/products/myodbc/index.html
              ">
      <!--导航4-->
      <param name="menu3" value="XML资源网址,main,
              XML官方网站,http://www.xml.org,
              XML新闻网站,http://www.xml.org/xml/news_et.shtml">
      <!--导航5-->
      <param name="menu4" value="Oracle资源网址,main,
              Oracle官方网址,http://www.oracle.com,
              Oracle 9i数据库下载网址,
              http://otn.oracle.com/software/content.html">
      <!--定义导航条的总数-->
      <param name="rows" value="5">
    </applet>