Applet與servlet通訊問題.
我在學習透過“物件序列”的技術從Servlet向Applet傳輸完整的Java物件時,程式總是除錯通不過.請各位高人指點.程式清單如下:
///////////////////////////
1.所需要物件序列的物件類.
import java.io.*;
public class DataObject implements Serializable
{
int id;
String name;
int age;
String department;
public DataObject(int id,String name,int age,String department)
{
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
private void writeObject(ObjectOutputStream out)throws IOException
{
out.writeInt(id);
out.writeInt(age);
out.writeUTF(name);
out.writeUTF(department);
}
private void readObject(ObjectInputStream in) throws IOException
{
id = in.readInt();
age = in.readInt();
name = in.readUTF();
department = in.readUTF();
}
}
///////////////////////////////////////////
2.伺服器Servlet清單
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
//傳回物件給Applet
res.setContentType("application/octet-stream");
DataObject dataObj = new DataObject(1,"張三",88,"歷史系");
try {
OutputStream out;
ObjectOutputStream objStream;
out = res.getOutputStream();
objStream = new ObjectOutputStream(out);
objStream.writeObject(dataObj);
objStream.close();
} catch (ClassNotFoundException e) {
}
}
public String getServletInfo()
{
return "A simple Servlet!";
}
}
///////////////////////////////////////////
3.客戶端applet程式清單.
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class TestApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java!!", 50, 60 );
}
public void invoke() {
try {
DataObject dataObj;
String info;
URL url = new URL("http://192.168.1.130:8080/Servlet/TestServlet");
URLConnection con = url.openConnection();
con.connect();
InputStream in = con.getInputStream();
showStatus("2");
ObjectInputStream objStream;
objStream = new ObjectInputStream(in);
dataObj = (DataObject)objStream.readObject();
// objStream.close();
info = Integer.toString(dataObj.id);
this.getGraphics().drawString(info,50,80);
info = Integer.toString(dataObj.age);
this.getGraphics().drawString(info,50,100);
info = dataObj.name;
this.getGraphics().drawString(info,50,120);
info = dataObj.department;
this.getGraphics().drawString(info,50,140);
}
catch (Exception e) {
}
}
}
///////////////////////////
1.所需要物件序列的物件類.
import java.io.*;
public class DataObject implements Serializable
{
int id;
String name;
int age;
String department;
public DataObject(int id,String name,int age,String department)
{
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
private void writeObject(ObjectOutputStream out)throws IOException
{
out.writeInt(id);
out.writeInt(age);
out.writeUTF(name);
out.writeUTF(department);
}
private void readObject(ObjectInputStream in) throws IOException
{
id = in.readInt();
age = in.readInt();
name = in.readUTF();
department = in.readUTF();
}
}
///////////////////////////////////////////
2.伺服器Servlet清單
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
//傳回物件給Applet
res.setContentType("application/octet-stream");
DataObject dataObj = new DataObject(1,"張三",88,"歷史系");
try {
OutputStream out;
ObjectOutputStream objStream;
out = res.getOutputStream();
objStream = new ObjectOutputStream(out);
objStream.writeObject(dataObj);
objStream.close();
} catch (ClassNotFoundException e) {
}
}
public String getServletInfo()
{
return "A simple Servlet!";
}
}
///////////////////////////////////////////
3.客戶端applet程式清單.
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class TestApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java!!", 50, 60 );
}
public void invoke() {
try {
DataObject dataObj;
String info;
URL url = new URL("http://192.168.1.130:8080/Servlet/TestServlet");
URLConnection con = url.openConnection();
con.connect();
InputStream in = con.getInputStream();
showStatus("2");
ObjectInputStream objStream;
objStream = new ObjectInputStream(in);
dataObj = (DataObject)objStream.readObject();
// objStream.close();
info = Integer.toString(dataObj.id);
this.getGraphics().drawString(info,50,80);
info = Integer.toString(dataObj.age);
this.getGraphics().drawString(info,50,100);
info = dataObj.name;
this.getGraphics().drawString(info,50,120);
info = dataObj.department;
this.getGraphics().drawString(info,50,140);
}
catch (Exception e) {
}
}
}
相關文章
- BMS和AFE通訊問題
- 多程式通訊系列問題
- 【IDEA】HTML通過servlet3.0註解名提交表單到servlet類找不到頁面的問題IdeaHTMLServlet
- servlet 呼叫jdbc 出現得問題ServletJDBC
- 5.React中元件通訊問題React元件
- 解決go-micro與其它gRPC框架之間的通訊問題GoRPC框架
- .NET WebSocket高併發通訊阻塞問題Web
- TCP通訊之經典問題解決TCP
- 用javac編譯servlet類出現問題Java編譯Servlet
- 資訊保安問題與防範
- Android多程式通訊之幾個基本問題Android
- Android 多程式通訊之幾個基本問題Android
- Vue的常見問題(七) - 元件的通訊方式Vue元件
- C# UDP通訊 ReceiveAsync() 一直等待問題C#UDP
- [Java] 什麼是Applet (Java 小程式)JavaAPP
- Binder通訊機制與IPC通訊.md
- 圓通問題頻發背後的“罪與罰”
- 你知道可以通過網址訪問的Servlet如何實現嗎?Servlet
- CAsyncSocket TCP協議通訊速度慢的問題(轉)TCP協議
- Vue 中利用 eventBus 進行資料通訊的問題Vue
- 通訊是個大問題,還好我們有方法。
- v-if和v-show區別+元件通訊問題元件
- 使用 Rxjs 解決 Angular Component 之間的通訊問題JSAngular
- 序列通訊與並行通訊的區別並行
- ROS話題通訊和服務通訊的區別ROS
- 最近在看父子元件通訊中遇到的一個問題?元件
- 【ROS教程】話題通訊ROS
- 誰再問Servlet的問題,我就親自上門來教學了Servlet
- flutter 與native通訊初探Flutter
- 【rosbridge】ROS與Android通訊ROSAndroid
- Servlet3:從根源瞭解並解決編碼問題Servlet
- 點選<a href>標籤無法跳轉到servlet的問題Servlet
- 深入分析網路通訊,Wireshark助你解決網路問題!
- 如何解決聯邦學習中的通訊開銷問題?聯邦學習
- java鎖的問題速通Java
- servlet 轉發與重定向Servlet
- Filter 與 Servlet 的區別FilterServlet
- Servlet生命週期與方法Servlet
- Spring 容器與 Servlet互動SpringServlet