2011-09-01 49 views
1

我想從JavaScript傳遞值到Java(小程序),但每次它出來是空的。java到javascript通信

我的java類:

package com.vaannila.utility; 


import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 

import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

import prefuse.util.ui.JPrefuseApplet; 

public class dynamicTreeApplet extends JPrefuseApplet { 

    private static final long serialVersionUID = 1L; 
    public static int i = 1; 
    public String dieasenameencode; 
// System.out.println("asjdjkhcd"+dieasenameencode); 
    public void init() { 
     System.out.println("asjdjkhcd"+dieasenameencode); 
     System.out.println("the value of i is " + i); 
     URL url = null; 
//  String ashu=this.getParameter("dieasenmae"); 
//  System.out.println("the value of the dieases is "+ashu); 
     //Here dieasesname is important to make the page refresh happen 

     //String dencode = dieasenameencode.trim(); 
     try { 
      //String dieasename = URLEncoder.encode(dencode, "UTF-8"); 
      // i want this piece of the code to be called 
      url = new URL("http://localhost:8080/docRuleToolProtocol/appletRefreshAction.do?dieasename="+dieasenameencode); 
      URLConnection con = url.openConnection(); 
      con.setDoOutput(true); 
      con.setDoInput(true); 
      con.setUseCaches(false); 
      InputStream ois = con.getInputStream(); 
      this.setContentPane(dynamicView.demo(ois, "name")); 
      ois.close(); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (FileNotFoundException f) { 
      f.printStackTrace(); 

     } catch (IOException io) { 
      io.printStackTrace(); 
     } 
     ++i; 
    } 

} 

我的javascript:

function showSelected(value){ 
alert("the value given from"+value); 
var diseasename=value; 
alert(diseasename); 
document.decisiontree.dieasenameencode="diseasename"; 
} 

回答

0

我不認爲你可以直接訪問小程序變量,但你絕對可以調用applet的方法。嘗試爲變量創建一個setter並從JavaScript調用它。