<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Simple CDI Hello World with Eclipse and JBoss Tools
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="http://community.jboss.org/people/max.andersen%40jboss.com">Max Andersen</a> in <i>JBoss Tools</i> - <a href="http://community.jboss.org/community/tools/blog/2010/12/10/simple-cdi-hello-world-in-eclipse">View the full blog post</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>With JBoss Tools 3.2 Beta 2 it is simple to start using CDI (Context Dependency Injection).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>All you need is the following:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><ul><li><a class="jive-link-external-small" href="http://www.jboss.org/jbossas/downloads/">JBoss AS 6</a> or another Eclipse supported CDI container such as Glassfish</li><li><a class="jive-link-blog-small" href="http://community.jboss.org/community/tools/blog/2010/12/09/jboss-developer-studio-beta-2-is-ready">JBoss Developer Studio 4 Beta 2 </a>or another Eclipse with <a class="jive-link-blog-small" href="http://community.jboss.org/community/tools/blog/2010/12/09/jboss-tools-32-beta-2">JBoss Tools 3.2 Beta 2</a> installed</li><li>5 minutes to watch or read the following blog</li></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Here it is as a video walk through:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><object height="350" width="425"><param name="allowfullscreen" value=""/><param name="allowscriptaccess" value=""/><param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=17669512&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1"/><param name="wmode" value=""/><embed allowfullscreen="true" allowscriptaccess="always" height="350" src="http://www.vimeo.com/moogaloop.swf?clip_id=17669512&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" width="425" wmode="transparent"></embed></object></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>...and here are the steps in Screenshot form:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Step 1: Install/Configure the Server runtime</h2><p>The simplest way to setup JBoss runtimes is to use the "JBoss Runtimes" preference page which are available</p><p>via the Preferences or by simply pressing Ctrl+3 and type in "JBoss runtimes".</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10704/hellocdi_ctrl3runtimes.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10704/385-144/hellocdi_ctrl3runtimes.png </span></a></p><p>When pressing enter you should see the following Preferences page:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10705/hellocdi_preferencepageruntimes.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10705/450-230/hellocdi_preferencepageruntimes.png </span></a></p><p>Click the "Search..." button and point it to a directory with your JBoss runtimes; in this case I got a few.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10706/hellocdi_selectruntimefolder.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10706/450-335/hellocdi_selectruntimefolder.png </span></a></p><p>When you have selected the directory JBoss Tools will scan for runtimes it knows about and show</p><p>you what it found:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10707/hellocdi_foundruntimes.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10707/450-165/hellocdi_foundruntimes.png </span></a></p><p>In this dialog you can then remove the checkmarks for runtimes you do not want configured or simply press OK and JBoss Tools will make all selected runtimes available from within the IDE.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>For the Server parts it is now availabe in the server view:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10708/cdihelloworld_servers.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10708/265-105/cdihelloworld_servers.png </span></a></p><p>From here you can start the server (in this case JBoss 6) by clicking the Debug or Run icon. If you start via Debug icon the server will wait at any breakpoints you have set in your code and allow you to inspect the current state.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In any case when the server starts up it sends the output of the console to the Console View:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10709/helloworld_cdi_console.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10709/450-187/helloworld_cdi_console.png </span></a></p><h2>Step 2: Create the Project</h2><p>Now use the File &gt; New &gt; Dynamic Web Project or simply press Ctrl+N and find the "Dynamic Web Project" wizard</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10710/dynamic_web_project.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10710/450-332/dynamic_web_project.png </span></a></p><p>On the Wizard page, type in a project name (i.e. "cditest") and Click the "Modify" button under Configuration.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10711/webprojectwizard.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10711/450-474/webprojectwizard.png </span></a></p><p>The Modify Configuration is where you can enable/disable what Eclipse calls project facets.</p><p>In this case we simply want to enable the Context Dependency Injection facet.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10712/cdifacet.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10712/450-277/cdifacet.png </span></a></p><p>Now Click Ok and press Finish on the Wizard and a basic WAR project will be created for you.</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10713/cdiproject.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10713/229-190/cdiproject.png </span></a></p><h2>Step 3: Create the code</h2><p>The code we will create are the minimal for printing out Hello World via a servlet.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>We will need to add the following files:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><ul><li>WebContent/WEB-INF/beans.xml to enable CDI fully for the project (in next revision of JBoss Tools this file will be created for you)</li><li>src/org/cditest/HelloWorld.java the bean for giving the "Hello World" greeting.</li><li>src/org/cditest/HelloServlet.java the servlet for rendering the greeting in its response method.</li></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>To create the bean.xml simply use Ctrl+N and type in "XML File" and choose the WebContent/WEB-INF folder and give it the name: beans.xml</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10714/beans.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10714/450-477/beans.png </span></a></p><p>The default file is empty and that will suffice for this blog; remember the beans.xml file is simply just</p><p>a marker file to inform the CDI container that the War archive should be scanned for beans.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Next on comes the HelloWorld and Servlet class - for this I will just paste the simplest code</p><p>which you can paste into two .java files or use Eclipse's excellent wizards for it. If you want to see the full steps for this you can watch the video above.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>HelloWorld.java:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> org.cditest;
&#160;
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> HelloWorld <font color="navy">{</font>
&#160;&#160;&#160; <font color="navy"><b>public</b></font> String getGreeting() <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> <font color="red">"Hello World!"</font>;
&#160;&#160;&#160; <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>HelloServlet.java:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> org.cditest;
&#160;
<font color="navy"><b>import</b></font> java.io.IOException;
<font color="navy"><b>import</b></font> javax.inject.Inject;
<font color="navy"><b>import</b></font> javax.servlet.*;
<font color="navy"><b>import</b></font> javax.servlet.annotation.WebServlet;
<font color="navy"><b>import</b></font> javax.servlet.http.*;
&#160;
@WebServlet(<font color="red">"/HelloServlet"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> HelloServlet <font color="navy"><b>extends</b></font> HttpServlet <font color="navy">{</font>
&#160;&#160;&#160; @Inject <font color="navy"><b>private</b></font> HelloWorld hello;
&#160;&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160; <font color="navy"><b>public</b></font> HelloServlet() <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>super</b></font>();
&#160;&#160;&#160; <font color="navy">}</font>
&#160;
&#160;&#160;&#160; <font color="navy"><b>protected</b></font> <font color="navy"><b>void</b></font> doGet(HttpServletRequest request, HttpServletResponse response) 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>throws</b></font> ServletException, IOException <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.getOutputStream().println(<font color="red">"&lt;html&gt;&lt;h1&gt;"</font> + hello.getGreeting() + <font color="red">"&lt;/h1&gt;&lt;/html&gt;"</font>);
&#160;&#160;&#160; <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If you look at the code of the HelloServlet you can see it simply a class with a @WebServlet annotation to specify the context/url name, an @Inject annotated field called Hello and a doGet() method which prints out the value of hello.getGreeting().</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In short, the simplest example of @Inject annotation on a servlet.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Step 4: Run the Servlet</h2><p><br/><span>From within Eclipse you can use the Run As... or Debug As... toolbar menu to run what is the current selection and the tools will try and guess what kind of "Run" that means. i.e. for an XHTML page named home.xhtml it might be to open up a browser at </span><a class="jive-link-external-small" href="http://localhost:8080/home.seam" target="_blank">http://localhost:8080/home.seam</a><span> or in this case for a Servlet to open up the browser at the @WebServlet specified location relative to the deployment name, i.e. </span><a class="jive-link-external-small" href="http://localhost:8080/cditest/HelloServlet" target="_blank">http://localhost:8080/cditest/HelloServlet</a><span>.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10715/runonserver.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10715/450-243/runonserver.png </span></a></p><p>For new projects like this example Eclipse will ask you what Server you wish to run on:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10716/selectserver.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10716/450-477/selectserver.png </span></a></p><p>In this case I got several servers available, so I will choose the JBoss 6 server and</p><p>choose "Always use this server when running this project" so I will not have to answer</p><p>this question again on future Run's.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>When the server is selected JBoss Tools will deploy the application and show the relevant page</p><p>in a browser:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/38-3430-10718/browserhelloworld.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/38-3430-10718/419-121/browserhelloworld.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Step 5: Have fun!</h2><p>That is it - and there is of course more functionallity in the CDI tooling such as validations, quick fixes and code completions but I thought it would be good to just show the simplest application imaginable.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>But with the steps described above you now know how to setup any JBoss runtime, create any kind of project and enable the Eclipse facets you want on them.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If you want to see how easy it is to debug your applications then watch the video above, it has a little more details.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Hope you enjoyed it <span> ;) </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>p.s. the exact same steps can be used to run against Glassfish or other CDI enabled servers that has Eclipse WTP server adapters.</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="http://community.jboss.org/community/tools/blog/2010/12/10/simple-cdi-hello-world-in-eclipse">going to Community</a></p>

</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>