Hallo!
Ich hab' grad folgendes Problem: Ich hab ein Applet programmiert, das mittels URL, URLConnection und ObjectInputStream/ObjectOutputStream eine serialisierte ArrayList von einem JSP entgegennimmt. Das funktioniert soweit einwandfrei.
Hier der vereinfachte Code:
Die HTML-Datei (readapplet.html):
Die JSP-Datei (readdata.jsp):
Die Java Applet Klasse (ReadApplet.java):
Wie gesagt, der Code oben funktioniert einwandfrei!
Aber ich muss auch die Möglichkeit haben, dass das Applet ein Objekt serialisiert und dieses an eine JSP sendet.
Hier der vereinfachte Code:
Die HTML-Datei (writeapplet.html):
Die Java Applet Klasse (WriteApplet.java):
Der Einfachheithalber sendet das Applet derzeit nur Daten an die JSP-Datei, das Einlesen der Antwort mit dem InputStream ist daher auskommentiert.
Die JSP-Datei, die das serialisierte Date-Objekt entgegennimmt und in der Session speichert (writedata.jsp).
Und hier eine JSP-Datei, die für Testzwecke die Session-Variable ausgibt (show_written_data.jsp):
Derzeit sieht es so aus, dass das Applet WriteApplet nicht an die JSP-Datei writedata.jsp herankommt, aber es wird auch keine Exception oder so geworfen. Irgendwo steckt daher wahrscheinlich ein elementarer Fehler im Applet Code ... Kann auch eine falsche Konfiguration der Umgebung (Browser, Firewall, Betriebssystem) für das nicht funktionieren verantwortlich sein?
Ich hab' mich schon mit den URLConnection-Eigenschaften herumgespielt, aber ohne Erfolg ...
... deswegen wende ich mich an Euch. Vielen Dank.
mfg Kaan
Ich hab' grad folgendes Problem: Ich hab ein Applet programmiert, das mittels URL, URLConnection und ObjectInputStream/ObjectOutputStream eine serialisierte ArrayList von einem JSP entgegennimmt. Das funktioniert soweit einwandfrei.
Hier der vereinfachte Code:
Die HTML-Datei (readapplet.html):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Applet / JSP Communication</title>
</head>
<body>
<h1>Read data from a JSP</h1>
<applet code="ReadApplet.class" width="640" height="480">
Applet can not be showed!
</applet>
</body>
</html>
Die JSP-Datei (readdata.jsp):
Code:
<%@ page import="java.io.*" %><%@
page import="java.util.*" %><%
// imports done
// define ArrayList with some data
ArrayList<String> data = new ArrayList<String>();
data.add("John McClane");
data.add("John Rambo");
data.add("Jason Bourne");
data.add("Rocky Balboa");
data.add("Kaan (haha)");
if (request.getParameter("name") != null) {
data.add(request.getParameter("name"));
}
// set content type
String contentType = "application/x-java-serialized-object";
response.setContentType(contentType);
// wrap the OutputStream into an ObjectOutputStream
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
// write ArrayList into the ObjectOutputStream
oos.writeObject(data);
// flush and close ObjectOutputStream
oos.flush();
oos.close();
%>
Die Java Applet Klasse (ReadApplet.java):
Code:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class ReadApplet extends JApplet {
private JTextArea textArea = new JTextArea();
public void init() {
// create a new JScrollPane which embeds the JTextArea
JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// add the JScrollPane to the content pane
add(scrollPane);
}
public void start() {
// define ArrayList for the data to retrieve
ArrayList<String> data = new ArrayList<String>();
try {
// define the source URL
URL urlJsp = new URL(getDocumentBase(), "readdata.jsp?name=Stefan");
// open an URL connection to the source
URLConnection connection = urlJsp.openConnection();
// we don't want to send any data, but we want to get some
connection.setDoInput(true);
connection.setDoOutput(false);
// don't use a cached version of URL connection
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
// get an ObjectInputStream
ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
// read data
data = (ArrayList<String>) ois.readObject();
// close ObjectInputStream
ois.close();
} catch (MalformedURLException ex) {
System.err.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.err.println(ex.getMessage());
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
// print the content of the ArrayList into the JTextArea
Iterator<String> it = data.iterator();
while (it.hasNext()) {
textArea.append(it.next());
textArea.append("\n");
}
}
public void stop() {
// do nothing
}
public void destroy() {
// do nothing
}
}
Wie gesagt, der Code oben funktioniert einwandfrei!
Aber ich muss auch die Möglichkeit haben, dass das Applet ein Objekt serialisiert und dieses an eine JSP sendet.
Hier der vereinfachte Code:
Die HTML-Datei (writeapplet.html):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Applet / JSP Communication</title>
</head>
<body>
<h1>Write data to a JSP</h1>
<applet code="WriteApplet.class" width="640" height="480">
Applet can not be showed!
</applet>
</body>
</html>
Die Java Applet Klasse (WriteApplet.java):
Code:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class WriteApplet extends JApplet {
public void init() {
add(new JLabel("WriteApplet"));
}
public void start() {
try {
// define the destination URL
URL urlJsp = new URL(getDocumentBase(), "writedata.jsp");
// open an URL connection to the destination
URLConnection connection = (URLConnection) urlJsp.openConnection();
// we want to send and receive some data
connection.setDoInput(true);
connection.setDoOutput(true);
// don't use a cached version of URL connection
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
// get an ObjectOutputStream
ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
// get an ObjectInputStream
//ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
// write data
oos.writeObject(new Date());
// flush and close ObjectOutputStream
oos.flush();
oos.close();
// close ObjectInputStream
//ois.close();
} catch (MalformedURLException ex) {
System.err.println(ex.getMessage());
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
public void stop() {
// do nothing
}
public void destroy() {
// do nothing
}
}
Der Einfachheithalber sendet das Applet derzeit nur Daten an die JSP-Datei, das Einlesen der Antwort mit dem InputStream ist daher auskommentiert.
Die JSP-Datei, die das serialisierte Date-Objekt entgegennimmt und in der Session speichert (writedata.jsp).
Code:
<%@ page import="java.io.*" %><%@
page import="java.util.*" %><%
// imports done
// wrap the InputStream into an ObjectInputStream
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
// read data from the ObjectInputStream
Date date = (Date) ois.readObject();
// close ObjectInputStream
ois.close();
// write the retrieved data into the session
session.setAttribute("appletdata", date);
%>
Und hier eine JSP-Datei, die für Testzwecke die Session-Variable ausgibt (show_written_data.jsp):
Code:
<html>
<head>
<title>Show written data</title>
</head>
<body>
<h1>Show written data</h1>
<%= session.getAttribute("appletdata") %>
</body>
</html>
Derzeit sieht es so aus, dass das Applet WriteApplet nicht an die JSP-Datei writedata.jsp herankommt, aber es wird auch keine Exception oder so geworfen. Irgendwo steckt daher wahrscheinlich ein elementarer Fehler im Applet Code ... Kann auch eine falsche Konfiguration der Umgebung (Browser, Firewall, Betriebssystem) für das nicht funktionieren verantwortlich sein?
Ich hab' mich schon mit den URLConnection-Eigenschaften herumgespielt, aber ohne Erfolg ...
... deswegen wende ich mich an Euch. Vielen Dank.
mfg Kaan