views:

893

answers:

1

I want to write some page with JavaFX applet. I want content on the applet to be dependent on user logged in.

I know I can call web services from JFX, but then what about login and session? Besides I think there might exist some better solutions for such communication than calling from applet a web service sitting on the machine applet comes from.

How can I do it?

+1  A: 

You can build a servlet which returns the name of the logged in user.

Then in javafx you can use the class javafx.io.http.HttpRequest to call the servlet and read out the username. (The API also has some examples of how to use the HttpRequest)

The following javafx code prints out the return string of a Servlet:

var response: String;

def myRequest: HttpRequest = HttpRequest {

location: "http://localhost:8080/demo/foo.do";

method: HttpRequest.GET;

onInput: function(is: java.io.InputStream) {
    var buff: StringBuffer = new StringBuffer();

        var reader: BufferedReader

            = new BufferedReader(new InputStreamReader(is));

        var data: String;

        while ((data = reader.readLine()) != null) {

          buff.append(data);

        }

        response = buff.toString();

        reader.close();
        println(response);
}
};

myRequest.start();

EDIT: You should also take a look at this article: http://blogs.sun.com/warren/entry/authenticating_a_javafx_application_using which shows how to access the html document and cookies from within the applet which resides on the document. That should be a very interesting approach for authentication.

räph
This is something I was missing, if only such HttpRequest shares session with "normal", browser request. I assume it does. So, does it?
amorfis
hmm. Actually I just tried and they don't seem do share the same session. At least the session of the fx httprequest was empty. But I'm not that much into servlets. Maybe there is some way to store the username on serverside to return it to fx later.But I also found another very interesting article I just added to the answer...
räph