    <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  :: My First Steps in Java Web Development</title>
        <link>https://members.accu.org/index.php/articles/1189</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 14, #4 - Aug 2002</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/c113/">144</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+113/">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;My First Steps in Java Web Development</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 August 2002 13:15:54 +01:00 or Sat, 03 August 2002 13:15:54 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a>Background</h2>
</div>
<p>As a new member to the ACCU, my software development experience
is limited to Java although I am hoping to learn a great deal about
C and C++. I started programming in October 2001 for my
postgraduate degree, where I was immediately introduced to Java as
opposed to C or C++. All of my university projects, especially
web-based activities, have been strongly geared towards Java
technologies. Consequently, I hope that this article will serve as
an introduction to Java Server Pages (JSPs) for many of you,
demonstrating how easily and quickly they can be picked up from
scratch.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e23" id="d0e23"></a>The
Project</h2>
</div>
<p>The project, which is currently in the testing stage, is an
evaluation version of a web-based flight and hotel booking system
to be used by a new (but sadly imaginary!) low-cost airline -
TinyJet. The project requirements determined our use of Java
technologies to implement a three-tier website.</p>
<p>As with most university projects, the objective was to test our
project management and team-building skills as opposed to the
precision of our code; however, for me, I saw this project as the
biggest challenge I have been faced with on the course, and I was
damned if I was going to let the whole teambuilding thing stand in
the way of us producing the best website imaginable!!!!</p>
<p>By that I mean that I have given a great deal of time to
properly researching JSP web development in order that I could pass
on the knowledge to my team members. I would strongly recommend
Wrox's &quot;Beginning JSP Web Development&quot; (<a href=
"#Falkner">Falkner</a>) for anyone who is taking their first steps
with JSPs. Wrox have a reputation for making things more
complicated than necessary, but I found this book to be relatively
gentle, aiding me with incredibly useful examples. By the end of
the 9-week development stage, I would confidently state that all 6
team members were able to produce interactive and dynamically
generated web pages using JSPs.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e35" id="d0e35"></a>JSPs vs
Servlets</h2>
</div>
<p>So why did we choose to develop the TinyJet system using JSPs as
opposed to Java Servlets? There are two schools of thought when it
comes to this decision. JSP technology is an extension of the
Servlet technology: as such, when a JSP is loaded into the browser,
it is compiled within your Servlet engine and converted into a Java
Servlet. Members of the first party believe that Servlets are
easier to code because the developer has to think their way through
the program using the code that the page is produced in. These
people see JSPs as more awkward to code because it introduces
another style of Java coding on top of the more traditional Java
used in Servlets.</p>
<p>Members of the second party believe that JSPs are easier for
non-Java programmers for precisely the same reason!!! These people
believe that the further level of abstraction away from Java coding
enables web-page designers with little development experience to
work on the design without having to worry about the program logic
in the lower tiers.</p>
<p>One of the easiest ways to distinguish between the two
development styles is this - writing Servlets is like writing a
Java program but writing JSPs is like writing an HTML webpage.</p>
<p>A basic Servlet must import the <tt class=
"literal">javax.servlet</tt> libraries used for developing http
applications. It must also extend the <tt class=
"classname">HttpServlet</tt> class provided in the library in order
that you may override the <tt class="methodname">doGet</tt> and
<tt class="methodname">doPost</tt> methods of the webpage.</p>
<pre class="programlisting">
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class EmmasServlet extends HttpServlet {
  public void doGet(HttpServletRequest req,
                    HttpServletResponse res)
        throws ServletException, IOException {
    //dynamically prints html
    //java methods can go here
    out.println(&quot;&lt;html&gt;&lt;body&gt;&quot;);
    out.println(&quot;Emma's first servlet&quot;);
    out.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);
    out.close();
  }
}
</pre>
<p>In contrast, a JSP is written using tags, similar to those used
in html. In fact, a JSP need not ever refer to Java or JSP code at
all, there may be times when you choose to represent your simple
html page using a .jsp file ending, in which case the tags WILL BE
html tags. In general, html and JSP code can be mixed together,
placing the JSP code within script tags. There is no need to define
the <tt class="methodname">doGet</tt> and <tt class=
"methodname">doPost</tt> methods or to place methods within try and
catch blocks to deal with exceptions.</p>
<pre class="programlisting">
&lt;%@ page import='java.io.* %&gt;
&lt;%@ page import='java.util.* %&gt;
&lt;html&gt;
&lt;body&gt;
  Emma's first JSP.
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>For TinyJet, the decision between Servlets and JSPs was made for
us. The team already had limited experience with Servlets and so it
was clear that the project was supposed to test our abilities with
the newer technology. This caused problems for some members who
were terrified at the thought of moving from one technology to
another - however, once we had started, the shift from Servlets to
JSPs proved an easy transition.</p>
<p>In order to implement JSPs, or indeed, Java Servlets, you will
need to run a Servlet engine. We used Jakarta's Tomcat although
J-Run is also a common choice for small or personal projects.
Tomcat can be downloaded for free from <a href=
"http://jakarta.apache.org/tomcat" target=
"_top">http://jakarta.apache.org/tomcat</a>. The Servlet engine can
function as a web-server or can be run alongside your existing
web-server. I should perhaps point out that members on our course
experienced difficulties with different versions of tomcat working
alongside different versions of the Java SDK. One combination that
definitely works is the latest Tomcat version (about 4.0) and an
older version of the Java SDK (1.2.1).</p>
<p>The Tomcat Servlet engine requires a fixed directory structure
that should be established before you start; this requires that
web-pages (either HTML or JSP) be put in a different folder to
Servlets or Java Beans which are held in a 'virtual directory'. It
is best to check your Servlet engine documentation.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e79" id="d0e79"></a>3-Tier Web
Applications - An OO Approach:</h2>
</div>
<p>The TinyJet project is a three-tier web application. The Java
code and web pages sit above two Access databases that store
information about Flight times, Flight and Hotel availability,
Customer details and Booking details.</p>
<p>Our challenge was to develop, using JSPs, an application that
used a number of Java classes to read from and write to these
databases. As such, we have tried as far as possible to keep the
JSPs responsible only for formatting information on the web page.
We have used a central tier of Java 'Beans' or specially written
stand-alone Java classes which actually perform the business and
programming logic that enables information to be put into and taken
out of the databases.</p>
<p>The way that we approached this development was to write classes
that modelled the real-life entities identified in the first
paragraph of this section: Flight, Hotel, Customer and Booking. We
developed different <tt class="classname">Customer</tt> and
<tt class="classname">Booking</tt> classes for the Hotel part of
the system as the Hotel database required different kinds of
information to the Flight database. For this reason, we developed
abstract classes to represent a <tt class="classname">Customer</tt>
and a <tt class="classname">Booking</tt>, and extended these
depending upon whether the information was for a <tt class=
"classname">HotelBooking</tt> or a <tt class=
"classname">FlightBooking</tt>.</p>
<p>In addition to these main classes, we developed two further
classes that enabled the developer responsible for producing JSPs
to print out lists of the <tt class="classname">Hotel</tt> or
<tt class="classname">Flight</tt> objects. These <tt class=
"classname">FlightList</tt> and <tt class=
"classname">HotelList</tt> classes contained methods that read from
the database, creating <tt class="classname">Vector</tt>s of
<tt class="classname">Flight</tt>s or <tt class=
"classname">Hotel</tt>s objects. The JSP, having called the method,
was able to use a forloop to run through the <tt class=
"classname">Vector</tt>, one object at a time, printing the
required attributes to the webpage. This is a very useful tool for
printing out a list of available <tt class="classname">Flight</tt>s
or a list of <tt class="classname">Hotel</tt>s in a given
destination.</p>
<pre class="programlisting">
&lt;%
for (int x=0; x&lt; myHotel.cityHotels.size();
     x++) {
  Hotel tempHotel =
         (Hotel)myHotel.cityHotels.get(x); %&gt;
  Name:  &lt;%=tempHotel.getName()%&gt; &lt;BR&gt;
  Price: &lt;%=tempHotel.getPrice()%&gt; &lt;BR&gt;
  &lt;IMG SRC='&lt;%=temp.getPicPath1()%&gt;' 

WIDTH=100&gt; &lt;BR&gt;
  Facilities: &lt;%=tempHotel.getFacilities()%&gt;
              &lt;BR&gt;
  &lt;%
}
%&gt;
</pre>
<p>The most important aspect of developing Java classes or 'beans'
for use with JSPs is to remember to provide <tt class=
"literal">get</tt> and <tt class="literal">set</tt> methods for all
variables. The JSP tag library provides tags not only for
instantiating classes but also for setting and getting object
properties, given that a <tt class="literal">set</tt> or <tt class=
"literal">get</tt> method is defined for them:</p>
<pre class="programlisting">
//instantiates a Hotel
&lt;jsp:useBean id='myHotel' class='HotelPackage.Hotel' /&gt;
//assigns a city to a Hotel
&lt;jsp:setProperty name='myHotel' property='city' value='Amsterdam' /&gt; 
//gets a Hotel's name
You selected: &lt;jsp:getProperty name='myHotel' property='title' /&gt;&lt;BR&gt;
</pre>
<p>The TinyJet application makes frequent use of the web user's
session object using the <tt class=
"methodname">session.setAttribute()</tt> and <tt class=
"methodname">session.getAttribute()</tt> Java methods to store the
user's selected options. It also makes good use of <tt class=
"literal">java.sql.*</tt> and <tt class="literal">java.text.*</tt>
methods that enabled us to convert date and currency formats from
the Access database into UK Java date and currency formats that
could be printed to the web page; something that can take quite a
while to get your head around.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e170" id="d0e170"></a>Outlook</h2>
</div>
<p>The TinyJet system was a fast application to develop. Even
though we had no prior JSP experience, the actual pages were very
simple to construct once the middle-tier classes had been
developed. We were able to apply attractive web designs using
traditional web tools such as DreamWeaver, and we also found it
easy to integrate client-side scripting with the server-side
JSPs.</p>
<p>I would suggest that those looking to start web development in
Java begin with JSPs as opposed to Servlets. I found the JSPs to
work very similarly to html pages and so an understanding of the
http and Servlet methods wasn't necessary. For those people with
prior Java experience, the transfer of your Java knowledge to JSPs
should be easily achieved although a background in basic web
development would be needed. When it comes from moving from C or
C++ to Java web development tools, I am afraid I am no help! I do
believe however that if I can pick it up as a beginner in
programming, it can't be too daunting for those with previous
programming knowledge (I hope!) My advice is not to be worried
about the transition because in my experience from TinyJet, it's
the worry that holds people back, not their ability.</p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e177" id="d0e177"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Falkner" id="Falkner"></a>
<p class="bibliomixed">[Falkner] Falkner. J., et al. (2001).
Beginning JSP Web Development. Birmingham: Wrox Press.</p>
</div>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
