Project

General

Profile

Sample-hello-world » History » Version 18

Henning Blohm, 28.02.2021 22:48

1 2 Henning Blohm
h1. Hello World Web App Sample
2
3
This sample application shows how to set up the most basic Java Web application in z2. 
4
5 16 Henning Blohm
Z2 integrates with the Jetty Web container and Web applications on Z2 are really just standard Java EE Web applications. What will be unfamiliar for Maven, Gradle, or Ant user is how applications are developed and modularized on Z2. This sample case is hence more about Web applications as developed on Z2 than it is about Web application development.
6 2 Henning Blohm
7 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.
8 3 Henning Blohm
9 17 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 .
10 2 Henning Blohm
11
h2. Prerequisites
12
13
{{include(Java_Version_Requirements)}}
14
15
h2. Setting up the sample
16
17
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.
18
19
Here's the really fast version:
20
21
{{include(Install_sample_prefix)}}
22
23
<pre><code>
24 15 Henning Blohm
git clone -b v2.8 https://www.z2-environment.net/git/z2-samples.hello-world-web
25 2 Henning Blohm
</code></pre>
26
27
{{include(Install_sample_postfix)}}
28
29
The first time you launch the sample, it will take a while to download all required resources.
30
31
h2. Step by Step - Or Setting Things Up Yourself
32 5 Henning Blohm
33
In this section we will manually create the basic structure of a Z2 module and see how it is discovered by Z2 during development.
34
35
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
36
37
{{include(Install_sample_prefix)}}
38
39
h3. Create a Module
40
41 14 Henning Blohm
Change into install and create a folder @com.zfabrik.samples.hello-world@ and in that folder create the following structure
42 5 Henning Blohm
43
<pre>
44
├── java
45
│   ├── src.api
46
│   ├── src.impl
47
│   └── z.properties
48
└── web
49
    ├── WebContent
50
    │   └── WEB-INF
51
    │       └── web.xml
52
    └── z.properties
53
</pre>
54
55 6 Henning Blohm
where @java/z.properties@:
56
57 7 Henning Blohm
<pre>
58 6 Henning Blohm
com.zfabrik.component.type=com.zfabrik.java
59
60
java.privateReferences=\
61
	com.zfabrik.servletjsp
62 7 Henning Blohm
</pre>
63 6 Henning Blohm
64
and @web/z.properties@:
65
66 7 Henning Blohm
<pre>
67 6 Henning Blohm
com.zfabrik.component.type=com.zfabrik.ee.webapp
68
69
webapp.path=/hello-world
70
webapp.requiredPaths=
71
72
webapp.server=environment/webServer
73
com.zfabrik.systemStates.participation=environment/webWorkerUp
74 7 Henning Blohm
</pre>
75 6 Henning Blohm
76
and @web/WEB-INF/web.xml@:
77
78
<pre><code class="xml">
79
<?xml version="1.0" encoding="UTF-8"?>
80
<web-app id="z2WebApp" version="3.0"
81
	xmlns="http://java.sun.com/xml/ns/javaee"
82
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
83
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
84
</web-app>
85
</code></pre>
86 1 Henning Blohm
87
88
*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).
89
90 16 Henning Blohm
This is the most essential module structure for a Web Application: 
91 1 Henning Blohm
92 16 Henning Blohm
* You will find all Web Application resources directly under the web folder. 
93 18 Henning Blohm
* Java resources however would be put into the java component folder, 
94
** in src.impl if they are not to be re-used by (or "not be visible to") other modules and
95
** in src.api, if they are to be re-used by (and hence "possibly be visible to") other modules.
96 16 Henning Blohm
97 7 Henning Blohm
h3. Let Z2 Find It
98
99 16 Henning Blohm
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_ (see also "develop":http://www.z2-environment.eu/v28doc#develop) 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_. 
100 7 Henning Blohm
101 14 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. @com.zfabrik.samples.hello-world/LOCAL@. The module structure should now look like this:
102 8 Henning Blohm
103
<pre>
104
├── java
105
│   ├── src.api
106
│   ├── src.impl
107
│   └── z.properties
108
├── LOCAL
109
└── web
110
    ├── WebContent
111
    │   └── WEB-INF
112
    │       └── web.xml
113
    └── z.properties
114
</pre>
115 7 Henning Blohm
116
To tell Z2 that there is some change, we need to "synchronize". But first, we need to start Z2. 
117
118
{{include(Install_sample_postfix)}}
119
120 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.
121
122
In the Z2 logs you should see 
123
124
<pre>
125 14 Henning Blohm
...pp.WebAppResource [800]: Starting WebApp: com.zfabrik.samples.hello-world/web
126
...pp.WebAppResource [800]: Done starting Web App (/hello-world): com.zfabrik.samples.hello-world/web
127 10 Henning Blohm
</pre>
128
129
and when accessing http://localhost:8080/hello-world you should see some default Jetty provided directory listing.
130
131
Congratulations: You have your first Web app running.
132
133
h3. Add Some Logic
134
135
Before closing, let's add some real "hello world" logic.
136
137 14 Henning Blohm
The simplest thing to do is to add an @index.jsp@ Java Server page: So let's add @com.zfabrik.samples.hello-world/web/WebContent/index.jsp@:
138 10 Henning Blohm
139
<pre><code class="xml">
140
<html>
141
<body>
142
<h1>Hello World!</h1>
143
</body>
144
</html>
145
</code></pre>
146
147
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. 
148 1 Henning Blohm
149 10 Henning Blohm
Visiting http://localhost:8080/hello-world you will now see:
150 11 Henning Blohm
151 13 Henning Blohm
!clipboard-202102281913-sdv39.png!
152 11 Henning Blohm
153
Now. So far, this is neither running any Java code of ours nor does it even use any JSP logic. 
154
155
So, as our last enhancement, let's write a Servlet that prints "Servlet World" and include its output into @index.jsp@. Admittedly, that is not very useful, but so what.
156
157 14 Henning Blohm
As the Servlet is not supposed to be used by other modules, we will put it in @src.impl@. More specifically add  @com.zfabrik.samples.hello-world/java/src.impl/samples.hello.world.impl/WorldServlet.java@:
158 11 Henning Blohm
159
<pre><code class="java">
160
package sample.hello.world.impl;
161
162
import java.io.IOException;
163
164
import javax.servlet.ServletException;
165
import javax.servlet.annotation.WebServlet;
166
import javax.servlet.http.HttpServlet;
167
import javax.servlet.http.HttpServletRequest;
168
import javax.servlet.http.HttpServletResponse;
169
170
@SuppressWarnings("serial")
171
@WebServlet(urlPatterns = {"/WEB-INF/world"})
172
public class WorldServlet extends HttpServlet {
173
	@Override
174
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
175
		resp.getWriter().write("Servlet World");
176
	}
177
}
178
</code></pre>
179
180 14 Henning Blohm
and change @com.zfabrik.samples.hello-world/web/WebContent/index.jsp@:
181 11 Henning Blohm
182
<pre><code class="xml">
183
<html>
184
<body>
185
<h1>Hello <jsp:include page="/WEB-INF/world"/>!</h1>
186
</body>
187 1 Henning Blohm
</html>
188
</code></pre>
189
190 13 Henning Blohm
The module structure looks like this now:
191
192
<pre>
193
├── java
194
│   ├── src.api
195
│   ├── src.impl
196
│   │   └── sample
197
│   │       └── hello
198
│   │           └── world
199
│   │               └── impl
200
│   │                   └── WorldServlet.java
201
│   └── z.properties
202
├── LOCAL
203
└── web
204
    ├── WebContent
205
    │   ├── index.jsp
206
    │   └── WEB-INF
207
    │       └── web.xml
208
    └── z.properties
209
</pre>
210
211 1 Henning Blohm
Synchronizing and visiting http://localhost:8080/hello-world you will now see:
212
213 13 Henning Blohm
!clipboard-202102281913-13jtf.png!
214
215
h2. Final Note
216 12 Henning Blohm
217
We will stop here. Please have a look at other samples and the reference documentation of the current version:
218
219
{{include(Current Version)}}