1

ex) Authenticate users on their desktop app from a server.

Questions about server: Do I have to use Tomcat ? Are there any other solutions ? could I even use Apache ?

KJW
  • 15,035
  • 47
  • 137
  • 243

2 Answers2

1

You need some way in your Java app to download the pages at specific given URLs.

URL url;
InputStream is = null;
DataInputStream dis;
String line;

try {
    url = new URL("https://stackoverflow.com/");
    is = url.openStream();  // throws an IOException
    dis = new DataInputStream(new BufferedInputStream(is));

    while ((line = dis.readLine()) != null) {
        System.out.println(line);
    }
} catch (MalformedURLException mue) {
     mue.printStackTrace();
} catch (IOException ioe) {
     ioe.printStackTrace();
} finally {
    try {
        is.close();
    } catch (IOException ioe) {
        // nothing to see here
    }
}

(taken from How do you Programmatically Download a Webpage in Java)

After that, it seems to me that any web server that will accept URLs with arguments will work, with a script on the server (any language) to accept requests. All you need is a response, and to maintain the session on the server.

Example: http://myserver.com/myscript.php?action=login&username=blah&password=blah would return a page saying if the action succeeded or not, and you would parse this.

Community
  • 1
  • 1
Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • Sorry, why was I modded down? I was just giving the simplest method to allow a desktop application to communicate with the server. – Chris Dennett Jun 17 '10 at 15:47
  • 1
    +1 HTTP is well defined, and the question mentions nothing that precludes it. http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol – trashgod Sep 09 '10 at 00:58
  • but, how about to do POST A FORM if we use the Java Swing (not web app)? – gumuruh Aug 16 '11 at 08:13
  • You could try appending the form element names / values to the URL, including the 'submit' button name, with any value (so it resolves to True). The format is like this: http://www.xyz-abc.kz/index.php?title=apocalypse.php&na=doom – Chris Dennett Aug 16 '11 at 10:04
1

There are a number of ways you could handle it:

  1. Use a plain socket connection to communicate. Have a look at the java.net packages. http://java.sun.com/docs/books/tutorial/networking/sockets/

  2. Use a web service and submit data, something like a REST service http://java.sun.com/developer/technicalArticles/WebServices/restful/

And these could be implemented with your own server, tomcat, apache, glassfish, jboss etc.... Too many options to name.

Essentially, you need something to listen on a pre-defined port on your server, this could be any of the above. Your swing app can use the above APIs also to communicate back and forth with the server. If you post a little more detail on what you are trying to do, you will probably get some more specific recommendations also.

Brad Gardner
  • 1,627
  • 14
  • 14