• Post Reply Bookmark Topic Watch Topic
  • New Topic

exact API for java stand-alone application talk to web tier-servlet

 
Ruilin Yang
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Please help! I would like to talk to a servlet from a java stand-alone application (richer graphic. What are exact API at both client side and the server side ?
Thanks
Ruilin Yang
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at java.net and java.io.
You can open a connection to the servlet URL and pass objects back and forth. I have an example of this in my class notes:
Client code:
URL servlet = new URL("http://127.0.0.1/servlet/ServletTest");
URLConnection conn = servlet.openConnection();
conn.setRequestProperty("CONTENT_TYPE", "application/octet-stream");
conn.setDoOutput(true);
ObjectOutputStream os = new ObjectOutputStream(conn.getOutputStream());
os.writeObject(shoppingCart);
os.flush();
os.close();
ObjectInputStream is = new ObjectInputStream(conn.getInputStream());
shoppingCart = (ShoppingCart)is.readObject();
is.close();

Servlet code:
// req = HttpServletRequest res = HttpServletResponse
ObjectInputStream is = new ObjectInputStream(req.getInputStream());
ObjectOutputStream os = new ObjectOutputStream(res.getOutputStream());
res.setContentType("application/octet-stream");
shoppingCart = (ShoppingCart)is.readObject();
processOrder(shoppingCart);
os.writeObject(shoppingCart);
os.flush();
os.close();
 
Ruilin Yang
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
That is great to me. Thanks a lot.
With API you posted we need to serialized the object which is passed between server and client.
Here I read from J2EE blueprint:
The java application programming model for Web clients send messages to the J2EE server in the form of HTTP POST requests with an XML payload, and receive XML in the HTTP response.
This means another way instead of sending objects. However, with XML, we need a parser at both server and client side ?
Do I miss anything here, please clarify.
Thanks again
Ruilin
[ January 25, 2002: Message edited by: Rulin Yang ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use XML then you can use SOAP but that is not required in this case. Just make sure that whatever objects you want to pass back and forth implement Serializable and the writeObject() and readObject() methods will do all the work for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!