File: usage.html

package info (click to toggle)
libnb-platform18-java 12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 729,800 kB
  • sloc: java: 5,059,097; xml: 574,432; php: 78,788; javascript: 29,039; ansic: 10,278; sh: 6,386; cpp: 4,612; jsp: 3,643; sql: 1,097; makefile: 540; objc: 288; perl: 277; haskell: 93
file content (129 lines) | stat: -rw-r--r-- 6,130 bytes parent folder | download | duplicates (3)
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
<!--

    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

      http://www.apache.org/licenses/LICENSE-2.0

    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="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>