ex) Authenticate users on th开发者_如何转开发eir desktop app from a server.
Questions about server: Do I have to use Tomcat ? Are there any other solutions ? could I even use Apache ?
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.
There are a number of ways you could handle it:
Use a plain socket connection to communicate. Have a look at the java.net packages. http://java.sun.com/docs/books/tutorial/networking/sockets/
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.
精彩评论