Viewing problem solution
Programming / Guides
Michael Girdley and
Kathryn A. Jones
My Articles at CodeGuru
Progress Bar in VB
SnR in VB Application
K Dev Planet
My Blog - Google
Fuzzy Logic Guide
PDF Conversion Guide
Java Servlets - Development & Deployment ------------------------------------------------------ There are so many articles on the Net providing the tutorial approach for developing, compiling and running the Java Servlets. However, when I jumped into the world of Servlet programming, I didn't find even a single document stating where and how one should proceed (step-by-step). To my horror I found that Servlet programming was not my cup of coffee. But since, I didn't give up - after hours of 'research' I found a way to get going. Now I want to share this with you. The first and foremost problem for a novice Servlet programmer is - how to compile? The second one is then, how to run? The latter being more complicated than former. For execution many authors suggest ServletRunner (an .exe which is assumed to be supplied with JSDK2.0) but since I downloaded and installed the latest (at the time of writing) JSDK i.e., v2.1, I couldn't find it in the installation directory (probably it has been dropped). I thus had to use the JSDK approach (instead of the ServletRunner or Tomcat, the latter being a web server - a Servlet engine) for testing the basic Java Servlets. This writing will give you to-the-point and comprehensive material that you can use to start writing and testing your own Servlets. REMEMBER that this is not a Java Servlet tutorial (if you are interested in one, kindly visit the Sun site at, http://java.sun.com/products/servlets) but merely a guide that you can use for knowing how to compile and run Java Servlets. So without further ado here we go. Design the Servlet (write down the code for .java file) and then *compile the source file with the javac compiler (either including -classpath switch or if the classpath already set, without it). This will generate one or more .class files. These class files contain the bytecode that the Servlet Engine will interpret at the time, the file referencing it, calls it. I assume that the JSDK 2.1 (i.e., Java Servlet Development Kit v 2.1 - Java Web Server) has been installed in the C:\jsdk2.1 directory. The root of your webpages will be a directory located as C:\jsdk2.1\webpages. This directory contains all the directories and files (.html files) that you want to publish. Once you have designed/created and developed the Servlet (.class file is ready), you need to place this(ese) file(s) (.class) in a directory located as C:\jsdk2.1\webpages\web-inf\servlets). The Java Server automatically checks this directory for any updated definitions of the Servlets. Then from an html file located in your webpages directory (or its subdirectories), mentioned earlier, you can call the Servlet. For example, If you Servlet is named HelloClientServlet.class and it has been placed in C:\jsdk2.1\webpages\web-inf\servlets directory, you can call it from your html file which is located in C:\jsdk2.1\webpages\myservlets named as HelloClient.html. The code for calling the same from your html file may vary depending upon you requirements, however the simplest approach to test your Servlets will be,
Run HelloClient Servlet
In the above code fragment, whenever the user clicks on the hyperlink the Servlet starts running. On the other hand if you want to invoke the Servlet via a form processing event then use,
This button will activate the HelloClient Servlet
Emphasize on the method attribute which is GET and not the POST. Contrary to this is POST, use it when you have override the doPost method in the main Servlet class (derived from HttpServlet, say). While accessing the html page from IE or other Web browser you need to supply the following URL, http://localhost:8080/myservlets/HelloClient.html REMEMBER that there is no such directory called 'servlet' (emphasize on the singular term, as opposed of the servlets), however still you will have to supply this in your html file, in order to access the Servlet. I have used ../ here to depict the root, you can write down the full path as,
Run HelloClient Servlet
Also remember, that if you have modified the file called servlets.properties located in C:\jsdk2.1\webpage\web-inf i.e., have added the following line, HelloCS.code=HelloClientServlet Then you can access the Servlet from you html file as,
Run HelloClient Servlet
too, where HelloCS works as an alias for the HelloClientServlet Servlet. Make it a point that server is not running while you are updating the .class or .properties files or the servlet won't reflect the changes. If the server is already running it, first shut it down. Compile the .java file(s), update the servlets.properties file appropriately and finally run the server. ------------------------------------------------------- * Classpath environmental variable under Windows 98 can be set as writing down the following line in the autoexec.bat file, this is because whenever your PC boots, you will require to set the Classpath, SET CLASSPATH=%CLASSPATH%;C:\jsdk2.1\servlet.jar This is required because all the packages and/or classes your Servlet references are there in this servlet.jar file which is noting but an archived (compressed) java-bytecode file, containing all the classes and packages. If you don't set up the classpath environment variable you can still compile the source files (.java servlets) as, C:\> javac -classpath "C:\jsdk2.1\servlet.jar"
But since this approach is quite cumbersome approach I will recommend you using former. ________________________________________________________________________ This document has been written with reference to JSDK 2.1 and may (not) be incompatible with earlier versions. © 2005 Krishna Kumar Khatri.
All rights reserved. Copyright © 1999 -
Krishna Kumar Khatri
Best viewed with Microsoft IE 6 or later, under 800x600 resolution with True Color (24-bit).