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

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