Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

servlets - How to access `ServletContext` from within a Vaadin 7 app?

How do I access the current ServletContext from within my Vaadin 7 app?

I want to use the ServletContext object’s setAttribute, getAttribute, removeAttribute, and getAttributeNames methods to manage some global state for my Vaadin app.

Also, if using those methods for that purpose is inappropriate for Vaadin apps, please explain.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

tl;dr

For Vaadin 7 & 8, as well as Vaadin Flow (versions 10+):

VaadinServlet.getCurrent().getServletContext()

VaadinServlet

The VaadinServlet class inherits a getServletContext method.

To get the VaadinServlet object, call the static class method getCurrent.

From most anywhere within your Vaadin app, do something like this:

ServletContext servletContext = VaadinServlet.getCurrent().getServletContext();

CAVEAT
Does not work in background threads. In threads you launch, this command returns NULL. As documented:

In other cases, (e.g. from background threads started in some other way), the current servlet is not automatically defined.

@WebListener (ServletContextListener)

By the way, you are likely to want to handle such global state when the web app deploys (launches) in the container.

You can hook into your Vaadin web app’s deployment with the @WebListener annotation on your class implementing the ServletContextListener interface. Both methods of that interface, contextInitialized and contextDestroyed, are passed a ServletContextEvent from which you can access the ServletContext object by calling getServletContext.

@WebListener ( "Context listener for doing something or other." )
public class MyContextListener implements ServletContextListener
{

    // Vaadin app deploying/launching.
    @Override
    public void contextInitialized ( ServletContextEvent contextEvent )
    {
        ServletContext context = contextEvent.getServletContext();
        context.setAttribute( … ) ;
        // …
    }

    // Vaadin app un-deploying/shutting down.
    @Override
    public void contextDestroyed ( ServletContextEvent contextEvent )
    {
        ServletContext context = contextEvent.getServletContext();
        // …
    }

}

This hook is called as part of your Vaadin app being initialized, before executing the Vaadin servlet (or any other servlet/filter in your web app). To quote the doc on the contextInitialized method:

Receives notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...