Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
A Java servlet that is also a stand alone program. And a server that is also a web client.

I've been teaching the fundamentals of Java (a private 4 day course) to a number of groups over the past few weeks - each time tailoring the course towards the projects that the delegates (within a large organisation) will be using it for, and also tailoring to suite the background of the delegates. This week, my delegates turned out to be rather more experienced programmers than the earlier groups, an they have a target application that's web based. So on the final day - today - we took teaching examples that we had been working on and added a servlet interface.

The main application class is [here] and includes both a main method for it to run stand alone, and a doGet method for it to run as a servlet.

• In order to avoid code repetition, both the main and the doGet method call a protected action method.

• Since main is static and doGet is not, the main method creates an object on which it can run the action method.

• Both methods set up a PrintWriter for output; the main method explicitly closes it (otherwise buffers may not be flushed, and some data lost) and the doGet method does not close it (leaving it available to the Apache Tomcat server).

This program also contains an example of a Comparator class, since we sorted a whole collection of objects in a "non-natural" way - the natural way to sort places is alphabetically, but one of my delegates fancied sorting them from north to south - so that's what we did all the way from Thurso and Georgemas to Falmouth and Penzance. The comparator source code is [here].

The data used by this program is located on our main server, and so the servlet becomes itself a client to another web server, from which it reads the data URL. Our MyStream class - [here] - actually includes two constructors to open the data source stream - a single parameter version for a local file, and a two parameter version for a remove file via a web protocol, using a java.net.URL object and calling an openStream method on it. Polymorphic code then allows the same method readLine to read either locally or remotely.

The final source code in this example is our Station class - see [here]. You will note (if you've read some of the more blog articles posted earlier this week) that I'm re-using code. And that's exactly what I should be doing with Java - the initial developement of my classes may be a little more longwinded than it would be in Perl, Ruby or Python - but their robustness and reliability then makes them an excellent fit for the large projects being undertaken by this customer.
(written 2015-02-19)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J906 - Java - Servlets in More Detail
  [4432] Java web application for teaching - now with sessions and clustering / load balancing demonstrations - (2015-02-20)
  [3293] Distributing the server load - yet ensuring that each user return to the same system (Apache httpd and Tomcat) - (2011-05-18)
  [3044] Changing a Servlet - more that just editing and compiling - (2010-11-12)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2652] Reading and writing cookies in Java Servlets and JSPs - (2010-02-26)
  [2183] Servlet life cycle, and Java Servlet variable scope - (2009-05-16)
  [1909] javax.servlet cannot be resolved - how to solve - (2008-11-26)
  [1550] Java (JSP and Servlet examples) live on our server - (2008-02-23)
  [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
  [479] New servlet from old - (2005-10-28)

J714 - Java - Fundamental classes
  [4421] How healthy are the stars of stage and screen? - (2015-02-09)
  [4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
  [4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2734] for and foreach in Java - (2010-04-22)
  [2649] Length, size or capacity in Java? - (2010-02-24)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [2323] Java Collection Objects in the java.util package - (2009-08-05)
  [1910] Java - Generics - (2008-11-27)
  [1502] Java, sorting, ArrayList example, generics - (2008-01-11)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [42] Do languages change? - (2004-09-08)

A507 - Web Application Deployment - Java - Servlets and JSPs
  [3997] Servlet v JSP (Java Server Page). What is the difference? - (2013-02-06)
  [2525] Using JSPs, Tag Libraries, Java Beans, Tomcat in one short example - (2009-12-04)
  [2151] What is a JSP tag library? - (2009-05-01)

Back to
The spirit of Java - delegating to classes
Previous and next
Horse's mouth home
Forward to
Java web application for teaching - now with sessions and clustering / load balancing demonstrations
Some other Articles
Images of our rail promotion campaign
Public training courses - upcoming dates
Different views of a Welsh Valley - but headed home
A Java servlet that is also a stand alone program. And a server that is also a web client.
The spirit of Java - delegating to classes
Wind Turbines - beauty or menace?
Using the lead - passing arrays and other collections in Java
Java example - for loop and conditionals from course exercise
FileMaker Day to Unix Time conversion
4759 posts, page by page
Link to page ... 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 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4431_A-J ... ient-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat