You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-requesthandler.asciidoc 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ---
  2. title: Request Handlers
  3. order: 4
  4. layout: page
  5. ---
  6. [[advanced.requesthandler]]
  7. = Request Handlers
  8. Request handlers are useful for catching request parameters or generating
  9. dynamic content, such as HTML, images, PDF, or other content. You can provide
  10. HTTP content also with stream resources, as described in
  11. <<../application/application-resources#application.resources.stream,"Stream
  12. Resources">>. The stream resources, however, are only usable from within a
  13. Vaadin application, such as in an [classname]#Image# component. Request handlers
  14. allow responding to HTTP requests made with the application URL, including GET
  15. or POST parameters. You could also use a separate servlet to generate dynamic
  16. content, but a request handler is associated with the user session and it can
  17. easily access data associated with the session and the user.
  18. To handle requests, you need to implement the [interfacename]#RequestHandler#
  19. interface. The [methodname]#handleRequest()# method gets the session, request,
  20. and response objects as parameters.
  21. If the handler writes a response, it must return [literal]#++true++#. This stops
  22. running other possible request handlers. Otherwise, it should return
  23. [literal]#++false++# so that another handler could return a response.
  24. Eventually, if no other handler writes a response, a UI will be created and
  25. initialized.
  26. In the following example, we catch requests for a sub-path in the URL for the
  27. servlet and write a plain text response. The servlet path consists of the
  28. context path and the servlet (sub-)path. Any additional path is passed to the
  29. request handler in the [parameter]#pathInfo# of the request. For example, if the
  30. full path is [filename]#/myapp/myui/rhexample#, the path info will be
  31. [filename]#/rhexample#. Also, request parameters are available.
  32. [source, java]
  33. ----
  34. // A request handler for generating some content
  35. VaadinSession.getCurrent().addRequestHandler(
  36. new RequestHandler() {
  37. @Override
  38. public boolean handleRequest(VaadinSession session,
  39. VaadinRequest request,
  40. VaadinResponse response)
  41. throws IOException {
  42. if ("/rhexample".equals(request.getPathInfo())) {
  43. // Generate a plain text document
  44. response.setContentType("text/plain");
  45. response.getWriter().append(
  46. "Here's some dynamically generated content.\n");
  47. response.getWriter().format(Locale.ENGLISH,
  48. "Time: %Tc\n", new Date());
  49. // Use shared session data
  50. response.getWriter().format("Session data: %s\n",
  51. session.getAttribute("mydata"));
  52. return true; // We wrote a response
  53. } else
  54. return false; // No response was written
  55. }
  56. });
  57. ----
  58. A request handler can be used by embedding it in a page or opening a new page
  59. with a link or a button. In the following example, we pass some data to the
  60. handler through a session attribute.
  61. [source, java]
  62. ----
  63. // Input some shared data in the session
  64. TextField dataInput = new TextField("Some data");
  65. dataInput.addValueChangeListener(event ->
  66. VaadinSession.getCurrent().setAttribute("mydata",
  67. event.getProperty().getValue()));
  68. dataInput.setValue("Here's some");
  69. // Determine the base path for the servlet
  70. String servletPath = VaadinServlet.getCurrent()
  71. .getServletContext().getContextPath()
  72. + "/book"; // Servlet
  73. // Display the page in a pop-up window
  74. Link open = new Link("Click to Show the Page",
  75. new ExternalResource(servletPath + "/rhexample"),
  76. "_blank", 500, 350, BorderStyle.DEFAULT);
  77. layout.addComponents(dataInput, open);
  78. ----