2011-01-31 107 views
0

我想从applet嵌入的JSP页面访问Applet的输出流。但它给NullPointerException。我已经给出了下面的代码。Jsp和小程序通信

public class CheckJavaVersion extends Applet 
{ 
    private static final long serialVersionUID = 1L; 
private static Label versionCheck; 
    public static String javaVersion; 
    public static URLConnection connection; 

    public void init() 
    { 
     try 
     { 
      Color colFrameBackground = new Color(198, 0, 0); 
      this.setBackground(colFrameBackground); 
      versionCheck = new Label("Java Version:"+System.getProperty("java.version")); 
      this.add(versionCheck); 
      javaVersion = versionCheck.getText(); 
      javaVersion="testversion"; 

      String javaVersion1= URLEncoder.encode(javaVersion); 
      URL currentPage=getDocumentBase(); 
      String protocol=currentPage.getProtocol(); 
      String host=currentPage.getHost(); 
      int port=currentPage.getPort(); 
      String urlSuffix=currentPage.toString(); 
      URL dataurl=new URL(protocol,host,port,urlSuffix); 
      connection=dataurl.openConnection(); 
      connection.setUseCaches(false); 
      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      ByteArrayOutputStream byteStream=new ByteArrayOutputStream(512); 
      PrintWriter out=new PrintWriter(byteStream,true); 
      out.print(javaVersion1); 
      out.flush(); 
      System.out.println(byteStream.toString()); 
      connection.setRequestProperty("Content-Length",String.valueOf(byteStream.size())); 
      connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
      byteStream.writeTo(connection.getOutputStream()); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

JSP页面

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Communication</title> 
</head> 
<body> 
<jsp:plugin code="com.applets.CheckJavaVersion" codebase="/AppletURLComm" type="applet"> 
</jsp:plugin> 

<% 
try 
{ 
    BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(CheckJavaVersion.connection.getInputStream())); 
    String data; 
    while((data=bufferedReader.readLine())!=null) 
    { 
     out.print(data); 
    } 
} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 
%> 
</body> 
</html> 
+1

堆栈跟踪始终是一个有价值的信息块。无论如何...你想用这个做什么? – mdrg 2011-01-31 11:53:07

回答

0

您无法读取在JSP页面中scriptlet的小程序中的变量。 Applet是一个Java应用程序,将HTML发送到客户端后将在客户端浏览器中运行,因此这将无法工作。

搜索了一下这里,所以你会发现这一点:applet communication using post method