Session Tracking

149 views
Skip to first unread message

JAGINI UDAYKUMAR

unread,
Mar 1, 2012, 12:25:55 AM3/1/12
to Google Web Toolkit
Hi

I am working on gwt. I want to know how to handle sessions(Session
Tracking like in servlets). Can anyone help me.

Maverick

unread,
Mar 1, 2012, 1:40:38 AM3/1/12
to Google Web Toolkit
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

                                         }
                                  });







Shrey Malhotra

unread,
Mar 1, 2012, 8:40:56 AM3/1/12
to google-we...@googlegroups.com
I am using cookies for login ! Which would you prefer ? Session or cookies ?

jhulford

unread,
Mar 1, 2012, 10:45:50 PM3/1/12
to Google Web Toolkit
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.
Reply all
Reply to author
Forward
0 new messages