2011-01-31 12 views
0

アプレットが埋め込まれているJSPページからアプレットの出力ストリームにアクセスしたいとします。しかし、それは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

+0

Related:http://stackoverflow.com/questions/4850351/applet-and-jsp-communication – BalusC

+0

[削除されたコメント] – mdrg

答えて

0

あなたは、JSPページのスクリプトレットでアプレットから変数を読み取ることができません。アプレットは、HTMLがクライアントに送信された後にクライアントブラウザで実行されるJavaアプリケーションであるため、これはうまく動作しません。

SOあなたがこれを見つけるだろうに、ここで少し検索:applet communication using post method

関連する問題