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
A507 - Web Application Deployment - Java - Servlets and JSPs [479] New servlet from old - (2005-10-28)
[2151] What is a JSP tag library? - (2009-05-01)
[2525] Using JSPs, Tag Libraries, Java Beans, Tomcat in one short example - (2009-12-04)
[3044] Changing a Servlet - more that just editing and compiling - (2010-11-12)
[3997] Servlet v JSP (Java Server Page). What is the difference? - (2013-02-06)
J714 - Java - Fundamental classes [42] Do languages change? - (2004-09-08)
[1062] Java sorting - comparable v comparator - (2007-02-02)
[1502] Java, sorting, ArrayList example, generics - (2008-01-11)
[1910] Java - Generics - (2008-11-27)
[2323] Java Collection Objects in the java.util package - (2009-08-05)
[2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
[2421] Sorting Collections of Objects in Java - (2009-09-25)
[2649] Length, size or capacity in Java? - (2010-02-24)
[2734] for and foreach in Java - (2010-04-22)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3048] String handling - from first steps to practical examples - (2010-11-13)
[4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
[4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
[4421] How healthy are the stars of stage and screen? - (2015-02-09)
J906 - Java - Servlets in More Detail [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
[1550] Java (JSP and Servlet examples) live on our server - (2008-02-23)
[1909] javax.servlet cannot be resolved - how to solve - (2008-11-26)
[2183] Servlet life cycle, and Java Servlet variable scope - (2009-05-16)
[2652] Reading and writing cookies in Java Servlets and JSPs - (2010-02-26)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[3293] Distributing the server load - yet ensuring that each user return to the same system (Apache httpd and Tomcat) - (2011-05-18)
[4432] Java web application for teaching - now with sessions and clustering / load balancing demonstrations - (2015-02-20)
Some other Articles
Images of our rail promotion campaign Public training courses - upcoming datesDifferent views of a Welsh Valley - but headed homeA Java servlet that is also a stand alone program. And a server that is also a web client.The spirit of Java - delegating to classesWind Turbines - beauty or menace?Using the lead - passing arrays and other collections in JavaJava example - for loop and conditionals from course exerciseFileMaker Day to Unix Time conversion