Thursday, March 1, 2012

Re: Session Tracking

Setting the session as a static member variable is going to cause you
all sorts of concurrency problems, even if it was a non-static member
you'll have the same issues since the same servlet is used to service
every HTTP request. There's really no reason for it.

If your server side is java, everything in GWT is a servlet so there's
really no difference in how GWT works. If using GWT RPC, just extend
RemoteServiceServlet and use getThreadLocalRequest().getSession() as
suggested to retrieve your client's HTTPSession.

On Mar 1, 12:40 am, Maverick <sujeet...@gmail.com> wrote:
> Write a service on Server side which extends RemoteServiceServlet.
> Create an object of type HttpSession as a member.
> Write methods to serAttribute and getAttribute.
>
> On client side. Inject the service and call setAttribute and
> getAttribute methods wherever you want to add data to session or read
> data from session.
>
> Refer sample code below:
>
> SessionService ( On server side)
>
> import java.awt.List;
>
> import javax.servlet.http.HttpSession;
>
> import com.google.gwt.user.server.rpc.RemoteServiceServlet;
>
> @SuppressWarnings("serial")
> public class SessionImpl extends RemoteServiceServlet implements
> ISession {
>
>        public static HttpSession httpSession;
>
>        @Override
>        public String getStrAttribue(String attribute) {
>               httpSession = getThreadLocalRequest().getSession(true);
>               return  (String) httpSession.getAttribute(attribute);
>        }
>
>        @Override
>        public void setAttribue(String attribute, String attValue) {
>               httpSession = getThreadLocalRequest().getSession(true);
>               httpSession.setAttribute(attribute, attValue);
>
>        }
>
> }
>
> To use session (on client side)
>
> @Inject
>        public ISessionAsync session;
>
>
> session.setAttribue("User","Sujit",new  AsyncCallback<Void>() {
>
>                                          @Override
>                                          public void onSuccess(Void
> result) {
>                                                 //some event
>                                          }
>
>                                          @Override
>                                          public void
> onFailure(Throwable caught) {
>                                                 // TODO Auto-generated
> method stub
>
>                                          }
>                                   });

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment