You need to understand that the phase of the portlet (which starts when you click on the menu) and the servlet request are completely separate requests.
I do not know the intricacies of Vaadin, but I assume that it is a server-side component such as JSF. Therefore, in your menuSelected method menuSelected you need to change the color of the chart (what I see, you are doing with the correction to your question). That's right, but how did pie and its data share with the servlet? This is a servlet that will use color, so you need to somehow convey it. This may be one of several methods, of which a couple is given here:
- shared session state
- URL parameter
Generally, the overall state of a session is a bad idea because it closely connects the components together. And URL parameters can be a bad option if the parameters contain sensitive data. But for your example, the URL parameters are fine.
So, after going over the information that you indicated in the comment, you use the Label component to render the <img> . You say something like:
new Label("img link",Label.CONTEXT_XHTML));
So, now you can add the URL parameters to the servlet, for example:
new Label("<img src='/path/to/servlet?background=blue'>",Label.CONTEXT_XHTML));
Thus, this URL parameter will be visible to the servlet, which can read it from the request parameters (as in the example below) and set the color to chart .
In addition to requiring user input, I would modify your code a bit. Basically, to move the JDBC driver initialization to the init() servlet method and make sure the connection is closed in the finally block. @home also makes the right comment that ideally you will use the connection pool to capture the connection.
I added a few comments to show how you take the query parameter and use it in your chart. (EDIT - and added some rudimentary logic to set the color).
public class PieChart extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { super.init(); try { Class.forName("org.postgresql.Driver").newInstance(); } catch (InstantiationException e) { throw new ServletException(e); } catch (IllegalAccessException e) { throw new ServletException(e); } catch (ClassNotFoundException e) { throw new ServletException(e); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String background = request.getParameter("background");