问题
My app has an expensive service method, results of which must be 1) checked for errors and 2) presented to a Java applet via a URL (i.e. as opposed to a JavaScript variable). The method result is a string, and the applet is only capable of loading data from a file or URL.
I tried to deal with the problem using a session variable:
def action1 = {
def input = params['input']
def result = expensiveServiceMethod( input )
def failed = result == null
session['result'] = result
render( view:'view1', model:[failed:failed] )
}
def action2 = {
def result = session['result']
render( result )
}
Then, in view1
the applet is conditionally displayed depending on the failure status, and the results are accessed by the applet via the action2
URL.
Unfortunately, result
in action2
is coming up null
. I've verified that result
is not null
in action1
. Am I doing it wrong?
Note
I would have used flash
instead, but there are additional requests made in order to initialize the applet.
回答1:
Applets aren't able to track session cookies their self. So when your applet sends second request to action2 - it does't send session cookie back to server. Hence for server its like a brand new session, any thing you set in session during action1 won't be available in action2. You will have to track cookies in your applet and send them back to server when making calls.
I have never done it, but I think you may use Apache commons http client on your client side (applet) - it has support for tracking cookies
See this question -
来源:https://stackoverflow.com/questions/11134851/grails-session-variable-null