Journal Articles
Browse in : |
All
> Journals
> CVu
> 144
(17)
All > Topics > Programming (877) Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. 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/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: My First Steps in Java Web Development
Author: Administrator
Date: 03 August 2002 13:15:54 +01:00 or Sat, 03 August 2002 13:15:54 +01:00
Summary:
Body:
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.
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.
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!!!!
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 "Beginning JSP Web Development" (Falkner) 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.
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.
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.
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.
A basic Servlet must import the javax.servlet libraries used for developing http applications. It must also extend the HttpServlet class provided in the library in order that you may override the doGet and doPost methods of the webpage.
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("<html><body>"); out.println("Emma's first servlet"); out.println("</body></html>"); out.close(); } }
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 doGet and doPost methods or to place methods within try and catch blocks to deal with exceptions.
<%@ page import='java.io.* %> <%@ page import='java.util.* %> <html> <body> Emma's first JSP. </body> </html>
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.
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 http://jakarta.apache.org/tomcat. 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).
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.
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.
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.
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 Customer and Booking 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 Customer and a Booking, and extended these depending upon whether the information was for a HotelBooking or a FlightBooking.
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 Hotel or Flight objects. These FlightList and HotelList classes contained methods that read from the database, creating Vectors of Flights or Hotels objects. The JSP, having called the method, was able to use a forloop to run through the Vector, 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 Flights or a list of Hotels in a given destination.
<% for (int x=0; x< myHotel.cityHotels.size(); x++) { Hotel tempHotel = (Hotel)myHotel.cityHotels.get(x); %> Name: <%=tempHotel.getName()%> <BR> Price: <%=tempHotel.getPrice()%> <BR> <IMG SRC='<%=temp.getPicPath1()%>' WIDTH=100> <BR> Facilities: <%=tempHotel.getFacilities()%> <BR> <% } %>
The most important aspect of developing Java classes or 'beans' for use with JSPs is to remember to provide get and set 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 set or get method is defined for them:
//instantiates a Hotel <jsp:useBean id='myHotel' class='HotelPackage.Hotel' /> //assigns a city to a Hotel <jsp:setProperty name='myHotel' property='city' value='Amsterdam' /> //gets a Hotel's name You selected: <jsp:getProperty name='myHotel' property='title' /><BR>
The TinyJet application makes frequent use of the web user's session object using the session.setAttribute() and session.getAttribute() Java methods to store the user's selected options. It also makes good use of java.sql.* and java.text.* 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.
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.
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.
Notes:
More fields may be available via dynamicdata ..