<< Java文件操作大全 | 首页 | Spring mvc is Working with Checkboxes >>

Topic: Communication between Servlet and Applet

====== Applet ======

public class HelloWorldApplet extends Applet
{
JPanel panel = null;
JLabel label = null;

public void init()
{
try {
panel = new JPanel();

panel.setLayout(new GridLayout(1,2));
label = new JLabel("first file modified");
label.setBackground(new Color(153, 184, 213, 100));
panel.add(label);

JComponent data = (JComponent)sendObject();

//JButton button = new JButton(data);
add(data);
} catch (Exception e) {
e.printStackTrace();
}
}

public Object sendObject() {
OutputStream out;
ObjectOutputStream objectStream;
Object data = null;
try {
URLConnection con = getServletConnection();
out = con.getOutputStream();
objectStream = new ObjectOutputStream(out);
objectStream.writeObject("request from applet");
objectStream.flush();
objectStream.close();

out.close();

data = readInputStream(con);

} catch(Exception e) {
e.printStackTrace();
}
return data;
}

private URLConnection getServletConnection() throws MalformedURLException, IOException {
URL servletURL = new URL("http://10.77.5.192:8080/plugins/servlet/helloworld");
URLConnection con = servletURL.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setDefaultUseCaches(false);
((HttpURLConnection) con).setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/x-java-serialized-object");
con.setAllowUserInteraction(false);
return con;
}

private Object readInputStream(URLConnection con) {
Object object = null;

try {
InputStream in = con.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(in);
object = objectInputStream.readObject();
in.close();
} catch(Exception e) {
/*try {
// now failing to read the inputstream does not mean the server did not send
// any data, here is how you can read that data, this is needed for the same
// reason mentioned above.
((HttpURLConnection) con).getResponseCode();
InputStream es = ((HttpURLConnection) con).getErrorStream();
int ret = 0;
// read the response body
while ((ret = es.read(byteBuffer)) > 0) {
}
// close the errorstream
es.close();
} catch (IOException ex) {
ex.printStackTrace();
}*/
e.printStackTrace();
}

return object;
}
}

====== Servlet ======

public class HelloWorldServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ObjectInputStream in = null;

try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
InputStream inStream = request.getInputStream();
byte byteBuffer[] = new byte[1024];
int len;

while((len = inStream.read(byteBuffer)) > 0) {
byteStream.write(byteBuffer);
}

in = new ObjectInputStream(new ByteArrayInputStream(byteStream.toByteArray()));
Object obj = in.readObject();

response.setContentType("application/x-java-serialized-object");
OutputStream outStream = response.getOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(outStream);
objectStream.writeObject(new DiffPanel());
objectStream.flush();
objectStream.close();

//outStream.close();
in.close();
byteStream.reset();
} catch(Exception e) {
e.printStackTrace();
}
}
}
另一个例子:

Servlet:


// ObjectInputStream inputStream = new ObjectInputStream(
// new BufferedInputStream(request.getInputStream()));
//
// try {
// System.out.println("got this:");
//
// System.out.println("got this1-->" +
// inputStream.readObject().toString());
// System.out.println("got this2-->" +
// inputStream.readObject().toString());
//
// } catch (Exception ex) {
// System.out.println("error: " + ex);
// } finally {
// try{
// inputStream.close();
// } catch (Exception e1){
// // Do nothing
// }
// System.out.println("-end-");
// }

Applet:

// ObjectInputStream response = null;
// Object result = null;
//
// try {
//
// System.out.println("Start");
//
//
// URL url = new URL(getCodeBase(), "/psmis/pmMeterImport.html");
//
// System.out.println(url.toExternalForm());
//
// URLConnection urlConnection = url.openConnection();
// urlConnection.setDoOutput(true);
// urlConnection.setUseCaches(false);
// urlConnection.setRequestProperty("Content-Type",
// "application/octet-stream");
//
// ObjectOutputStream request = new ObjectOutputStream(
// new BufferedOutputStream(urlConnection.getOutputStream()));
// request.writeObject("Test Object");
// request.writeObject(new Point(3,5));
// request.flush();
// request.close();
//
//
// InputStream is = urlConnection.getInputStream();
//
// System.out.println("finished");
// } catch (Exception ex){
// System.out.println("error");
// ex.printStackTrace();
// } finally {
// try {
// if(response != null) {
// response.close();
// }
// } catch (Exception ex) {
// // Do nothing
// }
// }

标签 :



发表评论 发送引用通报