Thursday, September 27, 2012

Session Management Between Java Client and Java Servlet

For a variety of reasons, I am looking at what would be involved in writing a Java application to talk to a Java servlet.  Currently, the client side is a Javascript/struts page that loads from a server.  While this is a fine model if you had hundreds of thousands of potential clients, and the costs and security risks of pushing new client software, when the number of clients is small, and the cost of pushing new versions is trivial, there are some advantages to a thicker client with improved error checking and simplified debugging.

There is no shortage of examples of writing browser-based clients.  What I am hoping to find is an example with a Java application that opens a session with a Java servlet and exchanges data with that servlet.  

1 comment:

  1. Dang! I made a nice big response and it got eaten in a key stroke screwup...


    The fact that the server is Java is really not relevant to your problem. Since it was designed to feed a Javascript front end, it probably handles input and output either in XML or JSON.

    If it is in XML, you need to be able to parse the XML in Java, which can be easy or ugly, depending on various things. If possible, use XPATH with namespaces disabled.

    If in JSON, Google up a JSON library for Java.

    Doing the communications is an area where you want to be careful. There are lots of examples of HTTP client code out there, but many ignore error cases (unexpected EOF reading the response, timeouts, etc).

    You should probably use the Java library class If you are doing something really hairy, perhaps HttpClient library from Apache, but I've always found it to be overkill.


    One using a slighly lower level approach, but includes a bit of GUI:

    One using HttpUrlConnection:


    I haven't done a Java client in about a decade. When I did, we used some sort of UI (I think it was a NetBeans predecessor) that would let us build forms graphically and then it would generate skeleton Java code which we would fill in.
    I'm sure you can find plenty of stuff out there on the web to do it.

    If you want to discuss this, use the email on my website (URL attached to post).