File: usage.html

package info (click to toggle)
netbeans-ide 6.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 741,536 kB
  • ctags: 613,961
  • sloc: java: 3,969,489; xml: 336,553; jsp: 11,861; ruby: 10,091; cpp: 4,127; sh: 3,417; ansic: 1,734; sql: 1,306; haskell: 1,019; makefile: 487; perl: 403; objc: 288; php: 120
file content (149 lines) | stat: -rw-r--r-- 7,496 bytes parent folder | download | duplicates (4)
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
<!--
   - DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   -
   - Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   -
   - The contents of this file are subject to the terms of either the GNU
   - General Public License Version 2 only ("GPL") or the Common
   - Development and Distribution License("CDDL") (collectively, the
   - "License"). You may not use this file except in compliance with the
   - License. You can obtain a copy of the License at
   - http://www.netbeans.org/cddl-gplv2.html
   - or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
   - specific language governing permissions and limitations under the
   - License.  When distributing the software, include this License Header
   - Notice in each file and include the License file at
   - nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
   - particular file as subject to the "Classpath" exception as provided
   - by Sun in the GPL Version 2 section of the License file that
   - accompanied this code. If applicable, add the following below the
   - License Header, with the fields enclosed by brackets [] replaced by
   - your own identifying information:
   - "Portions Copyrighted [year] [name of copyright owner]"
   -
   - Contributor(s):
   -
   - The Original Software is NetBeans. The Initial Developer of the Original
   - Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
   - Microsystems, Inc. All Rights Reserved.
   -
   - If you wish your version of this file to be governed by only the CDDL
   - or only the GPL Version 2, indicate your decision by adding
   - "[Contributor] elects to include this software in this distribution
   - under the [CDDL or GPL Version 2] license." If you do not indicate a
   - single choice of license, a recipient has the option to distribute
   - your version of this file under either the CDDL, the GPL Version 2 or
   - to extend the choice of license to its licensees as provided above.
   - However, if you add GPL Version 2 code and therefore, elected the GPL
   - Version 2 license, then the option applies only if the new code is
   - made subject to such option by the copyright holder.
  -->

<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="keyword">import</font> org.netbeans.api.web.dd.DDProvider;
<font class="keyword">import</font> org.netbeans.api.web.dd.WebApp;
<font class="keyword">import</font> org.netbeans.api.web.dd.Listener;
<font class="keyword">import</font> org.openide.filesystems.*;

<font class="keyword">public class</font> <b>ListenerExample</b> {
<font class="comment">    
    /**
     * @param args the command line arguments
     */
</font>
   <font class="keyword">public static void</font> main(String[] args)<font class="keyword">throws</font> <b>Exception</b> {

	<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&lt;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="keyword">import</font> org.netbeans.api.web.dd.DDProvider;
 <font class="keyword">import</font> org.netbeans.api.web.dd.WebApp;
 <font class="keyword">import</font> org.netbeans.api.web.dd.Servlet;
 <font class="keyword">import</font> org.netbeans.api.web.dd.InitParam;
 <font class="keyword">import</font> org.openide.filesystems.*;

 <font class="keyword">public class</font> <b>InitParamExample</b> {
 <font class="comment">    
    /**
     * @param args the command line arguments
     */
</font>
    <font class="keyword">public static void</font> main(String[] args) <font class="keyword">throws</font> <b>Exception</b> {

	<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> );

	    <font class="comment">// print all init params to console</font>
            <b>InitParam</b> [] newParams = servlet.getInitParam();
            <font class="keyword">for</font> (int i=<font class="constant">0</font>;i&lt;newParams.length;i++)
                System.out.println("<font class="constant">init-param [</font>"+i+"<font class="constant">] = </font>"+newParams[i].getParamName()+"<font class="constant"> -&gt; </font>"+newParams[i].getParamValue());

	    <font class="comment">// write changes back to file object</font>
            webApp.write(fo);
        }
    }
 }
</pre>
</div>
</p>

</BODY></HTML>