0

I found a problem in my GWT application that when I use one user to open two GWT clients and call the same server method, one client get the data just the same as the another, but it is wrong. How to avoid this problem.

for example: I open a page to do plus calculate 1+1 and get answer from server method add(), and at the same time, i open another page to do plus calculate 2+2 and get answer from the same server method, I get the same answer that 1+1=2 and 2+2=2 too. My english is not good enough to descript this problem more clearly By the way, I use global variables to store data in servlet.

Another example:

public class ConfigServiceImpl extends GwtConfigController implements ConfigService {
    private String serviceInstanceId = "";
    public String testConnection() {
     if(serviceInstanceId.trim().length() > 0){
              return "error : serviceInstanceId";
         }
         serviceInstanceId = "test";
         return serviceInstanceId;
}
}

In my client widget init, call testConnection. I open two page to test, first page, return "test", second return error message.

  • 1
    The description is a bit vague. What data? What does it mean that _one user opens two GWT clients_, do you open the same app in two different browser tabs, or is it something different? – siledh Oct 17 '13 at 08:43

2 Answers2

0

You should write a servlet that calls your methods and test if the error is really coming from the client: I'm pretty sure it is because of your code on the server: each web page lives its own javascript variables, there is no way for two instances of a client to share variables: it would be as if I can steal data from a user's open pages through javascript (that would be a serious security issue)

Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40
  • Thank u for your answer, I have a test case: My ServiceImpl class has a variable -- private String serviceInstanceId = ""; In this class, has a function: public TestRender initOne2One(String oid) { if(serviceInstanceId.length() > 0){ //return render with error message; } serviceInstanceId = "test"; } when init client widget, this function will be called – user2889481 Oct 17 '13 at 11:15
  • But test result is that first call, no error message, second call from different client by the same user, error message return. – user2889481 Oct 17 '13 at 11:35
0

You shouldn't store data on the server side in global variables (members in the servlet class?). Either store data in a database or (easier) store it in sessions. So the data gets user-specific and is not shared between different users:

    @Override
    public void add(int a, int b) {
    HttpServletRequest request = this.getThreadLocalRequest();
    HttpSession session = request.getSession();
    int result = a + b;
    session.setAttribute("aNameForTheValue", result); }


@Override
   public void otherMethod()
    {
    HttpServletRequest request = this.getThreadLocalRequest();
   HttpSession session = request.getSession();
   int addResult=  (int) session.getAttribute("aNameForTheValue"); 
   ...
   }
Community
  • 1
  • 1
Akkusativobjekt
  • 2,005
  • 1
  • 21
  • 26
  • I have thought that one client request should connect to one independent server instance. Is it wrong? if it is wrong how can I store my process data when calculate a complicate question? If add() function is just my first step, how can I store its result for next step? – user2889481 Oct 17 '13 at 11:31
  • Afaik there is only one instance of each Impl class. I added a code snippet to my answer to store data in the session. – Akkusativobjekt Oct 17 '13 at 11:47
  • Is GWT have any way to connect one client request to one independent server instance? I must store lots of process data in one server place to get them quickly. – user2889481 Oct 17 '13 at 12:02
  • You could store an object with the necessary members and methods in the session. Then in the servlet classes you get the object from the session and call the needed methods. – Akkusativobjekt Oct 17 '13 at 12:12