该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/**
* Get a parameter value
*
* @param key String
* @param def String
* @return String
*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**
* Construct the applet
*/
public GoodEveningApplet() {
}
/**
* Initialize the applet
*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**
* Component initialization
*
* @throws Exception exception
*/
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
upper.setBackground(Color.orange);
jLabel1.setText("jLabel1");
jButton1.setText("jButton1");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.add(upper, BorderLayout.NORTH);
upper.add(jButton1);
upper.add(jLabel1);
}
/**
* Start the applet
*/
public void start() {
}
/**
* Stop the applet
*/
public void stop() {
}
/**
* Destroy the applet
*/
public void destroy() {
}
/**
* Get Applet information
*
* @return String
*/
public String getAppletInfo() {
return "Applet Information";
}
/**
* Get parameter info
*
* @return String[][]
*/
public String[][] getParameterInfo() {
return null;
}
private String refresh() throws MalformedURLException, IOException {
URL url = new URL("http://localhost:8080/appletservlet/showmemservlet");
URLConnection con = url.openConnection();
con.setUseCaches(false);
InputStream in = con.getInputStream();
DataInputStream textStream;
textStream = new DataInputStream(in);
String line1 = textStream.readLine();
String line2 = textStream.readLine();
double freeMem = Double.parseDouble(line1);
double totalMem = Double.parseDouble(line2);
double usedMem = totalMem - freeMem;
double percentUsed = (int) 100 * (usedMem / totalMem);
return String.valueOf(percentUsed);
}
public void jButton1_actionPerformed(ActionEvent e) {
try {
System.out.println(refresh());
jLabel1.setText(refresh());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
*** GoodEveningApplet.html文件如下:
Good Evening HTML Page
firstapplet.GoodEveningApplet will appear below in a Java enabled browser.
CODEBASE = "."
CODE = "firstapplet.GoodEveningApplet.class"
ARCHIVE = "GoodEvening.jar"
NAME = "TestApplet"
WIDTH = 400
HEIGHT = 300
HSPACE = 0
VSPACE = 0
ALIGN = top
>
You need a Java-enabled browser running JDK 1.1.x or
greater to view this applet.
总结
从上面的例子大家可以看到,如果你使用web技术定义了很多的业务组件,你都可以通过java隧道技术来访问,而且客户端程序可以有很多的变化。
另外,对于上面的例子我们只是用最简单的方法从服务器端向客户端发送了两个字符串类型,如果服务器处理的数据很复杂,我们可以将数据封装成数据类,然后让这个类实现Serializable这个接口就可以了,这样处理起来会更加方便,有兴趣的朋友可以自己试试。