You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by Andrei <gm...@gmail.com> on 2010/07/12 00:23:38 UTC

init on servlet level

For GAE, if I have stand alone servlet, I would use
Where do I initialize variables (like cache here) per servlet instance
with Click?
Thanks

  // --------------------------------
  public void init() throws ServletException {

    try {
      CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
      cache = cacheFactory.createCache(Collections.emptyMap());
    } catch (CacheException e) {
      log.severe( "could not init Cache" );
    }

  }

Re: init on servlet level

Posted by Andrei <gm...@gmail.com>.
Bob,
I had static variable before, will try ServletContextListener
Thanks

On Sun, Jul 11, 2010 at 7:49 PM, Bob Schellink <sa...@gmail.com> wrote:
> On 12/07/2010 09:16, Andrei wrote:
>>
>> I don't understand
>> How would I initialize cache (for GAE Memcache) variable once per
>> servlet creation
>
>
> So you want two caches? One for each Servlet or just a single global cache?
>
>
> so I can use it in my Page?
>> From what I understand new Page is created per each request.
>
> Why not store the cache as an attribute in the ServletContext? Or even a static variable? Another
> common approach is to use a Filter and a ThreadLocal. For each request you can inject the cache into
> the ThreadLocal and remove it afterwards. And your page can still access the cache from the
> ThreadLocal in a static way.
>
> Kind regards
>
> Bob
>

Re: init on servlet level

Posted by Bob Schellink <sa...@gmail.com>.
On 12/07/2010 09:16, Andrei wrote:
> 
> I don't understand
> How would I initialize cache (for GAE Memcache) variable once per
> servlet creation 


So you want two caches? One for each Servlet or just a single global cache?


so I can use it in my Page?
> From what I understand new Page is created per each request.

Why not store the cache as an attribute in the ServletContext? Or even a static variable? Another
common approach is to use a Filter and a ThreadLocal. For each request you can inject the cache into
the ThreadLocal and remove it afterwards. And your page can still access the cache from the
ThreadLocal in a static way.

Kind regards

Bob

Re: init on servlet level

Posted by Andrei <gm...@gmail.com>.
Thanks Bob

I don't understand
How would I initialize cache (for GAE Memcache) variable once per
servlet creation so I can use it in my Page?
>From what I understand new Page is created per each request.

On Sun, Jul 11, 2010 at 7:01 PM, Bob Schellink <sa...@gmail.com> wrote:
> On 12/07/2010 08:23, Andrei wrote:
>> For GAE, if I have stand alone servlet, I would use
>> Where do I initialize variables (like cache here) per servlet instance
>> with Click?
>
>
> Yep, you can use the standalone servlet or a ServletContextListener for initialization.
>
> Kind regards
>
> Bob
>

Re: init on servlet level

Posted by Bob Schellink <sa...@gmail.com>.
On 12/07/2010 08:23, Andrei wrote:
> For GAE, if I have stand alone servlet, I would use
> Where do I initialize variables (like cache here) per servlet instance
> with Click?


Yep, you can use the standalone servlet or a ServletContextListener for initialization.

Kind regards

Bob