How to access `ServletContext` from within a Vaadin 14 app?

放肆的年华 提交于 2020-01-16 13:05:23

问题


I am asking the same as the Question, How to access ServletContext from within a Vaadin 7 app?, but for Vaadin 14.

In Vaadin 7, Vaadin provided the simple and handy call:

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

That particular call is no longer there on VaadinServlet in Vaadin 14.

➥ Is there an equivalent somewhere else in the Vaadin API?

My end goal is to use the key-value "attribute" collection to keep references to my app-wide objects.


回答1:


Methods still there, but not documented

Both methods:

  • VaadinServlet.getCurrent() ➙ VaadinServlet
  • VaadinServlet::getServletContext() ➙ javax.servlet.ServletContext

…are there still in Vaadin 14.

It seems the second method, VaadinServlet::getServletContext(), isn't listed in the Javadocs because it's inherited from javax.servlet.GenericServlet and the Javadoc generation is run in a way that doesn't have access to that class.




回答2:


VaadinContext

For the particular case of accessing attributes from ServletContext, there's a new VaadinContext abstraction introduced in Vaadin 14. It's added to make it possible for framework internals to access context attributes without being tied to the Servlet API. It can be accessed using VaadinService.getCurrent().getContext().

VaadinContext can track attributes by their class:

  • getAttribute(Class<T> type)
  • setAttribute(T value)
  • removeAttribute(Class<?> clazz)



回答3:


Update: One of the Answers by Leif Åstrand explains both methods are actually present in Vaadin 14 but one is undocumented.

I verified that we can indeed call:

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

So the rest of my Answer here is obsolete. I will leave my Answer as a curiosity, rather than delete it.


This may not be the best way, but it seems to be working for me.

The VaadinServletService class, extending com.vaadin.flow.server.VaadinService, offers the getCurrentServletRequest(). That method returns a javax.servlet.http.HttpServletRequest object. For there we can call javax.servlet.ServletRequest.getServletContext to return the javax.servlet.ServletContext you desire.

ServletContext servletContext = 
        VaadinServletService            // com.vaadin.flow.server.VaadinServletService
        .getCurrentServletRequest()     // Returns a javax.servlet.http.HttpServletRequest
        .getServletContext()            // Returns a `javax.servlet.ServletContext`. 
;

From there you can use the key-value "attribute" collection as you mentioned. Look to the setAttribute, getAttribute, and removeAttribute methods with a String object as the key and a Object as the value.

Saving.

DataSource dataSource = … ; 
VaadinServletService.getCurrentServletRequest().getServletContext().setAttribute( "javax.sql.DataSource" , dataSource ) ;

Retrieving. The key-value collection of "attributes" keep the value as an Object, so we must cast back to the expected class/interface.

DataSource dataSource = (DataSource) VaadinServletService.getCurrentServletRequest().getServletContext().getAttribute( "javax.sql.DataSource" ) ;


来源:https://stackoverflow.com/questions/59550032/how-to-access-servletcontext-from-within-a-vaadin-14-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!