简体   繁体   中英

How can I pass server session variables and attributes to the GWT engine?

I am wondering whether we can pass session attributes and parameters from JSP to GWT Entry point.

Like I have integrated Struts2 with GWT.

Now, my question is, Can we pass a session attribute from JSP to GWT Entry point? Thanks in advance.

I am not getting why you want to pass session object, after all you want use it in GWT Servlet and not in GWT UI side am I right?

You can get the session object in GWT Servlet by following ways:

  HttpServletRequest request = this.getThreadLocalRequest();
  HTTPSession session = request.getSession();

And after that you can user following methods to get and set session attributes:

  session.setAttribute("XXX", object);
  session.getAttribute("XXX")

EDIT:

I think then there isn't straight forward solution, the workaround can be as follow:

In EntryPoint from onModuleLoad call server side code of GWT which will have access to session object.

  HttpServletRequest request = this.getThreadLocalRequest();
  HTTPSession session = request.getSession();

Get the attributes and parameter you want from session object and return it to client in the form of beans.

The answer is JSP + GWT Dictionary class.

Using a JSP (instead of html file) as hosting file, call Dictionary class anytime after GWT on module load.

<script>
  var info = {js object structure}; // Use Dictionary class to retrieve info.
</script>

GWT-Platform login + session management

Where to strore config parameteres in GWT?

Semi dynamic constants in GWT

Can GWT Properties be used from the server?


But passing session info to entry point may not be advantageous. The begging question is, what happens when session expires and GWT client is still on the browser?

I guess you could check that if session is stale, you make a window.location call to refresh the GWT client. Which is not a good practice. One of the reasons of using AJAX is not having to say you are sorry (I mean, not having to refresh your page), while conducting client-server data interchanges.

You should pass all sorts of static info from server to client thro the JSP+Dictionary way, but not for session.

Your architecture should force re-authentication on session expiration, and the session info should be carried as headers (if you are using REST or RequestBuilder) or carried as part of callback response data structure (if using RPC).

I my application, I use hidden boxes in the jsp:

<input type="hidden" id="dir_lat" value="<%= request.getattribute("foo") %>"/>

and then to get the value in gwt.application:

String foo = ((InputElement) doc.getElementById("dir_lat")).getValue() ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM