Project

General

Profile

Sample-hello-world » History » Version 10

Henning Blohm, 28.02.2021 18:51

1 2 Henning Blohm
h1. Hello World Web App Sample
2
3
TBC
4
5
This sample application shows how to set up the most basic Java Web application in z2. 
6
7
Z2 integrates with the Jetty Web container and Web applications on Z2 are really just standard Java EE Web applications. What is unfamiliar for the typical Maven, Gradle, or Ant user is how applications are developed and modularized on Z2. So this sample case is hence more about Web applications as developed on Z2 than it is about Web application development.
8
9 4 Henning Blohm
Please skip to [[#Step by Step - Or Setting Things Up Yourself|Step by Step - Or Setting Things Up Yourself]] unless you just want to see it running.
10 3 Henning Blohm
11 2 Henning Blohm
This sample is stored in the repository "z2-samples-hello-world-web":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-hello-world-web.
12
13
h2. Prerequisites
14
15
{{include(Java_Version_Requirements)}}
16
17
h2. Setting up the sample
18
19
Follow these instructions, if you just want to see it running. Otherwise we recommend skipping this section and rather follow the step by step instructions below.
20
21
Here's the really fast version:
22
23
{{include(Install_sample_prefix)}}
24
25
<pre><code>
26
git clone -b v2.8 https://www.z2-environment.net/git/z2-samples-hello-world-web
27
</code></pre>
28
29
{{include(Install_sample_postfix)}}
30
31
The first time you launch the sample, it will take a while to download all required resources.
32
33
h2. Step by Step - Or Setting Things Up Yourself
34 5 Henning Blohm
35
36
In this section we will manually create the basic structure of a Z2 module and see how it is discovered by Z2 during development.
37
38
We assume that you have prepared some development workspace install and that you installed z2-base.core, the core runtime of z2 in that folder. If you have not done so yet, run
39
40
{{include(Install_sample_prefix)}}
41
42
h3. Create a Module
43
44 7 Henning Blohm
Change into install and create a folder @z2-samples-hello-world-web@ and in that folder create the following structure
45 5 Henning Blohm
46
<pre>
47
├── java
48
│   ├── src.api
49
│   ├── src.impl
50
│   └── z.properties
51
└── web
52
    ├── WebContent
53
    │   └── WEB-INF
54
    │       └── web.xml
55
    └── z.properties
56
</pre>
57
58 6 Henning Blohm
where @java/z.properties@:
59
60 7 Henning Blohm
<pre>
61 6 Henning Blohm
com.zfabrik.component.type=com.zfabrik.java
62
63
java.privateReferences=\
64
	com.zfabrik.servletjsp
65 7 Henning Blohm
</pre>
66 6 Henning Blohm
67
and @web/z.properties@:
68
69 7 Henning Blohm
<pre>
70 6 Henning Blohm
com.zfabrik.component.type=com.zfabrik.ee.webapp
71
72
webapp.path=/hello-world
73
webapp.requiredPaths=
74
75
webapp.server=environment/webServer
76
com.zfabrik.systemStates.participation=environment/webWorkerUp
77 7 Henning Blohm
</pre>
78 6 Henning Blohm
79
and @web/WEB-INF/web.xml@:
80
81
<pre><code class="xml">
82
<?xml version="1.0" encoding="UTF-8"?>
83
84
<web-app id="z2WebApp" version="3.0"
85
	xmlns="http://java.sun.com/xml/ns/javaee"
86
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
87
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
88
</web-app>
89
</code></pre>
90 1 Henning Blohm
91
92
*Note:* If you are using the Eclipse IDE and have installed the eclipsoid plugin ([[How_to_install_Eclipsoid]]) you can use the wizard at "File > New > Other > New Java/Web z2-Project". Note however that it may be necessary to adapt the generated files to match the ones above (in particular check for @web.xml@ to refere to servlet api 3.0 at least).
93
94
This is the most essential module structure for a Web Application. You will find all Web Application resources directly under the web folder. Java resources however would be put into the java component folder, in src.impl if they are not to be re-used by (or "be visible to") other modules, in src.api, if they are not to be re-used by (and hence "not visible to") other modules.
95 7 Henning Blohm
96
h3. Let Z2 Find It
97
98
So now we have a module structure in a folder next to our z2 installation. Unfortunately so far this has no meaning for z2. That is because it is not visible through any registered component repository. Fortunately however there is the _Development Component Repository_ that allows us to dynamically publish modules on the local file system for discovery by Z2. It is configured to look for modules next to z2-base.core that are _armed_. 
99
100 8 Henning Blohm
In order to arm the new module, i.e. in order to make it visible to Z2, simple put an (empty) file LOCAL into the root of the module hierarchy. I.e. @z2-samples-hello-world-web/LOCAL@. The module structure should now look like this:
101
102
<pre>
103
├── java
104
│   ├── src.api
105
│   ├── src.impl
106
│   └── z.properties
107
├── LOCAL
108
└── web
109
    ├── WebContent
110
    │   └── WEB-INF
111
    │       └── web.xml
112
    └── z.properties
113
</pre>
114 7 Henning Blohm
115
To tell Z2 that there is some change, we need to "synchronize". But first, we need to start Z2. 
116
117
{{include(Install_sample_postfix)}}
118
119 10 Henning Blohm
In order to synchronize (starting includes synchronization - but note for next time): Press the !clipboard-202102281850-fm6o9.png! button on the Z2 GUI or, from eclipsoid in Eclipse or IntelliJ press the Sync button in the IDE.
120
121
In the Z2 logs you should see 
122
123
<pre>
124
...pp.WebAppResource [800]: Starting WebApp: z2-samples-hello-world-web/web
125
...pp.WebAppResource [800]: Done starting Web App (/hello-world): z2-samples-hello-world-web/web
126
</pre>
127
128
and when accessing http://localhost:8080/hello-world you should see some default Jetty provided directory listing.
129
130
Congratulations: You have your first Web app running.
131
132
h3. Add Some Logic
133
134
Before closing, let's add some real "hello world" logic.
135
136
The simplest thing to do is to add an @index.jsp@ Java Server page: So let's add @z2-samples-hello-world-web/web/WebContent/index.jsp@:
137
138
<pre><code class="xml">
139
<html>
140
<body>
141
<h1>Hello World!</h1>
142
</body>
143
</html>
144
</code></pre>
145
146
After adding the file, synchronize Z2 by either pressing the !clipboard-202102281850-fm6o9.png! button on the Z2 GUI or by using the Eclipsoid function in Eclipse or IntelliJ. 
147
148
Visiting http://localhost:8080/hello-world you would now see: