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.

application-environment.asciidoc 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. ---
  2. title: Deploying an Application
  3. order: 9
  4. layout: page
  5. ---
  6. [[application.environment]]
  7. = Deploying an Application
  8. Vaadin Framework applications are deployed as __Java web applications__, which can contain
  9. a number of servlets, each of which can be a Vaadin application or some other
  10. servlet, and static resources such as HTML files. Such a web application is
  11. normally packaged as a WAR (Web application ARchive) file, which can be deployed
  12. to a Java application server (or a servlet container to be exact). A WAR file,
  13. which has the [filename]#.war# extension, is a subtype of JAR (Java ARchive),
  14. and like a regular JAR, is a ZIP-compressed file with a special content
  15. structure.
  16. For a detailed tutorial on how web applications are packaged, please refer to
  17. any Java book that discusses Java Servlets.
  18. In the Java Servlet parlance, a "web application" means a collection of Java
  19. servlets or portlets, JSP and static HTML pages, and various other resources
  20. that form an application. Such a Java web application is typically packaged as a
  21. WAR package for deployment. Server-side Vaadin UIs run as servlets within such a
  22. Java web application. There exists also other kinds of web applications. To
  23. avoid confusion with the general meaning of "web application", we often refer to
  24. Java web applications with the slight misnomer "WAR" in this book.
  25. [[application.environment.war-eclipse]]
  26. == Creating Deployable WAR in Eclipse
  27. To deploy an application to a web server, you need to create a WAR package. Here
  28. we give the instructions for Eclipse.
  29. . Select "File > Export" and then "Web > WAR File". Or, right-click the project in the Project Explorer and select "Web > WAR File".
  30. . Select the [guilabel]#Web project# to export. Enter [guilabel]#Destination# file name ([filename]#.war#).
  31. . Make any other settings in the dialog, and click [guibutton]#Finish#.
  32. [[application.environment.war]]
  33. == Web Application Contents
  34. The following files are required in a web application in order to run it.
  35. [filename]#WEB-INF/web.xml# (optional with Servlet 3.0)::
  36. This is the web application descriptor that defines how the application is organized, that is, what servlets and such it has.
  37. You can refer to any Java book about the contents of this file.
  38. It is not needed if you define the Vaadin servlet with the [classname]#@WebServlet# annotation in Servlet API 3.0.
  39. [filename]#WEB-INF/lib/*.jar# ::
  40. These are the Vaadin libraries and their dependencies.
  41. They can be found in the installation package or as loaded by a dependency management system such as Maven.
  42. Your UI classes::
  43. You must include your UI classes either in a JAR file in [filename]#WEB-INF/lib# or as classes in [filename]#WEB-INF/classes#
  44. Your own theme files (OPTIONAL)::
  45. If your application uses a special theme (look and feel), you must include it in [filename]#VAADIN/themes/themename# directory.
  46. Widget sets (OPTIONAL)::
  47. If your application uses add-ons or custom widgets, they must be compiled to the [filename]#VAADIN/widgetset/# directory.
  48. When using add-ons, this is done automatically in Maven projects.
  49. See <<../addons/addons-maven#addons.maven, "Using Add-ons in a Maven Project">> for more information.
  50. [[application.environment.webservlet]]
  51. == Web Servlet Class
  52. When using the Servlet 3.0 API, you normally declare the Vaadin servlet classes
  53. with the [literal]#++@WebServlet++# annotation. The Vaadin UI associated with
  54. the servlet and other Vaadin-specific parameters are declared with a separate
  55. [literal]#++@VaadinServletConfiguration++# annotation.
  56. [subs="normal"]
  57. ----
  58. @WebServlet(value = "**/++*++**",
  59. asyncSupported = true)
  60. @VaadinServletConfiguration(
  61. productionMode = **false**,
  62. ui = **MyProjectUI**.class)
  63. public class **MyProjectServlet** extends VaadinServlet {
  64. }
  65. ----
  66. The Vaadin Plugin for Eclipse creates the servlet class as a static inner class
  67. of the UI class. Normally, you may want to have it as a separate regular class.
  68. The [parameter]#value# parameter is the URL pattern for mapping request URLs to
  69. the servlet, as described in <<application.environment.servlet-mapping>>. The
  70. [parameter]#ui# parameter is the UI class. Production mode is disabled by
  71. default, which enabled on-the-fly theme compilation, debug window, and other
  72. such development features. See the subsequent sections for details on the
  73. different servlet and Vaadin configuration parameters.
  74. You can also use a [filename]#web.xml# deployment descriptor in Servlet 3.0
  75. projects.
  76. [[application.environment.web-xml]]
  77. == Using a [filename]#web.xml# Deployment Descriptor
  78. A deployment descriptor is an XML file with the name [filename]#web.xml# in the
  79. [filename]#WEB-INF# sub-directory of a web application. It is a standard
  80. component in Java EE describing how a web application should be deployed. The
  81. descriptor is not required with Servlet API 3.0, where you can also define
  82. servlets with the [classname]#@WebServlet# annotation as decribed earlier, as
  83. web fragments, or programmatically. You can use both a [filename]#web.xml# and
  84. WebServlet in the same application. Settings in the [filename]#web.xml# override
  85. the ones given in annotations.
  86. The following example shows the basic contents of a deployment descriptor. You simply specify the UI class with the
  87. [parameter]#UI# parameter for the [classname]#com.vaadin.server.VaadinServlet#.
  88. The servlet is then mapped to a URL path in a standard way for Java Servlets.
  89. [subs="verbatim,replacements,quotes"]
  90. ----
  91. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  92. &lt;web-app
  93. id="WebApp_ID" version="**3.0**"
  94. xmlns="http://java.sun.com/xml/ns/j2ee"
  95. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  96. xsi:schemaLocation="**http://java.sun.com/xml/ns/javaee
  97. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd**"&gt;
  98. &lt;servlet&gt;
  99. &lt;servlet-name&gt;[replaceable]##myservlet##&lt;/servlet-name&gt;
  100. &lt;servlet-class&gt;
  101. [replaceable]##com.vaadin.server.VaadinServlet##
  102. &lt;/servlet-class&gt;
  103. &lt;init-param&gt;
  104. &lt;param-name&gt;UI&lt;/param-name&gt;
  105. &lt;param-value&gt;[replaceable]##com.ex.myprj.MyUI##&lt;/param-value&gt;
  106. &lt;/init-param&gt;
  107. &lt;!-- If not using the default widget set--&gt;
  108. &lt;init-param&gt;
  109. &lt;param-name&gt;widgetset&lt;/param-name&gt;
  110. &lt;param-value&gt;[replaceable]##com.ex.myprj.AppWidgetSet##&lt;/param-value&gt;
  111. &lt;/init-param&gt;
  112. &lt;/servlet&gt;
  113. &lt;servlet-mapping&gt;
  114. &lt;servlet-name&gt;[replaceable]##myservlet##&lt;/servlet-name&gt;
  115. &lt;url-pattern&gt;[replaceable]##/*##&lt;/url-pattern&gt;
  116. &lt;/servlet-mapping&gt;
  117. &lt;/web-app&gt;
  118. ----
  119. The descriptor defines a servlet with the name [filename]#myservlet#. The
  120. servlet class, [classname]#com.vaadin.server.VaadinServlet#, is provided by
  121. Vaadin framework and is normally the same for all Vaadin projects. For some
  122. purposes, you may need to use a custom servlet class that extends the
  123. [classname]#VaadinServlet#. The class name must include the full package path.
  124. [[application.environment.web-xml.widgetset]]
  125. === Widget Set
  126. The widget set is normally defined and compiled automatically in Maven projects.
  127. It may be necessary to define it manually in some cases, such as when developing custom widgets or if you need to include special rules in the widget set definition file ([filename]#.gwt.xml# module descriptor).
  128. The widget set of a UI can be defined with the [classname]#@WidgetSet# annotation for the UI class.
  129. [source, Java, subs="normal"]
  130. ----
  131. @WidgetSet("[replaceable]#com.example.myproject.MyWidgetSet#")
  132. class MyUI extends UI {
  133. ...
  134. ----
  135. You can also define it with the [parameter]#widgetset# init parameter for the servlet.
  136. The name of a widget set is technically a Java class name with the same path as the widget set definition file, but without the [filename]#.gwt.xml# extension.
  137. If a widget set is not specified, the default is used.
  138. In a project that does not use add-ons or custom widgets, the [classname]#com.vaadin.DefaultWidgetSet# is used.
  139. It contains all the widgets for the built-in Vaadin components.
  140. When using add-ons, the Vaadin Maven Plugin automatically defines an [classname]#AppWidgetSet# that includes all the add-on widget sets.
  141. The widget set must be compiled, as described in <<../addons/addons-overview.asciidoc#addons.overview,"Using Vaadin Add-ons">> (for add-ons) or <<../clientside/clientside-compiling#clientside.compiling,"Compiling a Client-Side Module">> (for custom widgets and client-side modules), and properly deployed with the application.
  142. [[application.environment.servlet-mapping]]
  143. == Servlet Mapping with URL Patterns
  144. The servlet needs to be mapped to an URL path, which requests it is to handle.
  145. With [classname]#@WebServlet# annotation for the servlet class:
  146. [subs="normal"]
  147. ----
  148. @WebServlet(value = "**/++*++**", asyncSupported = true)
  149. ----
  150. The URL pattern is defined in the above examples as [literal]#++/*++#. This
  151. matches any URL under the project context. We defined above the project context
  152. as [literal]#++myproject++# so the URL for the page of the UI will be
  153. http://localhost:8080/myproject/.
  154. [[application.environment.servlet-mapping.sub-paths]]
  155. === Mapping Sub-Paths
  156. If an application has multiple UIs or servlets, they have to be given different
  157. paths in the URL, matched by a different URL pattern. Also, you may need to have
  158. statically served content under some path. Having an URL pattern
  159. [literal]#++/myui/*++# would match a URL such as
  160. http://localhost:8080/myproject/myui/. Notice that the slash and the asterisk
  161. __must__ be included at the end of the pattern. In such case, you also need to
  162. map URLs with [literal]#++/VAADIN/*++# to a servlet (unless you are serving it
  163. statically as noted below).
  164. With a [classname]#@WebServlet# annotation for a servlet class, you can define
  165. multiple mappings as a list enclosed in curly braces as follows:
  166. [subs="normal"]
  167. ----
  168. @WebServlet(value = {"**/myui/++*++**", "/VAADIN/*"},
  169. asyncSupported = true)
  170. ----
  171. If you have multiple servlets, you should specify only one
  172. [literal]#++/VAADIN/*++# mapping.It does not matter which servlet you map the
  173. pattern to, as long as it is a Vaadin servlet.
  174. You do not have to provide the above [literal]#++/VAADIN/*++# mapping if you
  175. serve both the widget sets and (custom and default) themes statically in the
  176. [filename]#/VAADIN# directory in the web application. The mapping simply allows
  177. serving them dynamically from the Vaadin JAR. Serving them statically is
  178. recommended for production environments as it is faster. If you serve the
  179. content from within the same web application, you may not have the root pattern
  180. [literal]#++/*++# for the Vaadin servlet, as then all the requests would be
  181. mapped to the servlet.
  182. [[application.environment.parameters]]
  183. == Other Servlet Configuration Parameters
  184. The servlet class or deployment descriptor can have many parameters and options
  185. that control the execution of a servlet. You can find complete documentation of
  186. the basic servlet parameters in the appropriate
  187. link:http://wiki.apache.org/tomcat/Specifications[Java Servlet Specification].
  188. ////
  189. JCP or Oracle don't seem to have a proper index
  190. URL.
  191. ////
  192. [classname]#@VaadinServletConfiguration# accepts a number of special parameters,
  193. as described below.
  194. In a [filename]#web.xml#, you can set most parameters either as a
  195. [literal]#++<context-param>++# for the entire web application, in which case
  196. they apply to all Vaadin servlets, or as an [literal]#++<init-param>++# for an
  197. individual servlet. If both are defined, servlet parameters override context
  198. parameters.
  199. [[application.environment.parameters.production-mode]]
  200. === Production Mode
  201. By default, Vaadin applications run in __debug mode__ (or __development mode__),
  202. which should be used during development. This enables various debugging
  203. features. For production use, you should have the
  204. [literal]#++productionMode=true++# setting in the
  205. [classname]#@VaadinServletConfiguration#.
  206. The parameter and the debug and production modes are described in more detail in
  207. <<../advanced/advanced-debug#advanced.debug,"Debug Mode
  208. and Window">>.
  209. [[application.environment.parameters.uiprovider]]
  210. === Custom UI Provider
  211. Vaadin normally uses the [classname]#DefaultUIProvider# for creating
  212. [classname]#UI# class instances. If you need to use a custom UI provider, you
  213. can define its class with the [parameter]#UIProvider# parameter. The provider is
  214. registered in the [classname]#VaadinSession#.
  215. The parameter is logically associated with a particular servlet, but can be
  216. defined in the context as well.
  217. [[application.environment.parameters.heartbeat]]
  218. === UI Heartbeat
  219. Vaadin monitors UIs by using a heartbeat, as explained in
  220. <<application-lifecycle#application.lifecycle.ui-expiration,"UI
  221. Expiration">>. If the user closes the browser window of a Vaadin application or
  222. navigates to another page, the Client-Side Engine running in the page stops
  223. sending heartbeat to the server, and the server eventually cleans up the
  224. [classname]#UI# instance.
  225. The interval of the heartbeat requests can be specified in seconds with the
  226. [parameter]#heartbeatInterval# parameter either as a context parameter for the
  227. entire web application or an init parameter for the individual servlet. The
  228. default value is 300 seconds (5 minutes).
  229. [[application.environment.parameters.session-timeout]]
  230. === Session Timeout After User Inactivity
  231. In normal servlet operation, the session timeout defines the allowed time of
  232. inactivity after which the server should clean up the session. The inactivity is
  233. measured from the last server request. Different servlet containers use varying
  234. defaults for timeouts, such as 30 minutes for Apache Tomcat. There is no way to
  235. programmatically set the global session timeout, but you can set it in the
  236. deployment descriptor with:
  237. ((("session-timeout")))
  238. ----
  239. <session-config>
  240. <session-timeout>30</session-timeout>
  241. </session-config>
  242. ----
  243. ((("Out of
  244. Sync")))
  245. The session timeout should be longer than the heartbeat interval or otherwise
  246. sessions are closed before the heartbeat can keep them alive. As the session
  247. expiration leaves the UIs in a state where they assume that the session still
  248. exists, this would cause an Out Of Sync error notification in the browser.
  249. ((("closeIdleSessions")))
  250. However, having a shorter heartbeat interval than the session timeout, which is
  251. the normal case, prevents the sessions from expiring. If the
  252. [parameter]#closeIdleSessions# parameter for the servlet is enabled (disabled by
  253. default), Vaadin closes the UIs and the session after the time specified in the
  254. [parameter]#session-timeout# init parameter expires after the last non-heartbeat
  255. request.
  256. [[application.environment.parameters.push]]
  257. === Push Mode
  258. You can enable server push, as described in
  259. <<../advanced/advanced-push#advanced.push,"Server Push">>,
  260. for a UI either with a [classname]#@Push# annotation for the UI or in the
  261. descriptor. The push mode is defined with a [parameter]#pushmode# init parameter. The
  262. [literal]#++automatic++# mode pushes changes to the browser automatically after
  263. __access()__ finishes. With [literal]#++manual++# mode, you need to do the push
  264. explicitly with [methodname]#push()#. You can enable asynchronous processing with the
  265. [literal]#++async-supported++# init parameter.
  266. [[application.environment.parameters.xsrf]]
  267. === Cross-Site Request Forgery Prevention
  268. Vaadin uses a protection mechanism to prevent malicious cross-site request
  269. forgery (XSRF or CSRF), also called one-click attacks or session riding, which
  270. is a security exploit for executing unauthorized commands in a web server. This
  271. protection is normally enabled. However, it prevents some forms of testing of
  272. Vaadin applications, such as with JMeter. In such cases, you can disable the
  273. protection by setting the [parameter]#disable-xsrf-protection# context parameter to
  274. [literal]#++true++#.
  275. [[application.environment.configuration]]
  276. == Deployment Configuration
  277. The Vaadin-specific parameters defined in the deployment configuration are
  278. available from the [classname]#DeploymentConfiguration# object managed by the
  279. [classname]#VaadinSession#.
  280. [source, java]
  281. ----
  282. DeploymentConfiguration conf =
  283. getSession().getConfiguration();
  284. // Heartbeat interval in seconds
  285. int heartbeatInterval = conf.getHeartbeatInterval();
  286. ----
  287. Parameters defined in the Java Servlet definition, such as the session timeout,
  288. are available from the low-level [classname]#HttpSession# or
  289. [classname]#PortletSession# object, which are wrapped in a
  290. [classname]#WrappedSession# in Vaadin. You can access the low-level session
  291. wrapper with [methodname]#getSession()# of the [classname]#VaadinSession#.
  292. [source, java]
  293. ----
  294. WrappedSession session = getSession().getSession();
  295. int sessionTimeout = session.getMaxInactiveInterval();
  296. ----
  297. You can also access other [classname]#HttpSession# and
  298. [classname]#PortletSession# session properties through the interface, such as
  299. set and read session attributes that are shared by all servlets belonging to a
  300. particular servlet or portlet session.