How to trigger a servlet??

Hello!
I have a servlet that does some stuff.
I would like to trigger this servlet from a button.

  • Q1 how do I trigger a servlet from a button?
  • Q2 I would like to to have the servlet response displayed in the gui, how do I do that?
  • Q3 As an option I would like to have this new vaadin progressbar displaying while the servlet is working, how do I do that?

thanks in advance

Tommy Stenberg
EdgeGuide AB

Hi Tommy,

As for Q1 and Q2: I suppose, you may at least use httpclient or core http protocol support form java and make a regular http request from the server to your servlet. Then parse or do whatever you want with the response, update the UI controls of your app with the data from that response, etc. HttpClient has higher level HTTP API, so probably it will be easier to use.

Q3: As a variant: In button’s click listener, remove your button from the UI and add a progress indicator in button’s place. Then spawn a new thread which will be calling your stand-alone servlet. When that thread is finished getting data form a servlet, update the UI with that data, remove progress bar and return button back to reset the UI state. Do not forget to perform all UI values update and progress bar removal stuff being synchronized to an application instance.

As another variant, my
tpt extra package
contains a special TPTLazyLoadingLayout which automatically performs thread spawning, progress indicator manipulation and UI substitutions for such cases, so you may use it or take it as a starting/information point. You may check the
tpt online demo
(see “widgets” tab ) to see how it works.

Hi Dimitri!
Thank you very much for your quick answer!
Since I am quite new to vaadin, I am not sure if I follow you.
Maybe you can give an example of the code for the button, and how to handle the response.
I will indeed look at your examples you provided.

Happy new year.
Tommy Stenberg

Hi Tommy,

not sure, what example you’d like to see,

  • for example on how to add a click listener to a button please check the
    sampler

  • for the example on how to connect to another web page/servlet/whatever else you can check the documentation for
    Apache HttpClient
    , in short words this will look like as follows:


HttpGet httpGet = new HttpGet ( "http://www.somesite.com/page.jsp" );
HttpClient httpclient = getHttpClient ();
HttpResponse response = httpclient.execute ( httpGet );
InputStream stream = response.getEntity().getContent();

Then input stream will contain the results of called page or servlet - you can then do with this data whatever you need.

Thank you I got it now!

/Tommy Stenberg