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.

CreatingAServlet3.0Application.asciidoc 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ---
  2. title: Creating A Servlet 3.0 Application
  3. order: 3
  4. layout: page
  5. ---
  6. [[creating-a-servlet-3.0-application]]
  7. = Creating a servlet 3.0 application
  8. Servlet 3.0 introduces a `@WebServlet` annotation which can be used to
  9. replace the traditional web.xml. The straightforward approach to create
  10. a Vaadin application using servlet 3.0 annotations is to simply move
  11. whatever is in web.xml to a custom servlet class (extends `VaadinServlet`)
  12. and annotate it using `@WebServlet` and add `@WebInitParams` as needed. You
  13. will end up with something like
  14. [source,java]
  15. ....
  16. @WebServlet(value = "/*", asyncSupported = true, initParams = {
  17. @WebInitParam(name = "ui", value = "com.example.MyUI"),
  18. @WebInitParam(name = "productionMode", value = "false")
  19. })
  20. public class MyServlet extends VaadinServlet {
  21. }
  22. ....
  23. The problem you will face sooner or later with both this approach as
  24. well as using web.xml is that you will misspell some parameter name or
  25. class name. Maybe you change the UI class name and the init parameter is
  26. not automatically updated - and the head scratching and debugging
  27. starts.
  28. Vaadin 7.1 introduces two features which makes this a lot easier,
  29. `@VaadinServletConfiguration` and automatic UI finding.
  30. `@VaadinServletConfiguration` is a type safe, Vaadin version of
  31. `@WebInitParam` which provides you with the option to select UI by
  32. referring the UI class directly, toggle `productionMode` using a boolean
  33. and more. The above example rewritten using `@VaadinServletConfiguration`
  34. looks like:
  35. [source,java]
  36. ....
  37. @WebServlet(value = "/*", asyncSupported = true)
  38. @VaadinServletConfiguration(productionMode = false, ui = MYUI.class)
  39. public class MyServlet extends VaadinServlet {
  40. }
  41. ....
  42. Automatic UI finding takes this even one step further and allows you to
  43. leave out `@VaadinServletConfiguration` completely if you define your
  44. servlet class as a static inner class to your UI class. The above
  45. example could therefore also be written as
  46. [source,java]
  47. ....
  48. public class MYUI extends UI {
  49. @WebServlet(value = "/*", asyncSupported = true)
  50. public static class Servlet extends VaadinServlet {
  51. }
  52. ....
  53. For clarity the variant with `@VaadinServletConfiguration` is likely the
  54. better option. Please do note that `@VaadinServletConfiguration` comes
  55. with defaults for some parameters, most importantly
  56. `legacyPropertyToStringMode`, which might be important if you are
  57. migrating an older application.