best way to access application attributes from servlet

i have the following setup:

public class VaadinApp extends Application
private PoiReferenceContainer dataSource;

    public PoiReferenceContainer getDataSource() {
	return this.dataSource;

and a servlet that makes changes to some database objects, the datasource refers to.

	public class AddRating extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String poiId = request.getParameter("poi");
	String ratingString = request.getParameter("rating");
	if (poiId == null || ratingString == null) {
	    constructPage(response, "Please supply the two needed parameters");	    
	} else if ("".equals(poiId) || "".equals(ratingString)) {
	    constructPage(response, "Looks like you forgot to enter an values for at least one parameter");	    
	} else if (poiId != null && ratingString != null) {
	    double rating = Double.valueOf(ratingString);
	    double newRating = ((JPAFacade) FacadeFactory.getFacade()).addRating(poiId, rating);
	    constructPage(response, "the new rating is" + newRating);

Like in the Vaadin AddressBook Application (with JPA) I want to call refresh(); on the dataSource. What whould be the best way to access it from a servlet, as getApplication() does not work? Something static? Or is there some nice way of putting the Application into the servletContext?


Hmm… the problem is not only refreshing the server-side, but you want the client-side also to update, right?
I think you could use the HttpSession for sharing the refresh notification, and either with
, javascript refresh (vaadin.forceSync() method) or
server push
update the client-side.

See also this thread for a similar topic:

thank you for your answer, though it was not quite what i wanted to know. let me try to clarify:

i just need access to the current application reference. but i do not want it from a vaadin component, where i could easily call getApplication(), but from a servlet. How can I achive this?

The web container (Tomcat, Jetty, Glassfish…) invokes a Servlet (typically com.vaadin.terminal.gwt.server.ApplicationServlet) which creates one Application instance for each HTTP session. This is what the web.xml file specifies.

Given this situation, I can’t quite figure out what you’re asking for.

Thank you for your answer Jean-François,

i should again clarify, i am relatively new to web application development. I am talking about a regular HttpServlet. How can I access the Vaadin Application instance from a HttpServlet class?

While it is right that you can access Vaadin application from HttpSession, I’m not sure if that is the right approach - you are making your application dependent on the implementation.

Why not simply store the data into HttpSession and read it from there in your Vaadin application? Depending what you want to achieve, the database, message queue - or simply the HttpSession - is a better way of sharing data. And you can check for updated data in your Vaadin application event handling (or on every event request if needed using the

What I was referring in my previous answer was a situation that you want the UI from outside. That typically requires the changes to be immediately visible (i.e. client-side refresh).

Maybe what you wrote about Polling is the most precise solution to my problem. What I want to achieve is the following.

One of the users makes changes to one of the POJOs that is contained in a Vaadin Container. All users should immediately see these changes on their UI. What I did so far is writing a method in my Application that refreshs the Container instance. But I guess that is not the best way right? Better approaches?