    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Getting Started with J2EE</title>
        <link>https://members.accu.org/index.php/articles/1115</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 13, #3 - Jun 2001</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c120/">133</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-120/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+120/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Getting Started with J2EE</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 June 2001 13:15:45 +01:00 or Sun, 03 June 2001 13:15:45 +01:00</p>
<p><strong>Summary:</strong>&nbsp;<p>This document is meant as a brief introduction to the Java 2 Enterprise Edition.</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e23" id=
"d0e23"></a>Introduction</h2>
</div>
<p>This document is meant as a brief introduction to the Java 2
Enterprise Edition. It is intended to provide a short, sharp and
hands on guide to getting going developing Enterprise Java
solutions, it stems from my disappointment with the documentation
that comes with the reference implementation. Note that this
article discusses the reference implementation provided by Sun
Microsystems, the license of which prohibits commercial use.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>What you'll
need</h2>
</div>
<p>To follow this article and develop and deploy the examples you
will need to have a machine with the following installed:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Java 2 Platform SDK Standard Edition (Java SDK) (<a href=
"http://java.sun.com/j2se/1.3/" target=
"_top">http://java.sun.com/j2se/1.3/</a>)</p>
</li>
<li>
<p>Java 2 Platform Enterprise Edition (J2EE) (<a href=
"http://www.javasoft.com/j2ee/" target=
"_top">http://www.javasoft.com/j2ee/</a>)</p>
</li>
</ul>
</div>
<p>I suggest you download the documentation to accompany each of
the above.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e48" id="d0e48"></a>Configuring
your system</h2>
</div>
<p>Once you have downloaded the above and installed them you will
need to navigate to the directory in which you installed the J2EE,
and edit the file <tt class="filename">bin\userconfig.bat</tt>,
adding the following two lines (NOTE UNIX users I currently only
have a windows box at home so you'll have to adjust for UNIX):</p>
<pre class="programlisting">
set JAVA_HOME=c:\jdk1.3
set J2EE_HOME=c:\j2sdkee1.2.1
</pre>
<p>Though do remember to change the paths to actually match where
you have installed them. JAVA_HOME should reference the Java SDK,
and J2EE_HOME should reference the J2EE.</p>
<p>Now open a command prompt and navigate to your J2EE_HOME\bin
directory, and run userconfig.bat. If you get a message about being
out of environment space, right click on the command prompt title
bar, select Properties from the menu, then the Memory tab, and then
set the initial environment to 4096, and hit the OK button. You may
need to restart your command prompt before running the <tt class=
"filename">userconfig.bat</tt> again.</p>
<p>Once you have successfully executed <tt class=
"filename">userconfig.bat</tt>, it's time to run <tt class=
"filename">j2ee.bat</tt> (from the same directory). You should then
end up with the following displayed on the screen:</p>
<pre class="screen">
J2EE server Listen Port: = 1049
Redirecting the output and error streams to the following files:
c:\j2sdkee1.2.1\logs\scone\ejb\j2ee\system.out
c:\j2sdkee1.2.1\logs\scone\ejb\j2ee\system.err
J2EE server startup complete.
</pre>
<p>Your J2EE Server is now up and running, you might like to test
it by starting a web browser and pointing it at
http://localhost:8000 You should then see something like the
following:</p>
<pre class="screen">
    J2EE 1.2.1
  Default Home Page  

Congratulations! 
Viewing this page means that your Web Server has been installed correctly and is serving files properly. 

J2EE-ri-feedback@sun.com 
</pre>
<p>NOTE if you open additional command prompts you may need to run
<tt class="filename">userconfig.bat</tt> in each before using the
J2EE tools and batch files.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e84" id="d0e84"></a>Building
Enterprise Applications</h2>
</div>
<p>For some reason most tutorials start at EJBs and work forward to
the client, instead I have chosen to start with the simpler, and
more immediately rewarding JSP (we all like to see stuff really
working).</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e89" id="d0e89"></a>A First JSP</h3>
</div>
<p>Firstly you should be aware what JSP's are, JSP stands for Java
Server Page. JSP is very similar to Microsoft's ASP, in that it
embeds code within a HTML document. You can read more about JSP at
<a href="http://java.sun.com/products/jsp/index.html" target=
"_top">http://java.sun.com/products/jsp/index.html</a>. Have a look
at the following trivial example:</p>
<pre class="programlisting">
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;JSP Test Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
 
&lt;%! String message = &quot;Hello, World&quot;; %&gt;
The traditional greeting: &lt;b&gt;&lt;%= message %&gt;&lt;/b&gt;
&lt;body&gt;
&lt;/html&gt;
</pre>
<p>Save the above in the file JSPTest.jsp (for example) and we can
move onto deployment of your JSP.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e101" id="d0e101"></a>Deploying your
JSP</h3>
</div>
<p>To deploy your software developed with the J2EE we use the
Application Deployment tool, which you can start by running
deploytool.bat. You'll then be presented with this screen:</p>
<div class="figure"><a name="d0e106" id="d0e106"></a>
<p class="title c2">Figure 1. Application Deployment Tool</p>
<div class="mediaobject"><img src=
"resources/Figure1%20Application%20Deployment%20Tool.png" alt=
"Application Deployment Tool"></div>
</div>
<p>From the file menu choose New Application and fill in the
following dialog box with the relevant information:</p>
<div class="figure"><a name="d0e114" id="d0e114"></a>
<p class="title c2">Figure 2. New Application Dialog</p>
<div class="mediaobject"><img src=
"resources/Figure2%20New%20Application%20Dialog.png" alt=
"New Application Dialog"></div>
</div>
<p>Once you have done that again from the <span class=
"guimenu">File</span> menu choose <span class="guimenuitem">New Web
Component</span>, which will present you with the dialog box in
figure 3.</p>
<p>So have a read, and then click <span class=
"guibutton">Next</span>, you'll be presented with a rather more
complex screen on which you need to fill in the following
fields.</p>
<p>WAR Display Name This is a friendly name for the WAR you are
about to create. For example use <tt class=
"filename">JSPTestWAR</tt> Contents Click on <span class=
"guibutton">Add</span>, and use the wizard to get <tt class=
"filename">JSPTest.jsp</tt> listed in the contents. Then click
<span class="guibutton">Next</span> and change the <span class=
"emphasis"><em>Web Component Type</em></span> to JSP, and click
<span class="guibutton">Next</span>. On this screen you will need
to select the JSP Filename, and type in a Web Component Display
Name, I suggest <tt class="literal">TheJSPTest</tt>. Then click
<span class="guibutton">Finish</span>.</p>
<div class="figure"><a name="d0e159" id="d0e159"></a>
<p class="title c2">Figure 3. New Web Component</p>
<div class="mediaobject"><img src=
"resources/Figure3%20New%20Web%20Component.png" alt=
"New Web Component"></div>
</div>
<p>Finally in the <span class="emphasis"><em>Local
Applications</em></span> treeview navigate to the <span class=
"application">JSPTest</span> application, then select the Web
Context tab on the right, and edit the context root to say
<tt class="literal">JSPTestRoot</tt>. Finally go to the
<span class="guimenu">Tools</span> menu and select <span class=
"guimenuitem">Deploy</span>. Accept the default settings and deploy
your JSP.</p>
<p>Lastly you'll want to test it by opening <a href=
"http://localhost:8000/JSPTestRoot/JSPTest.jsp" target=
"_top">http://localhost:8000/JSPTestRoot/JSPTest.jsp</a> in your
favourite Internet browser. All being well you should see:</p>
<pre class="screen">
The traditional greeting: Hello, World
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e189" id="d0e189"></a>A First
Servlet</h3>
</div>
<p>Servlets are an alternative to traditional CGI scripts. You can
learn more at <a href=
"http://java.sun.com/products/servlet/index.html" target=
"_top">http://java.sun.com/products/servlet/index.html</a>. In the
meantime here is a very simple example, I'll explain what the code
does in a later article.</p>
<p>The following is a basic example servlet:</p>
<pre class="programlisting">
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TestServlet extends HttpServlet {
/**
 * Handle the HTTP GET method by building a 
 * simple web page.
 */
  public void 
  doGet(HttpServletRequest request,
       HttpServletResponse response) 
      throws ServletException, IOException {
    PrintWriter  out;
    String title = &quot;Test Servlet&quot;;
// set content type and other response header
// fields first
    response.setContentType(&quot;text/html&quot;);
// then write the data of the response
    out = response.getWriter();
// write out the page
    out.println(&quot;&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;&quot;);
    out.println(title);
    out.println(&quot;&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;&quot;);
    out.println(&quot;&lt;H1&gt;&quot; + title + &quot;&lt;/H1&gt;&quot;);
    out.println(&quot;&lt;P&gt;Hello, World.&lt;/P&gt;&quot;);
    out.println(&quot;&lt;/BODY&gt;&lt;/HTML&gt;&quot;);
    out.close();
  }
}
</pre>
<p>which will need compiling with the following line:</p>
<pre class="screen">
javac -classpath %J2EE_HOME%\lib\j2ee.jar TestServlet.java
</pre>
<p>You will have to make sure <tt class="literal">J2EE_HOME</tt> is
set for the command prompt you use to run the command. You may also
need to ensure Java is in your PATH (i.e. <tt class="literal">set
path=%PATH%;JAVA_HOME\bin</tt>).</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e213" id="d0e213"></a>Deploying Your
Servlet</h3>
</div>
<p>Once again you will need to run <span class=
"emphasis"><em>Application Deployment Tool</em></span>, then create
a new application as we did for <span class=
"application">JSPTest</span> previously, this time call it
<span class="application">ServletTest</span>. Then add a new Web
Component. You will need to add the <tt class=
"filename">TestServlet.class</tt> file, so click on <span class=
"guibutton">Add</span> in the contents pane, then click
<span class="guibutton">Next</span> in the dialog it opens (make
sure you add it as a Class file and not as a Content file). Then
click <span class="guibutton">Next</span>, make sure Servlet is
selected, and click <span class="guibutton">Next</span> again.
Select <tt class="classname">TestServlet</tt> in the Servlet Class,
enter a Web Component Display Name (I used <tt class=
"literal">TestServlet</tt> again). Click <span class=
"guibutton">Next</span> twice, then <span class=
"guibutton">Add</span> and add the alias <tt class=
"literal">ServletAlias</tt> and click <span class=
"guibutton">Finish</span>.</p>
<p>Finally you can now deploy your application as you did for the
JSP example. You should then be able to see the results by pointing
your Internet browser to: <a href=
"http://localhost:8000/ServletRoot/ServletAlias" target=
"_top">http://localhost:8000/ServletRoot/ServletAlias</a></p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e264" id="d0e264"></a>Enterprise Java
Beans</h3>
</div>
<p>Last but not least we'll look at Enterprise Java Beans (usually
referred to as EJBs). Each snippet below is from a file of the same
name as the class or interface, as you would expect.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e269" id="d0e269"></a>Remote
Interface</h3>
</div>
<pre class="programlisting">
import javax.ejb.*;
import java.rmi.*;
public interface Hello extends EJBObject {
  String greet(String s) 
                throws RemoteException;
}
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e274" id="d0e274"></a>Home
Interface</h3>
</div>
<pre class="programlisting">
import javax.ejb.*;
public interface HelloHome extends EJBHome {
  public Hello create() throws
           java.rmi.RemoteException,
           javax.ejb.CreateException;
}
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e279" id="d0e279"></a>The Bean
Itself</h3>
</div>
<pre class="programlisting">
import javax.ejb.*;
public class HelloBean implements SessionBean{
  SessionContext ctx;
  public void ejbCreate() {}
  public String greet(String str){
    return &quot;Hello &quot; + str;
  }
  public void 
  setSessionContext(SessionContext ctx){
    this.ctx = ctx;
  }
  public void ejbRemove(){}
  public void ejbActivate(){}
  public void ejbPassivate(){}
  public void setSessionContext(){}
  public HelloBean(){}
}
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e284" id="d0e284"></a>Deploying Your
EJB</h3>
</div>
<p>Firstly you will have to compile each of the classes:</p>
<pre class="screen">
javac -classpath %J2EE_HOME%\lib\j2ee.jar &lt;classname.java&gt;
</pre>
<p>Depending on how you work, you may need to add the directory to
which the other classes have been compiled to the <tt class=
"literal">classpath</tt> above. Once again you will need to run
<span class="emphasis"><em>Application Deployment Tool</em></span>,
then create a new application as we did for <span class=
"application">JSPTest</span> and <span class=
"application">ServletTest</span> previously, this time call it
<span class="application">EJBTest</span> (imaginative aren't
I?).</p>
<p>So as before create a new application using the <span class=
"emphasis"><em>deploytool</em></span>, I've called mine
<span class="application">HelloApp</span>. The select <span class=
"guimenu">File</span> -&gt; <span class="guimenuitem">New
Enterprise Bean</span> as shown below:</p>
<div class="figure"><a name="d0e322" id="d0e322"></a>
<p class="title c2">Figure 4. New Enterprise Bean</p>
<div class="mediaobject"><img src=
"resources/Figure4%20New%20Enterprise%20Bean.png" alt=
"New Enterprise Bean"></div>
</div>
<p>Then hit the <span class="guibutton">Next</span> button on the
info screen that is displayed, on the next screen set the JAR
display name to <tt class="literal">HelloJar</tt>, and add the
files <tt class="filename">Hello.class</tt>, <tt class=
"filename">HelloHome.class</tt>, and <tt class=
"filename">HelloBean.class</tt> to the <span class=
"emphasis"><em>Contents</em></span>. Then hit <span class=
"guibutton">Next</span>. On this screen you need to set the
<span class="emphasis"><em>Enterprise Bean Class</em></span> to
<tt class="classname">HelloBean</tt>, the <span class=
"emphasis"><em>Home Interface</em></span> to <tt class=
"literal">HelloHome</tt>, and the <span class="emphasis"><em>Remote
Interface</em></span> to <tt class="literal">Hello</tt>, and make
sure the <span class="emphasis"><em>Bean Type</em></span> is set to
<span class="emphasis"><em>Session</em></span>, <span class=
"emphasis"><em>Stateless</em></span>. Then set the <span class=
"emphasis"><em>Enterprise Bean Display Name</em></span> to
<tt class="literal">HelloBean</tt>, and click <span class=
"guibutton">Next</span>, and <span class="guibutton">Next</span>
again, in the EJB reference in <span class=
"emphasis"><em>Code</em></span> add a row with the following
entries:</p>
<pre class="screen">
Coded Name:  ejb/HelloBean
Type:    Session
Home:    HelloHome
Remote:  Hello
</pre>
<p>The click <span class="guibutton">Finish</span>. Next select the
<span class="emphasis"><em>JNDI Tab</em></span>, and set the
<span class="emphasis"><em>JNDI Name</em></span> to <tt class=
"literal">HelloBean</tt>.</p>
<p>You are now ready to deploy it, so select <span class=
"guimenuitem">Deploy Application</span> from the <span class=
"guimenu">Tools</span> menu, and this time check the <span class=
"emphasis"><em>Return Client Jar</em></span> checkbox (and select a
suitable path). Then hit <span class="guibutton">Next</span> twice
and then <span class="guibutton">Finish</span>.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e426" id="d0e426"></a>The EJB
Client</h3>
</div>
<pre class="programlisting">
import javax.rmi.*;
import javax.ejb.*;
import javax.naming.*;
public class HelloClient {
  public static void main(String[] args) {
    Context context;
    HelloHome home;
    try {
      context = new InitialContext();
      try {
        Object obj = context.lookup(&quot;HelloBean&quot;);
        try {
          home = (HelloHome) PortableRemoteObject.narrow(obj, HelloHome.class);
          Hello bean = home.create();
          System.out.println(bean.greet(&quot;World&quot;));
        }
        catch (Exception e){
          System.out.println(&quot;Exception: &quot; + e);
        }
      }
      catch (Exception e) {
        System.out.println(&quot;Can't look up HelloBean.&quot; +  e);
      }
    }
    catch (NamingException e) {
      System.out.println(&quot;Can't get context.&quot;);
    }
  }
}
</pre>
<p>You can now compile the above client using:</p>
<pre class="screen">
javac -classpath %J2EE_HOME%\lib\j2ee.jar;. HelloClient.java
</pre>
<p>Then run it using:</p>
<pre class="screen">
java -classpath %J2EE_HOME%\lib\j2ee.jar;.\HelloAppClient.jar;. HelloClient
</pre>
<p>You should then see: <tt class="computeroutput">Hello World</tt>
on your screen.</p>
<p>Congratulations, you've written and deployed an example of each
important bit of the Java 2 Enterprise Edition!</p>
<p>Finally, the code and an electronic copy of this article are
available from <a href=
"http://www.crickett.co.uk/articles/java/j2ee/intro.zip" target=
"_top">http://www.crickett.co.uk/articles/java/j2ee/intro.zip</a></p>
</div>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e450" id="d0e450"></a>References</h2>
</div>
<div class="bibliomixed">
<p class="bibliomixed">In addition to the URLs mentioned above (on
which the majority of this article is based), the reader may also
wish to consider the following books, reviews of which can be found
on the ACCU web site (<span class="bibliomisc"><a href=
"http://www.accu.org" target="_top">www.accu.org</a></span>).</p>
</div>
<div class="bibliomixed"><a name="Monson-Haefel" id=
"Monson-Haefel"></a>
<p class="bibliomixed">[Monson-Haefel] Enterprise JavaBeans, Second
Edition by Richard Monson-Haefel</p>
</div>
<div class="bibliomixed"><a name="Hunter-Crawford" id=
"Hunter-Crawford"></a>
<p class="bibliomixed">[Hunter-Crawford] Java Servlet Programming
by Jason Hunter, William Crawford</p>
</div>
<div class="bibliomixed"><a name="Englander" id="Englander"></a>
<p class="bibliomixed">[Englander] Developing Java Beans by Robert
Englander</p>
</div>
<div class="bibliomixed"><a name="Bergsten" id="Bergsten"></a>
<p class="bibliomixed">[Bergsten] JavaServer Pages by Hans
Bergsten</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">N.B. I don't work for O'Reilly, I just like
the layout and quality of their Java books.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
