views:

552

answers:

1

hi i have an applet and i must request to a web application to get data from server that is in database,i am workin with objects and it is very useful that server response me as objects !!

how an applet can communcate with server?

i think web services methode, RMI and... make me happy, but wich is the best and reliable?

+1  A: 

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.

Here is an example.

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();

Now read in the response

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

in.close();

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
 {
Knife-Action-Jesus
thanks a million, but can i ask, which is the better? web-service to connect with server or serialized objects?i am trying with web service but the ws-client has a problem to connect to server(web-service) !!!
sirvan
Ideally I would think you would want a webservice, so any applications can access this without worrying about maintaining a current copy of the serialized object. If its going to be used by only your group I don't see why you couldnt just use a serialized object. Serialized object - easier to implement, no xml parsing, no webservice hosting.Webservice - more flexible, probably more development time.Its really a question of what you need, simple vs flexible.
Knife-Action-Jesus
can you buffer and gzip the streams.. will it work?
Ryan Fernandes
hi, really thanks..., i correct the problem of connecting to web service, but i have a problem that make me despondent, how i can return an arraylist of custom object --vehicle--- from web service method!!!??i done it, but the problem is , in client side i receive arraylist of object !!! if u know how to solve it pls help me...
sirvan
I am not sure exactly what your asking, Are you asking how to cast the arraylist as an object of type vehicle? You have 2 options really just using a generic (Vehicle) cast or using the generics functionality. To use generics you can check out the 1.5 tutorial here. http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdfIf your using an older JRE pre 1.5 you wont be able to use generics and will have to explicit casts. If you can post any code to help understand your issue please do.
Knife-Action-Jesus
hi, no, the problem is not like this, the problem is not casting,i have a web service that must run query to db to obtain Vehicle's location info and create Vehicles object and put them (Vehicle objects) in an ArrayList... @WebMethodpublic ArrayList<VehileReport> getVehicles(){ //sample VehileReport v = new VehileReport("123",,"20km");ArrayList<VehileReport> list = new...list.add(v);return list;}in client side i run this...
sirvan
try { ws.AVLInfoServiceService service = new ws.AVLInfoServiceService(); ws.AVLInfoService port = service.getAVLInfoServicePort(); java.lang.String customerID = ""; java.util.List<ws.VehileReport> result = port.getVehicles(customerID); expected result to me is a list of VehicleReport but returned result is a list of empty class without attributes and !!!!i find it now !! @XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "vehileReport")public class VehileReport {}i think there is a problem with XML difination of VehicleReport class,it didn't created..
sirvan
sorry there is no enough space to post code, i think there is somework should be done with custom class in server side to describe it in XML or a thing like it... * <p>Java class for vehileReport complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="vehileReport"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> ...this comment is in client side vehicleReport class...i hope help u to understand.sorry and thanksrelly thanks
sirvan
What are you using to bind the xml? EMF,JAXB,XMLBeans? That will dictate what you need to do to pull the data out.
Knife-Action-Jesus
i use netBeans IDE 6.0.1 that create web service automatically ,it seem JAXB is be used... now what is your idea? i am beginner with english, so excuse me if any thing was wrong...
sirvan
I've used JAXB a while back, with Java 1.4, Netbeans came around with Tiger I believe, I have little experience with netbeans, Check out the tutorial here http://wiki.netbeans.org/JAXBWizard#section-JAXBWizard-JAXBWizard.My guess is your not marshalling /unmarshalling properly.
Knife-Action-Jesus