本文介绍了小程序 - 服务器的通信,我该怎么做呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,我必须发送到Web应用程序的请求,从在数据库服务器获取数据。我与对象的工作,这是非常有用的,服务器响应我的对象!

I have an applet and I must send a request to a web application to get data from the server that is in a database. I am working with objects and it is very useful that the server responses me with objects!!

如何一个Applet可以与服务器进行通信?

How an applet can communicate with a server?

我认为Web服务方法,RMI和......让我高兴,但是这是最好的和可靠的?

I think web services method, RMI and... make me happy, but which is the best and reliable?

推荐答案

只要它只是你的applet与服务器通信,您可以使用序列化对象。你只需要保持对象类相同版本的两个小程序罐子和服务器上。它不是最开放的或可膨胀的路要走,但它很快就开发时间和pretty固体。

As long as its only your applet communicating with the server you can use a serialized object. You just need to maintain the same version of the object class in both the applet jar and on the server. Its not the most open or expandable way to go but it is quick as far as development time and pretty solid.

下面是一个例子。

实例化servlet的连接

Instantiate the connection to the servlet

URL servletURL = new URL("<URL To your Servlet>");
URLConnection servletConnect = servletURL.openConnection();
servletConnect.setDoOutput(true); // to allow us to write to the URL
servletConnect.setUseCaches(false); // Write the message to the servlet and not from the browser's cache
servletConnect.setRequestProperty("Content-Type","application/x-java-serialized-object");

获取输出流,写你的对象

Get the output stream and write your object

MyCustomObject myObject = new MyCustomObject()
ObjectOutputStream outputToServlet;
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
outputToServlet.writeObject(myObject);
outputToServlet.flush(); //Cleanup
outputToServlet.close();

现在在响应读

ObjectInputStream in = new ObjectInputStream(servletConnection.getInputStream());
MyRespObject myrespObj;
try
{
    myrespObj= (MyRespObject) in.readObject();
} catch (ClassNotFoundException e1)
{
    e1.printStackTrace();
}

in.close();

在你的servlet

In your servlet

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
  MyRespObject myrespObj= processSomething(request);
  response.reset();
  response.setHeader("Content-Type", "application/x-java-serialized-object");
  ObjectOutputStream outputToApplet;
  outputToApplet = new ObjectOutputStream(response.getOutputStream());
  outputToApplet.writeObject(myrespObj);
  outputToApplet.flush();
  outputToApplet.close();
}

private MyRespObject processSomething(HttpServletRequest request)
{
  ObjectInputStream inputFromApplet = new ObjectInputStream(request.getInputStream());
  MyCustomObject myObject = (MyCustomObject) inputFromApplet.readObject();
  //Do Something with the object you just passed
  MyRespObject myrespObj= new MyRespObject();
  return myrespObj;
}

只要记住,要传递需要两个对象来实现序列化

Just remember that both Objects that you are passing need to implement serializable

 public Class MyCustomObject implements java.io.Serializable
 {

这篇关于小程序 - 服务器的通信,我该怎么做呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:26