开发者

Jsp and applet communication

I want to access the outputstream of the Applet from a JSP page where the applet is embedded. But it is giving NullPointerException. I have given the code below.

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 Page

<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>


You can't read a variable from the applet in a JSP page scriptlet. The Applet is a Java app that will run in the client browser after the HTML is sent to the client, so this will never work.

Searching a bit here on SO you would find this: applet communication using post method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜