星期一, 2月 05, 2007

公益教學網

青輔會的家教網站需要用到聊天室。我之前努力的找了一些applet的聊天室和線上語音現在面臨到了需要拿到參數才可以使用。

剛好發現這樣一個Demo的東西,就秀給大家看。(來源很不好意思,我找到放太久了,忘了是哪來的了,大陸的!)

另外一個問題是Applet存取用戶端電腦資源必須要做設定,但又不希望使用者動手。所以現在必須找一個方法讓Applet可以合法且安全的存取用戶端電腦!方法大概明瞭了,下次找到完整解法在Post,或是轉貼吧。

頁面上一個Applet呼叫一個伺服器Servlet,而從Servlet產生一個Java對象再傳給另外一個Applet,怎么樣讓Servlet實時地把對象傳給Applet這是一個問題,有一個方案是採用JMS(JavaMessageService),在我以後的文章中會有敘述的.這裡我給大家展示一個簡單的實現,那就是讓Applet主動訪問Servlet,讓Servlet返回Java對象.下面給出全部程式碼和配置,其中Applet部分涵蓋了Javascript與Applet的互相調用,對於該部分不感興趣的朋友可以省略.


第一步,編寫需要傳遞的對象類
/**
* Class Person just a demo for translate this class to client
*/

package exapplet;

import java.io.*;


public class Person
implements Serializable {//必須實現Serializable界面才能序列化

private String m_Name;

private int m_Age;


public Person() {

this("",0);

}

public Person(String name,int age) {

this.m_Name = name;

this.m_Age = age;

}

public String getName() {

return m_Name;

}

public int getAge() {

return m_Age;

}


//Serializable界面中聲明的方法

private void writeObject(java.io.ObjectOutputStream out)

throws IOException {

out.defaultWriteObject();

}

//Serializable界面中聲明的方法

private void readObject(java.io.ObjectInputStream in)

throws IOException, ClassNotFoundException {

in.defaultReadObject();

}



public static void
main(String[] args) {

try {

Person p = new Person("Liaoyuan",25);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new
File("test.obj")));

oos.writeObject(p);

oos.close();

ObjectInputStream ois = new ObjectInputStream (new FileInputStream(new
File("test.obj")));

Person q = (Person)ois.readObject();

ois.close();

System.out.println(q.getName());

System.out.println(q.getAge());

} catch(Exception exp) {

exp.printStackTrace();

}

}

}


第二步,實現Servlet

package exapplet;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;


public class MyServlet
extends HttpServlet

{

public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException,IOException

{

String fPath = "H:\\XSchool\\WorkingRoom\\exapplet\\src\\java.obj";

String act = req.getParameter("Action");

if ( act.equals("UpdateObject") ) {

//創建更新JavaObject

Person p = new Person("Liaoyuan",26);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new
File(fPath)));

oos.writeObject(p);

oos.close();

DataOutputStream dos = new DataOutputStream(res.getOutputStream());

dos.writeBytes("Server Java Object Updated OK!");

dos.close();

} else if ( act.equals("GetObject") ) {

//傳回對象給Applet

res.setContentType("application/octet-stream");

Person p;

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(fPath)));


try {

p = (Person)ois.readObject();

} catch (ClassNotFoundException e) {

p = new Person();

}

ois.close();



ObjectOutputStream oos = new
ObjectOutputStream(res.getOutputStream());

oos.writeObject(p);

oos.close();

}

}

public String getServletInfo()

{

return "A simple Servlet!";

}

}


第三步,實現呼叫Servlet更新對象的Applet


package exapplet;

import java.awt.*;

import java.applet.*;

import java.io.*;

import java.net.*;

import netscape.javascript.*;


public class MyApplet
extends java.applet.Applet

{

public void init()

{

}

public void paint(Graphics g)

{}

//This method will be call in html

public void invoke() {

try {

URL url = new URL("http://rookie:8080/workingroom/exapplet/PostToApplet?Action=UpdateObject");


URLConnection urlcon = url.openConnection();

urlcon.connect();


DataInputStream dis=new
DataInputStream(urlcon.getInputStream());

String txt = dis.readLine();

dis.close();



String[] info = new
String[]{txt};

JSObject.getWindow(this).call("alert",info);

} catch (Exception e) {

}

}

}


第四步,實現提取對象的Applet

package exapplet;

import java.awt.*;

import java.applet.*;

import java.io.*;

import java.net.*;

import netscape.javascript.*;

public class MyAppletB extends java.applet.Applet

{

public void init()

{

}

public void paint(Graphics g)

{}

//This method will be call
in html

public void invoke() {

try {

URL url = new URL("http://rookie:8080/workingroom/exapplet/PostToApplet?Action=GetObject");


URLConnection urlcon = url.openConnection();

urlcon.connect();

ObjectInputStream ois=new ObjectInputStream(urlcon.getInputStream());

Person p = (Person)ois.readObject();

ois.close();

String[] info = new String[]{"Person Info\n\n-Name: "+p.getName()+"\n-Age : "+p.getAge()};


JSObject.getWindow(this).call("alert",info);

} catch (Exception e) {

}

}

}


第五步,配製WebServer(我用的是Tomcat4.0)


拷貝編譯后的MyServlet.class到相應目錄下,

編輯對應的web.xml文件,確保下面的內容正確

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

<servlet>

<servlet-name>PostToApplet</servlet-name>

<servlet-class>exapplet.MyServlet</servlet-class>

</servlet>



<servlet-mapping>

<servlet-name>

PostToApplet

</servlet-name>

<url-pattern>

/exapplet/PostToApplet

</url-pattern>

</servlet-mapping>

</web-app>



第六步,編寫html文件

呼叫更新的html

<HTML>

<HEAD>

<TITLE> New Document For Using Applet Call Servlet Update Java Object</TITLE>


</HEAD>



<BODY>

Hello This is a Test!<br>

<APPLET id="obj_applet" align=center code="exapplet.MyApplet" codeBase="."
width=400 height=300 name="obj_applet" archive="BV7RFD3P.ZIP"></APPLET>

<Input type=button value="CallAppletMethod" onclick="callApplet()">

<Script language=javascript>

function callApplet()

{

obj_applet.invoke();

}

</Script>

</BODY>

</HTML>



提取對象的html

<HTML>

<HEAD>

<TITLE> New Document For Using Applet </TITLE>

</HEAD>



<BODY>

Hello This is a Test!<br>

<APPLET id="obj_applet" align=center code="exapplet.MyAppletB" codeBase="."
width=400 height=300 name="obj_applet" archive="BV7RFD3P.ZIP"></APPLET>

<Input type=button value="CallAppletMethod" onclick="callApplet()">

<Script language=javascript>

function callApplet()

{

obj_applet.invoke();

}

</Script>

</BODY>

</HTML>

</pre>

沒有留言:

張貼留言