Project

General

Profile

Sample-hello-world » History » Version 13

Henning Blohm, 28.02.2021 19:13

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
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.
6
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 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.
10
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
git clone -b v2.8 https://www.z2-environment.net/git/z2-samples-hello-world-web
25
</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 7 Henning Blohm
Change into install and create a folder @z2-samples-hello-world-web@ 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
81
<web-app id="z2WebApp" version="3.0"
82
	xmlns="http://java.sun.com/xml/ns/javaee"
83
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
84
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
85
</web-app>
86
</code></pre>
87 1 Henning Blohm
88
89
*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).
90
91
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.
92 7 Henning Blohm
93
h3. Let Z2 Find It
94
95
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_. 
96
97 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:
98
99
<pre>
100
├── java
101
│   ├── src.api
102
│   ├── src.impl
103
│   └── z.properties
104
├── LOCAL
105
└── web
106
    ├── WebContent
107
    │   └── WEB-INF
108
    │       └── web.xml
109
    └── z.properties
110
</pre>
111 7 Henning Blohm
112
To tell Z2 that there is some change, we need to "synchronize". But first, we need to start Z2. 
113
114
{{include(Install_sample_postfix)}}
115
116 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.
117
118
In the Z2 logs you should see 
119
120
<pre>
121
...pp.WebAppResource [800]: Starting WebApp: z2-samples-hello-world-web/web
122
...pp.WebAppResource [800]: Done starting Web App (/hello-world): z2-samples-hello-world-web/web
123
</pre>
124
125
and when accessing http://localhost:8080/hello-world you should see some default Jetty provided directory listing.
126
127
Congratulations: You have your first Web app running.
128
129
h3. Add Some Logic
130
131
Before closing, let's add some real "hello world" logic.
132
133
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@:
134
135
<pre><code class="xml">
136
<html>
137
<body>
138
<h1>Hello World!</h1>
139
</body>
140
</html>
141
</code></pre>
142
143
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. 
144 1 Henning Blohm
145 10 Henning Blohm
Visiting http://localhost:8080/hello-world you will now see:
146 11 Henning Blohm
147 13 Henning Blohm
!clipboard-202102281913-sdv39.png!
148 11 Henning Blohm
149
Now. So far, this is neither running any Java code of ours nor does it even use any JSP logic. 
150
151
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.
152
153
As the Servlet is not supposed to be used by other modules, we will put it in @src.impl@. More specifically add  @z2-samples-hello-world-web/java/src.impl/samples.hello.world.impl/WorldServlet.java@:
154
155
<pre><code class="java">
156
package sample.hello.world.impl;
157
158
import java.io.IOException;
159
160
import javax.servlet.ServletException;
161
import javax.servlet.annotation.WebServlet;
162
import javax.servlet.http.HttpServlet;
163
import javax.servlet.http.HttpServletRequest;
164
import javax.servlet.http.HttpServletResponse;
165
166
@SuppressWarnings("serial")
167
@WebServlet(urlPatterns = {"/WEB-INF/world"})
168
public class WorldServlet extends HttpServlet {
169
	@Override
170
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
171
		resp.getWriter().write("Servlet World");
172
	}
173
}
174
</code></pre>
175
176
and change @z2-samples-hello-world-web/web/WebContent/index.jsp@:
177
178
<pre><code class="xml">
179
<html>
180
<body>
181
<h1>Hello <jsp:include page="/WEB-INF/world"/>!</h1>
182
</body>
183 1 Henning Blohm
</html>
184
</code></pre>
185
186 13 Henning Blohm
The module structure looks like this now:
187
188
<pre>
189
├── java
190
│   ├── src.api
191
│   ├── src.impl
192
│   │   └── sample
193
│   │       └── hello
194
│   │           └── world
195
│   │               └── impl
196
│   │                   └── WorldServlet.java
197
│   └── z.properties
198
├── LOCAL
199
└── web
200
    ├── WebContent
201
    │   ├── index.jsp
202
    │   └── WEB-INF
203
    │       └── web.xml
204
    └── z.properties
205
</pre>
206
207 1 Henning Blohm
Synchronizing and visiting http://localhost:8080/hello-world you will now see:
208
209 13 Henning Blohm
!clipboard-202102281913-13jtf.png!
210
211
h2. Final Note
212 12 Henning Blohm
213
We will stop here. Please have a look at other samples and the reference documentation of the current version:
214
215
{{include(Current Version)}}