程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Servlet傳送對象給Applet使用

Servlet傳送對象給Applet使用

編輯:關於JAVA

需求是這樣的:他想通過頁面上一個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
* @author: rookie
* @datetime: 2002-7-26
*/
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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved