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