Request Handlers

    To handle requests, you need to implement the RequestHandler interface. The handleRequest() method gets the session, request, and response objects as parameters.

    In the following example, we catch requests for a sub-path in the URL for the servlet and write a plain text response. The servlet path consists of the context path and the servlet (sub-)path. Any additional path is passed to the request handler in the pathInfo of the request. For example, if the full path is /myapp/myui/rhexample, the path info will be /rhexample. Also, request parameters are available.

    A request handler can be used by embedding it in a page or opening a new page with a link or a button. In the following example, we pass some data to the handler through a session attribute.

    1. TextField dataInput = new TextField("Some data");
    2. dataInput.addValueChangeListener(event ->
    3. VaadinSession.getCurrent().setAttribute("mydata",
    4. event.getProperty().getValue()));
    5. // Determine the base path for the servlet
    6. String servletPath = VaadinServlet.getCurrent()
    7. .getServletContext().getContextPath()
    8. + "/book"; // Servlet
    9. Link open = new Link("Click to Show the Page",
    10. new ExternalResource(servletPath + "/rhexample"),
    11. "_blank", 500, 350, BorderStyle.DEFAULT);