基本上..当我从命令提示符运行时,我可以从LAN端口读取数据。
javac XYZ.java
appletviewer XYZ.html
这是在我的计算机上的工作。我可以读取数据并在小程序上显示。
但是,当我在ASPX页面上上传小程序并运行时。它正在加载并正在运行。但这不是从端口读取数据。
这是我的代码:
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.io.*;
import java.net.*;
public class FCApplet extends JApplet implements ActionListener , Runnable
{
Thread th = new Thread(this);
TextField txtID,txtName,txtResult;
Label lblID,lblName,lblResult;
String MemberID;
Panel p2;
public void init()
{
setBackground(Color.decode("#BFBFBF"));
lblID = new Label(" ID ");
lblID.setFont(new Font("Verdana", Font.BOLD , 12));
txtID= new TextField();
lblName= new Label(" Name ");
lblName.setFont(new Font("Verdana", Font.BOLD , 12));
txtName= new TextField();
lblResult= new Label(" Result ");
lblResult.setFont(new Font("Verdana", Font.BOLD , 12));
txtResult= new TextField();
Button b = new Button("Connect");
b.setFont(new Font("Verdana", Font.BOLD , 12));
b.addActionListener(this);
p2=new Panel();
p2.setLayout(new GridLayout(1,10,5,5));
p2.setPreferredSize(new Dimension(900, 20));
p2.add(lblID);
p2.add(txtID);
p2.add(lblName);
p2.add(txtName);
p2.add(lblResult);
p2.add(txtResult);
p2.add(b);
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10,10,10,10);
c.fill=GridBagConstraints.HORIZONTAL;
c.gridy=1;
gbl.setConstraints(p2,c);
add(p2);
}
public void StartTest()
{
DatagramSocket sock = null;
try
{
//1. creating a server socket, parameter is local port number
sock = new DatagramSocket(8001);
//buffer to receive incoming data
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
//2. Wait for an incoming data
System.out.println("Server socket created. Waiting for incoming data...");
//communication loop
while(true)
{
sock.receive(incoming);
byte[] data = incoming.getData();
String s = new String(data, 0, incoming.getLength());
//echo the details of incoming data - client ip : client port - client message
System.out.println(incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " : " + s);
if(s != "" )
{
String R = incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " : " + s;
//lblResultTest.setText(R);
String[] words = s.split("&");
txtResult.setText(words[2]);
this.getAppletContext().showDocument( this.getDocumentBase() );
}
//s = "OK : " + s;
//DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort());
//sock.send(dp);
}
}
catch(IOException e)
{
System.err.println("IOException " + e);
}
}
public void run()
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
public void actionPerformed(ActionEvent e)
{
Button source = (Button)e.getSource();
if(source.getLabel() == "Connect")
{
StartTest();
}
else{
JOptionPane.showMessageDialog(null,"Please Provide Input","alert",JOptionPane.WARNING_MESSAGE);
}
}
}
下面的图像是我在命令提示符下的输出
enter image description here
enter image description here
以下是Web浏览器的输出。
[在此处输入图片描述] [3]
最佳答案
是。终于得到解决方案。创建applet jar文件时出错。
研发后
最终成功获得结果。
http://www.pawlan.com/monica/articles/signedapps/
http://www.onjava.com/pub/a/onjava/2001/03/22/plugin.html?page=3