Using Java Servlets with Database Connectivity
The Common Gateway Interface (CGI) has and continues to be the most commonly used method for creating dynamic and responsive web pages. The main problem with CGI (that stems from the Hyper-Text Transfer Protocol) is that each new client request results in a new instance of the CGI executable being forked by the HTTP daemon. This can lead to considerable resource consumption on web hosting machinery. Many solutions exist to address this problem, most focusing on keeping the executable persistent between client requests. This has the added benefit of holding open costly resources like database and socket connections.
Java is one of the newest kids on the block, but as a C and C++ coder I really like some of the features of this language. Its object model is nice, it is (relatively) portable, and the class libraries available from both Sun and third parties are extensive. Servlets, a rather fanciful name, is the Java answer to the CGI problem. Servlets are Java classes, loaded and kept resident by the HTTP daemon. When the servlet is loaded, it is initialized by a method call; at that point, database connections can be established and held between client requests. In addition to this, there are a number of useful classes which facilitate the more complex server/browser interactions such as cookies. Unfortunately, the Servlet classes from Sun are still in a fluid state and, therefore, code written now may be broken by future releases. This is a fact of programming life and since this is a small application, not much harm is likely.
Since my site has been running the ubiquitous Apache web server, it was an easy choice to stick with this software. The Servlet part was the more tricky decision. I decided to play it safe and get the Apache-Java solution (http://java.apache.org/). This is not a finished product, but it certainly looked acceptable, and I wanted to have access to the source if anything broke. Alternatives include Live Software's JRun and IBM's Servlet Express. JRun is able to work with a number of web servers including Apache and Netscape, and the code is available under a fairly lenient license. Servlet Express is more of an unknown quantity—that is, I did not even evaluate it since it is only available for Apache 1.2.
The source code for JServ is available for download from the Java-Apache site, and the version I used was 0.9.11. Unpack the code using tar into a suitable directory (/usr/local/lib/JServ on my machine) and read the documentation carefully. A Makefile does most of the magic. The Makefile does need to be edited to modify the installation directory—alas, no autoconf. A recent JDK is required—I used Steve Byrnes 1.1.6-5 which I have found to be both fast and stable. I also have the tya 1.1-3i JIT (Just In Time Compiler) which quite transparently super-charges the execution of Java code, but it is not necessary. Make the JServ code to compile the Java source files to a local directory. Included in the JServ package is the requisite Servlet code from Sun. Both the Sun Servlet code (in the form of a jar file) and the JServ code need to be added to your CLASSPATH variable for both development and Apache.
Now it's Apache's turn. Get a nice, recent copy—I have 1.3.1, which I find to be a splendid creation—and unpack the source code into another directory. The magic of building an external Apache module is described in the INSTALL file in the root of the /apache directory. In order to compile the Apache daemon, it is first necessary to “configure” the Apache build environment. This is done through the wonderfully arcane autoconf-produced script configure. You merely need to add the following to the ./configure line:
This will produce a Makefile that will copy the mod_jserv.c file to the ./src/modules/extra directory and compile it into Apache. Note that merely copying the mod_jserv.c file into the extra directory will not accomplish anything. In order to make this new module shared (assuming DSO in apache is configured), add the following line to the configure command line:
--enable-shared=jservThis option will produce a .so shared object which is dynamically linked to Apache when necessary.
Obviously some care is required. I cribbed most of the details from the apache.spec file from the apache SRPM; I advise you to get this file and install it. Make the changes in the spec file and rebuild the RPM (rpm -ba apache.spec), then install the new RPM. This applies only to RPM-based systems (Red Hat, SuSE, Caldera, etc).
- You have to be careful there
7 weeks 3 days ago
- Wonder when LJ is going to
7 weeks 4 days ago
- Puerto Rico Free Software
7 weeks 5 days ago
7 weeks 6 days ago
- I hate voice commands
8 weeks 4 hours ago
- usabilty --- AGAIN with this nonsense
8 weeks 6 hours ago
- Don't make excuses
8 weeks 10 hours ago
- Sorry to let you know, but
8 weeks 10 hours ago
- Ridiculous statement. Not a
8 weeks 1 day ago
8 weeks 1 day ago