我想从嵌入applet的JSP页面访问Applet的输出流。但它正在给NullPointerException
。我已经给出了以下代码。
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>
答案 0 :(得分:0)
您无法从JSP页面scriptlet中的applet读取变量。 Applet是一个Java应用程序,它将在HTML发送到客户端后在客户端浏览器中运行,因此这将无法工作。
在这里搜索一下你会发现:applet communication using post method