getServerName and getContextPath()?

Hi everyBody
i’m new to itmill and i want to get reference to the current request?
i usually use request get information about the application like the following:
String path = request.getContextPath();
String basePath = request.getScheme()+“://”+request.getServerName()+“:”+request.getServerPort()+path+“/”;

so how can i do this in itmill

Step 1: register a new transactionListener in your Application class’ init:


public class MyApplication extends Application implements com.itmill.toolkit.service.ApplicationContext.TransactionListener {

    public void init() {
        getContext().addTransactionListener(this);
    }
}

Step 2: implement the interface methods:


    public void transactionStart(Application application, Object transactionData) {
        request = (HttpServletRequest) transactionData;
    }

    public void transactionEnd(Application application, Object transactionData) {
        request = null;
    }

thnx for ur reply
but how can i access the request from any other class for example a class extends VerticalLayout should i implement this interface?

also i’ve another question
in a login window i’ve tried to store the user information using this statments:
List user = new ArrayList();
user.add((String) login.getValue());
user.add((String) password.getValue());
getApplication().setUser(user);

but when i tried to get this information from any other classes through:
getApplication().getUser(user);
it return null so how i can solve this?
thanx

There are many ways to do this, one way is to make the VerticalLayout implement TransactionListener and add it as a transaction listener. Another would be to keep the listener in the application and call some method in the layout (myLayout.doStuff()).

I must point out, though, that there is seldom a need to access the request, and doing so in a VerticalLayout sounds, well, unusual. In your case this might be a perfectly acceptable solution though, I’m just trying to give ‘food for thought’ :wink:

There is no getUser(user) only getUser().
If you’re calling getApplication().getUser() there are two possibilities:

  1. If you’re getting a NullPointerException, getApplication() is returning null. This means your component does not know which application it belongs to. This happens if you call getApplication() in the constructor of your Component - you must addComponent() the component before it knows the application.
  2. If getUser() returns null - this probably means there is something wrong with the order of the calls, i.e you somehow end up calling getUser() before setUser().

Hope this helps, and was not too confusing… Feel free to ask for clarifications :smiley:

Best Regards,
Marc

Damn, it seems that I’ve got myself stuck with this again…

I’m supposed to open the first windows of my app in Application.init, right?

What should I do to access the request in the init phase? If I register the TransactionListener in init, the init runs before the transactionStart and thus I can’t access the request before I build my window.

And if I try to register the listener earlier (in contructor), it says that the getContext is null. And based on Application.start it still wouldn’t help, because the transactionStart would still be called after App.init…

Any ideas how to get past this? Without the request I can’t personalize the first page based on user’s role…

As a workaround I don’t create the first window in init but in the transactionStart (based on a flag telling me if the app is in init phase or running normally), but could there be any side effects doing it like this?

version 5.4.0

That’s correct - init method is invoked before any transaction happens, so there is no request information in it.

If your windows depends on request and need it for initialization, then you’ll need to move your windows init code into a separate method, then call it from a transactionStart event for the first time. The short scenario might be as follows:

  1. In Application.init method you just register transaction listener

  2. Create a prvivate boolean field in your Application class that will hold UI initialization status, say: private boolean uiInitialized = false

  3. Make standalone synchronized method, say guiInit() that will perform UI initialization and windows creation as well as initialization flag toggle:


private synchronized void guiInit()
{
   if ( ! uiInitialized )
   {
      // todo: initialize and create your windows here
      uiInitialized = true;
   }
}
  1. In your transactionStart method call to guiInit().

This is all in common, hope this helps.

Best,
Dmitri