Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-->
<HTML>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="Stylesheet" href="../prose.css" type='text/css'>
<title>DD API Usage</title>
</head>
<BODY>
<h2>The Examples of DD API Usage.</h2>
<h3>Example 1</h3>
In this example, DD API client prints all listener classes from deployment descriptor to console:<br>
<p>
<pre>
<div class="nonnormative">
<font class="keyword">package</font> mypackage;
<font class="comment">// get the file object of web.xml file</font>
<b>DDProvider</b> ddProvider = DDProvider.getDefault();
<b>FileObject</b> fo = <b>Repository</b>.getDefault().findResource("WEB-INF/web.xml");
<font class="comment">// get the deployment descriptor root object</font>
<b>WebApp</b> webApp = ddProvider.getDDRoot(fo);
<font class="comment">// print the version of deployment dscriptor</font>
System.out.println("DD version = "+webApp.getVersion());
<font class="comment">// get the array of listeners and print the listener classes</font>
<b>Listener</b> [] listeners = webApp.getListener();
<font class="keyword">for</font> (int i=<font color="constant">0</font>; i<listeners.length; i++) {
System.out.println("Listener ["+i+"] = "+listeners[i].getListenerClass());
}
}
}
</div>
</pre>
</p>
<h3>Example 2</h3>
In this example, DD API client searches a servlet "CarServlet" and, if such a servlet exists, 2 init parameters are created : "car_type" and "car_color".<br>
Then, finally, changes ara saved back to file object :<br>
<div class="nonnormative">
<pre>
<font class="keyword">package</font> mypackage;
<font class="comment">// get the file object of web.xml file</font>
<b>DDProvider</b> ddProvider = DDProvider.getDefault();
<b>FileObject</b> fo = Repository.getDefault().findResource("WEB-INF/web.xml");
<font class="comment">// get the deployment descriptor root object</font>
<b>WebApp</b> webApp = ddProvider.getDDRoot(fo);
<font class="comment">// print the version of deployment dscriptor</font>
System.out.println("DD version = "+webApp.getVersion());
<font class="comment">// looks for the "CarServlet" servlet at WebApp object by ServletName property</font>
<b>Servlet</b> servlet = (Servlet) webApp.findBeanByName("Servlet", "ServletName", "CarServlet");
if (servlet!=<font class="constant">null</font>) {
<font class="comment">// add the first InitParam object to Servlet object</font>
servlet.addBean("InitParam", new String[]{"ParamName","ParamValue"}, new String[]{"car_type","FORD"}, <font class="constant">null</font> );
<font class="comment">// add the second InitParam object to Servlet object</font>
servlet.addBean("InitParam", new String[]{"ParamName","ParamValue"}, new String[]{"car_color","green"}, <font class="constant">null</font> );
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.