-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapplications.html
More file actions
296 lines (294 loc) · 10.2 KB
/
webapplications.html
File metadata and controls
296 lines (294 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>exedio persistence - Web Application Trail</title>
<meta name="keywords" content="Persistence, Java">
<meta name="description" content="persistence framework for java">
<link rel="stylesheet" href="cope.css">
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/exedio">View on GitHub</a>
<h1 id="project_title">exedio persistence</h1>
<h2 id="project_tagline">Persistence Framework for Java.</h2>
<a id="toplink" href="index.html">Go to top</a>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h2>Web Application Trail</h2>
<p>
exedio persistence itself doesn't care,
whether it runs in a web container or not.
However it provides some additional convenience,
when used within web applications.
</p>
<p>
If you already decided to use exedio persistence for a project,
you may want to use the demo application as a template.
This way you can skip the steps described here.
The demo application is available under the
<a href="https://opensource.org/licenses/mit-license.php">The MIT License</a>.
<br>
<a href="http://sourceforge.net/projects/cope/files/copedemo/">Download</a>
</p>
<h3 id="contents">Contents</h3>
<ul>
<li><a href="#connect">Connecting</a></li>
<li><a href="#transactions">Transactions</a></li>
<li><a href="#console">exedio console</a></li>
<li><a href="#copernica">Copernica</a></li>
<li><a href="#context">Context Parameter</a></li>
<li><a href="#further">Further Reading</a></li>
</ul>
<h3 id="connect">Connecting</h3>
<p>
The framework must be told, where to connect to the database.
Within a servlet you probably
want to do this in the <code>init()</code> method.
The file <code>cope.properties</code> should be in the
<code>WEB-INF</code> subdirectory to protect it against client access.
So you will probably want to write something like this:
</p>
<pre>
public void init() throws ServletException
{
Main.model.<a href="api/com/exedio/cope/Model.html#connect(com.exedio.cope.ConnectProperties)">connect</a>(
new <a href="api/com/exedio/cope/ConnectProperties.html#<init>(java.io.File)">ConnectProperties</a>(
new File(getServletContext().
getRealPath("WEB-INF/cope.properties"))));
}
</pre>
<p>
For your convenience, there is a helper method,
which does just that:
</p>
<pre>
public void init() throws ServletException
{
ServletUtil.<a href="api/com/exedio/cope/util/ServletUtil.html#connect(com.exedio.cope.Model, javax.servlet.ServletConfig, java.lang.String)">connect</a>(Main.model, getServletContext());
}
</pre>
<p>
Even this can be ommitted, if you use the <i>CopeFilter</i>
explained below.
Please note, that
<code><a href="api/com/exedio/cope/Model.html#connect(com.exedio.cope.ConnectProperties)">connect</a></code>
is idempotent,
it doesn't hurt to call it more than once,
for example if you have more than one servlet.
</p>
<h3 id="transactions">Transactions</h3>
<p>
From the <a href="transactions.html">Transaction Trail</a> you
already know, how to use transactions with exedio persistence.
With a single servlet you may want to write something like this:
</p>
<pre>
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
{
try
{
Main.model.startTransaction();
...
Main.model.commit();
}
finally
{
Main.model.rollbackIfNotCommitted();
}
}
</pre>
<p>
However, this is not always feasible.
Sometimes you just don't have a single servlet
(or a small number of servlets)
in your application.
Even worse, the method <code>doGet</code> could be called reentrantly.
This is very common when using the struts framework.
</p>
<p>
A nice solution for both problems is delegating the transaction handling into a
<a href="https://docs.oracle.com/javaee/1.3/api/javax/servlet/Filter.html">filter</a>.
Class
<a href="api/com/exedio/cope/misc/CopeFilter.html">CopeFilter</a>
provides such a filter.
It must be deployed in your <code>web.xml</code>:
</p>
<pre>
<filter>
<filter-name>CopeFilter</filter-name>
<filter-class><a href="api/com/exedio/cope/misc/CopeFilter.html">com.exedio.cope.misc.CopeFilter</a></filter-class>
<init-param>
<param-name>model</param-name>
<param-value><i>com.exedio.copedemo.Main</i>#model</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CopeFilter</filter-name>
<url-pattern>*.do</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</pre>
<p>
The example above applies the filter for all request to <code>*.do</code>,
a typical setting for struts.
It assumes, that the main class resides in package <code>com.exedio.copedemo</code>,
please adapt this to your package.
</p>
<p>
As a side effect, the <i>CopeFilter</i> connects the
model in its own <code>init()</code>,
so you don't have to care about it anymore.
</p>
<h3 id="console">exedio console</h3>
<img src="console.png" alt="Console Screenshot" width="296" height="275" border="0" align="right">
<p>
When you start up your web application,
you have to create the database schema at least once.
This is done by calling <code>Main.model.createDatabase()</code>.
Instead of putting a button somewhere in a jsp of your application,
you can use the <i>exedio console</i>,
which assists you in creating/dropping/revising the database schema
and more.
</p>
<p>
On the right you see the relevant part of the <i>exedio console</i>.
The button <i>create</i> creates the database schema,
the button <i>drop</i> drops it.
</p>
<p>
To deploy the <i>exedio console</i> in your application,
put the file <code>exedio-cope-console.jar</code> into your
<code>WEB-INF/lib</code> directory.
Then you just mount another servlet within your <code>web.xml</code>:
</p>
<br clear="right">
<pre>
<servlet>
<servlet-name>console</servlet-name>
<servlet-class>com.exedio.cope.console.ConsoleServlet</servlet-class>
<init-param>
<param-name>model</param-name>
<param-value><i>com.exedio.copedemo.Main</i>#model</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>console</servlet-name>
<url-pattern>/console/*</url-pattern>
</servlet-mapping>
</pre>
<p>
Note, that the trailing "/*" in the url-pattern is mandatory,
otherwise the servlet cannot deliver it's resources
(style sheet etc).
</p>
<h3 id="copernica">Copernica</h3>
<a href="copernica1.png">
<img src="copernica1.w200.jpg" alt="Copernica Screenshot 1" width="200" height="163" border="0" align="right">
</a>
<a href="copernica2.png">
<img src="copernica2.w200.jpg" alt="Copernica Screenshot 2" width="200" height="113" border="0" align="right">
</a>
<p>
<i>Copernica</i> is an experimental generic backoffice for exedio persistence.
It allows the user to query and manipulate the data stored
in a model.
</p>
<p>
Although <i>Copernica</i> is still in a very early stage of development,
it already allows access to the persistent data
suitable for development and debugging.
</p>
<p>
Deploying <i>Copernica</i> in your application
is very similar to the <i>exedio console</i>.
Put the file <code>exedio-copernica.jar</code> into your
<code>WEB-INF/lib</code> directory
and the following snippet into your <code>web.xml</code>:
</p>
<br clear="right">
<pre>
<servlet>
<servlet-name>copernica</servlet-name>
<servlet-class>com.exedio.copernica.CopernicaServlet</servlet-class>
<init-param>
<param-name>model</param-name>
<param-value><i>com.exedio.copedemo.Main</i>#model</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>copernica</servlet-name>
<url-pattern>/copernica/*</url-pattern>
</servlet-mapping>
</pre>
<h3 id="context">Context Parameter</h3>
<p>
If you followed all the steps above,
you have the following snippet three times in your <code>web.xml</code>:
</p>
<pre>
<init-param>
<param-name>model</param-name>
<param-value><i>com.exedio.copedemo.Main</i>#model</param-value>
</init-param>
</pre>
<p>
You may want to simplify this
by replacing all three init parameters
by a single context parameter,
that has the same effect:
</p>
<pre>
<context-param>
<param-name>model</param-name>
<param-value><i>com.exedio.copedemo.Main</i>#model</param-value>
</context-param>
</pre>
<p>
Note, that the context parameter has to be placed outside the servlet tag.
</p>
<h3 id="further">Further Reading</h3>
<p>
This was the web application trail of the tour.
You may now proceed to trails:
</p>
<ul>
<li>
<a href="searching.html">Searching Trail</a>
gives you an introduction into the searching capabilities of exedio persistence.
</li>
<li>
<a href="fields2.html">Field Reloaded Trail</a>
covers all the more specific possibilities to store data.
</li>
<li>
<a href="schema.html">Schema Trail</a>
shows you the schema evolution support of the <i>exedio console</i>.
</li>
</ul>
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p>
Maintained by
<a href="https://www.exedio.com/" target="_top">exedio</a>
Gesellschaft für Softwareentwicklung mbH.
</p>
<a href="https://validator.w3.org/check?uri=referer" class="validhtml">Valid HTML5</a>
<p>
Slate theme by <a href="https://github.com/jasoncostello">Jason Costello</a>.
Published with <a href="https://pages.github.com">GitHub Pages</a>.
</p>
</footer>
</div>
</body>
</html>