小程序——服务器通信,我该怎么做? [英] Applet - server communication, how can I can do it?

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

问题描述

我有一个小程序,我必须向 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!!

小程序如何与服务器通信?

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?

推荐答案

只要您的小程序与服务器通信,您就可以使用序列化对象.您只需要在小程序 jar 和服务器上维护相同版本的对象类.它不是最开放或可扩展的方式,但它的开发时间很快,而且非常可靠.

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
 {

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆