<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("<font color="constant">WEB-INF/web.xml</font>");
<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("<font color="constant">DD version = </font>"+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("<font color="constant">Listener [</font>"+i+"<font color="constant">] = </font>"+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("<font class="constant">WEB-INF/web.xml</font>");
<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("<font class="constant">DD version = </font>"+webApp.getVersion());
<font class="comment">// looks for the "CarServlet" servlet at WebApp object by ServletName property</font>
<b>Servlet</b> servlet = (Servlet) webApp.findBeanByName("<font class="constant">Servlet</font>", "<font class="constant">ServletName</font>", "<font class="constant">CarServlet</font>");
if (servlet!=<font class="constant">null</font>) {
<font class="comment">// add the first InitParam object to Servlet object</font>
servlet.addBean("<font class="constant">InitParam</font>", new String[]{"<font class="constant">ParamName</font>","<font class="constant">ParamValue</font>"}, new String[]{"<font class="constant">car_type</font>","<font class="constant">FORD</font>"}, <font class="constant">null</font> );
<font class="comment">// add the second InitParam object to Servlet object</font>
servlet.addBean("<font class="constant">InitParam</font>", new String[]{"<font class="constant">ParamName</font>","<font class="constant">ParamValue</font>"}, new String[]{"<font class="constant">car_color</font>","<font class="constant">green</font>"}, <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 und die Messung sind noch experimentell.