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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. ---
  2. title: Deploying an Application
  3. order: 9
  4. layout: page
  5. ---
  6. [[application.environment]]
  7. = Deploying an Application
  8. Vaadin 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. // TODO Vaadin 7: What is the relationship between servlet and application?
  26. [[application.environment.war-eclipse]]
  27. == Creating Deployable WAR in Eclipse
  28. To deploy an application to a web server, you need to create a WAR package. Here
  29. we give the instructions for Eclipse.
  30. . Select "File > Export" and then "Web > WAR File". Or, right-click the project in the Project Explorer and select "Web > WAR File".
  31. . Select the [guilabel]#Web project# to export. Enter [guilabel]#Destination# file name ([filename]#.war#).
  32. . Make any other settings in the dialog, and click [guibutton]#Finish#.
  33. [[application.environment.war]]
  34. == Web Application Contents
  35. The following files are required in a web application in order to run it.
  36. [filename]#WEB-INF/web.xml# (optional with Servlet 3.0)::
  37. his is the web application descriptor that defines how the application is rganized, that is, what servlets and such it has.
  38. You can refer to any Java book about the contents of this file.
  39. It is not needed if you define the Vaadin servlet with the [classname]#@WebServlet# annotation in Servlet API 3.0.
  40. [filename]#WEB-INF/lib/*.jar# ::
  41. These are the Vaadin libraries and their dependencies.
  42. They can be found in the installation package or as loaded by a dependency management system such as Maven or Ivy.
  43. Your UI classes::
  44. You must include your UI classes either in a JAR file in [filename]#WEB-INF/lib# or as classes in [filename]#WEB-INF/classes#
  45. Your own theme files (OPTIONAL)::
  46. If your application uses a special theme (look and feel), you must include it in [filename]#VAADIN/themes/themename# directory.
  47. Widget sets (OPTIONAL)::
  48. If your application uses a project-specific widget set, it must be compiled in the [filename]#VAADIN/widgetset/# directory.
  49. [[application.environment.webservlet]]
  50. == Web Servlet Class
  51. When using the Servlet 3.0 API, you normally declare the Vaadin servlet classes
  52. with the [literal]#++@WebServlet++# annotation. The Vaadin UI associated with
  53. the servlet and other Vaadin-specific parameters are declared with a separate
  54. [literal]#++@VaadinServletConfiguration++# annotation.
  55. [subs="normal"]
  56. ----
  57. @WebServlet(value = "**/++*++**",
  58. asyncSupported = true)
  59. @VaadinServletConfiguration(
  60. productionMode = **false**,
  61. ui = **MyProjectUI**.class)
  62. public class **MyProjectServlet** extends VaadinServlet {
  63. }
  64. ----
  65. The Vaadin Plugin for Eclipse creates the servlet class as a static inner class
  66. of the UI class. Normally, you may want to have it as a separate regular class.
  67. The [parameter]#value# parameter is the URL pattern for mapping request URLs to
  68. the servlet, as described in <<application.environment.servlet-mapping>>. The
  69. [parameter]#ui# parameter is the UI class. Production mode is disabled by
  70. default, which enabled on-the-fly theme compilation, debug window, and other
  71. such development features. See the subsequent sections for details on the
  72. different servlet and Vaadin configuration parameters.
  73. You can also use a [filename]#web.xml# deployment descriptor in Servlet 3.0
  74. projects.
  75. [[application.environment.web-xml]]
  76. == Using a [filename]#web.xml# Deployment Descriptor
  77. A deployment descriptor is an XML file with the name [filename]#web.xml# in the
  78. [filename]#WEB-INF# sub-directory of a web application. It is a standard
  79. component in Java EE describing how a web application should be deployed. The
  80. descriptor is not required with Servlet API 3.0, where you can also define
  81. servlets with the [classname]#@WebServlet# annotation as decribed earlier, as
  82. web fragments, or programmatically. You can use both a [filename]#web.xml# and
  83. WebServlet in the same application. Settings in the [filename]#web.xml# override
  84. the ones given in annotations.
  85. The following example shows the basic contents of a deployment descriptor for a
  86. Servlet 2.4 application. 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="2.4"
  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/j2ee
  97. http://java.sun.com/xml/ns/j2ee/web-app_2_4.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.MyWidgetSet##&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.servlet]]
  125. === Servlet API Version
  126. The descriptor example given above was for Servlet 2.4. For a later version,
  127. such as Servlet 3.0, you should use:
  128. [subs="normal"]
  129. ----
  130. &lt;web-app
  131. id="WebApp_ID" version="**3.0**"
  132. xmlns="http://java.sun.com/xml/ns/j2ee"
  133. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  134. xsi:schemaLocation="**http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd**"&gt;
  135. ----
  136. Servlet 3.0 support is useful for at least server push.
  137. [[application.environment.web-xml.widgetset]]
  138. === Widget Set
  139. If the UI uses add-on components or custom widgets, it needs a custom widget
  140. set, which can be specified with the [parameter]#widgetset# parameter for the
  141. servlet. Alternatively, you can defined it with the [classname]#@WidgetSet#
  142. annotation for the UI class. The parameter is a class name with the same path
  143. but without the [filename]#.gwt.xml# extension as the widget set definition
  144. file. If the parameter is not given, the
  145. [classname]#com.vaadin.DefaultWidgetSet# is used, which contains all the widgets
  146. for the built-in Vaadin components.
  147. Unless using the default widget set (which is included in the
  148. [filename]#vaadin-client-compiled# JAR), the widget set must be compiled, as
  149. described in
  150. <<dummy/../../../framework/addons/addons-overview.asciidoc#addons.overview,"Using
  151. Vaadin Add-ons">> or
  152. <<dummy/../../../framework/clientside/clientside-compiling#clientside.compiling,"Compiling
  153. a Client-Side Module">>, and properly deployed with the application.
  154. [[application.environment.servlet-mapping]]
  155. == Servlet Mapping with URL Patterns
  156. The servlet needs to be mapped to an URL path, which requests it is to handle.
  157. With [classname]#@WebServlet# annotation for the servlet class:
  158. [subs="normal"]
  159. ----
  160. @WebServlet(value = "**/++*++**", asyncSupported = true)
  161. ----
  162. In a [filename]#web.xml#:
  163. [subs="normal"]
  164. ----
  165. &lt;servlet-mapping&gt;
  166. &lt;servlet-name&gt;**myservlet**&lt;/servlet-name&gt;
  167. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  168. &lt;/servlet-mapping&gt;
  169. ----
  170. The URL pattern is defined in the above examples as [literal]#++/*++#. This
  171. matches any URL under the project context. We defined above the project context
  172. as [literal]#++myproject++# so the URL for the page of the UI will be
  173. http://localhost:8080/myproject/.
  174. [[application.environment.servlet-mapping.sub-paths]]
  175. === Mapping Sub-Paths
  176. If an application has multiple UIs or servlets, they have to be given different
  177. paths in the URL, matched by a different URL pattern. Also, you may need to have
  178. statically served content under some path. Having an URL pattern
  179. [literal]#++/myui/*++# would match a URL such as
  180. http://localhost:8080/myproject/myui/. Notice that the slash and the asterisk
  181. __must__ be included at the end of the pattern. In such case, you also need to
  182. map URLs with [literal]#++/VAADIN/*++# to a servlet (unless you are serving it
  183. statically as noted below).
  184. With a [classname]#@WebServlet# annotation for a servlet class, you can define
  185. multiple mappings as a list enclosed in curly braces as follows:
  186. [subs="normal"]
  187. ----
  188. @WebServlet(value = {"**/myui/++*++**", "/VAADIN/*"},
  189. asyncSupported = true)
  190. ----
  191. In a [filename]#web.xml#:
  192. [subs="normal"]
  193. ----
  194. ...
  195. &lt;servlet-mapping&gt;
  196. &lt;servlet-name&gt;**myservlet**&lt;/servlet-name&gt;
  197. &lt;url-pattern&gt;**/myui/++*++**&lt;/url-pattern&gt;
  198. &lt;/servlet-mapping&gt;
  199. &lt;servlet-mapping&gt;
  200. &lt;servlet-name&gt;**myservlet**&lt;/servlet-name&gt;
  201. &lt;url-pattern&gt;/VAADIN/*&lt;/url-pattern&gt;
  202. &lt;/servlet-mapping&gt;
  203. ----
  204. If you have multiple servlets, you should specify only one
  205. [literal]#++/VAADIN/*++# mapping.It does not matter which servlet you map the
  206. pattern to, as long as it is a Vaadin servlet.
  207. You do not have to provide the above [literal]#++/VAADIN/*++# mapping if you
  208. serve both the widget sets and (custom and default) themes statically in the
  209. [filename]#/VAADIN# directory in the web application. The mapping simply allows
  210. serving them dynamically from the Vaadin JAR. Serving them statically is
  211. recommended for production environments as it is faster. If you serve the
  212. content from within the same web application, you may not have the root pattern
  213. [literal]#++/*++# for the Vaadin servlet, as then all the requests would be
  214. mapped to the servlet.
  215. [[application.environment.parameters]]
  216. == Other Servlet Configuration Parameters
  217. The servlet class or deployment descriptor can have many parameters and options
  218. that control the execution of a servlet. You can find complete documentation of
  219. the basic servlet parameters in the appropriate
  220. link:http://wiki.apache.org/tomcat/Specifications[Java Servlet Specification].
  221. ////
  222. JCP or Oracle don't seem to have a proper index
  223. URL.
  224. ////
  225. [classname]#@VaadinServletConfiguration# accepts a number of special parameters,
  226. as described below.
  227. In a [filename]#web.xml#, you can set most parameters either as a
  228. [literal]#++<context-param>++# for the entire web application, in which case
  229. they apply to all Vaadin servlets, or as an [literal]#++<init-param>++# for an
  230. individual servlet. If both are defined, servlet parameters override context
  231. parameters.
  232. [[application.environment.parameters.production-mode]]
  233. === Production Mode
  234. By default, Vaadin applications run in __debug mode__ (or __development mode__),
  235. which should be used during development. This enables various debugging
  236. features. For production use, you should have the
  237. [literal]#++productionMode=true++# setting in the
  238. [classname]#@VaadinServletConfiguration#, or in [filename]#web.xml#:
  239. ----
  240. <context-param>
  241. <param-name>productionMode</param-name>
  242. <param-value>true</param-value>
  243. <description>Vaadin production mode</description>
  244. </context-param>
  245. ----
  246. The parameter and the debug and production modes are described in more detail in
  247. <<dummy/../../../framework/advanced/advanced-debug#advanced.debug,"Debug Mode
  248. and Window">>.
  249. [[application.environment.parameters.uiprovider]]
  250. === Custom UI Provider
  251. Vaadin normally uses the [classname]#DefaultUIProvider# for creating
  252. [classname]#UI# class instances. If you need to use a custom UI provider, you
  253. can define its class with the [parameter]#UIProvider# parameter. The provider is
  254. registered in the [classname]#VaadinSession#.
  255. In a [filename]#web.xml#:
  256. [subs="normal"]
  257. ----
  258. &lt;servlet&gt;
  259. ...
  260. &lt;init-param&gt;
  261. &lt;param-name&gt;UIProvider&lt;/param-name&gt;
  262. &lt;param-value&gt;**com.ex.my.MyUIProvider**&lt;/param-value&gt;
  263. &lt;/init-param&gt;
  264. ----
  265. The parameter is logically associated with a particular servlet, but can be
  266. defined in the context as well.
  267. [[application.environment.parameters.heartbeat]]
  268. === UI Heartbeat
  269. Vaadin monitors UIs by using a heartbeat, as explained in
  270. <<dummy/../../../framework/application/application-lifecycle#application.lifecycle.ui-expiration,"UI
  271. Expiration">>. If the user closes the browser window of a Vaadin application or
  272. navigates to another page, the Client-Side Engine running in the page stops
  273. sending heartbeat to the server, and the server eventually cleans up the
  274. [classname]#UI# instance.
  275. The interval of the heartbeat requests can be specified in seconds with the
  276. [parameter]#heartbeatInterval# parameter either as a context parameter for the
  277. entire web application or an init parameter for the individual servlet. The
  278. default value is 300 seconds (5 minutes).
  279. In a [filename]#web.xml#:
  280. ----
  281. <context-param>
  282. <param-name>heartbeatInterval</param-name>
  283. <param-value>300</param-value>
  284. </context-param>
  285. ----
  286. [[application.environment.parameters.session-timeout]]
  287. === Session Timeout After User Inactivity
  288. In normal servlet operation, the session timeout defines the allowed time of
  289. inactivity after which the server should clean up the session. The inactivity is
  290. measured from the last server request. Different servlet containers use varying
  291. defaults for timeouts, such as 30 minutes for Apache Tomcat. You can set the
  292. timeout under [literal]#++<web-app>++# with:
  293. In a [filename]#web.xml#:
  294. ((("session-timeout")))
  295. ----
  296. <session-config>
  297. <session-timeout>30</session-timeout>
  298. </session-config>
  299. ----
  300. ((("Out of
  301. Sync")))
  302. The session timeout should be longer than the heartbeat interval or otherwise
  303. sessions are closed before the heartbeat can keep them alive. As the session
  304. expiration leaves the UIs in a state where they assume that the session still
  305. exists, this would cause an Out Of Sync error notification in the browser.
  306. ((("closeIdleSessions")))
  307. However, having a shorter heartbeat interval than the session timeout, which is
  308. the normal case, prevents the sessions from expiring. If the
  309. [parameter]#closeIdleSessions# parameter for the servlet is enabled (disabled by
  310. default), Vaadin closes the UIs and the session after the time specified in the
  311. [parameter]#session-timeout# parameter expires after the last non-heartbeat
  312. request.
  313. In a [filename]#web.xml#:
  314. ----
  315. <servlet>
  316. ...
  317. <init-param>
  318. <param-name>closeIdleSessions</param-name>
  319. <param-value>true</param-value>
  320. </init-param>
  321. ----
  322. [[application.environment.parameters.push]]
  323. === Push Mode
  324. You can enable server push, as described in
  325. <<dummy/../../../framework/advanced/advanced-push#advanced.push,"Server Push">>,
  326. for a UI either with a [classname]#@Push# annotation for the UI or in the
  327. descriptor. The push mode is defined with a [parameter]#pushmode# parameter. The
  328. [literal]#++automatic++# mode pushes changes to the browser automatically after
  329. __access()__ finishes. With [literal]#++manual++# mode, you need to do the push
  330. explicitly with [methodname]#push()#. If you use a Servlet 3.0 compatible
  331. server, you also want to enable asynchronous processing with the
  332. [literal]#++async-supported++# parameter.
  333. In a [filename]#web.xml#:
  334. [subs="normal"]
  335. ----
  336. &lt;servlet&gt;
  337. ...
  338. &lt;init-param&gt;
  339. &lt;param-name&gt;pushmode&lt;/param-name&gt;
  340. &lt;param-value&gt;**automatic**&lt;/param-value&gt;
  341. &lt;/init-param&gt;
  342. &lt;async-supported&gt;**true**&lt;/async-supported&gt;
  343. ----
  344. [[application.environment.parameters.xsrf]]
  345. === Cross-Site Request Forgery Prevention
  346. Vaadin uses a protection mechanism to prevent malicious cross-site request
  347. forgery (XSRF or CSRF), also called one-click attacks or session riding, which
  348. is a security exploit for executing unauthorized commands in a web server. This
  349. protection is normally enabled. However, it prevents some forms of testing of
  350. Vaadin applications, such as with JMeter. In such cases, you can disable the
  351. protection by setting the [parameter]#disable-xsrf-protection# parameter to
  352. [literal]#++true++#.
  353. In a [filename]#web.xml#:
  354. ----
  355. <context-param>
  356. <param-name>disable-xsrf-protection</param-name>
  357. <param-value>true</param-value>
  358. </context-param>
  359. ----
  360. [[application.environment.configuration]]
  361. == Deployment Configuration
  362. The Vaadin-specific parameters defined in the deployment configuration are
  363. available from the [classname]#DeploymentConfiguration# object managed by the
  364. [classname]#VaadinSession#.
  365. [source, java]
  366. ----
  367. DeploymentConfiguration conf =
  368. getSession().getConfiguration();
  369. // Heartbeat interval in seconds
  370. int heartbeatInterval = conf.getHeartbeatInterval();
  371. ----
  372. Parameters defined in the Java Servlet definition, such as the session timeout,
  373. are available from the low-level [classname]#HttpSession# or
  374. [classname]#PortletSession# object, which are wrapped in a
  375. [classname]#WrappedSession# in Vaadin. You can access the low-level session
  376. wrapper with [methodname]#getSession()# of the [classname]#VaadinSession#.
  377. [source, java]
  378. ----
  379. WrappedSession session = getSession().getSession();
  380. int sessionTimeout = session.getMaxInactiveInterval();
  381. ----
  382. You can also access other [classname]#HttpSession# and
  383. [classname]#PortletSession# session properties through the interface, such as
  384. set and read session attributes that are shared by all servlets belonging to a
  385. particular servlet or portlet session.