Friday, August 15, 2008

JSP Quick-Start Guide for Linux

Downloading and Installing Tomcat


Since Apache doesn't support JSP out of the box, we need to add something else to the mix to provide that support. Unfortunately, JSP is too complicated to be supported by a simple Apache module; thus, we need to install an entirely new program to provide that support, then instruct Apache to forward requests for JSP pages to that program (note: this may sound like CGI, but it isn't; just wait and see). The program we'll be using is called Tomcat, and is also written by the Apache Group.


Tomcat is in fact a simple Web server in its own right. It doesn't support any of the advanced features of Apache, however; that's not its job! By linking Tomcat and Apache together, you get a system that provides full support for JSP (thanks to Tomcat) while maintaining the performance and expandability (PHP, Perl, SSL, etc.) of Apache.


Your first step should be to download and install the Java 2 Software Development Kit (Java 2 SDK) from Sun. This is required both to run Tomcat (which is a Java program), and for Tomcat to be able to compile JSPs for use. The current version of the Java 2 SDK (also called the JDK) as of this writing is version 1.4.1_01, and is available for download here. Download the ~42MB "Linux self-extracting file" (be sure to download the SDK, not the JRE!). The filename should be j2sdk-1_4_1_01-linux-i586.bin. Extract this file into your /usr/local directory with this series of commands:


[kevin@sp kevin]$ su

Password: ********

[root@sp kevin]# chmod u+x j2sdk-1_4_1_01-linux-i586.bin

[root@sp kevin]# mv j2sdk-1_4_1_01-linux-i586.bin /usr/local/

[root@sp kevin]# cd /usr/local

[root@sp local]# ./j2sdk-1_4_1_01-linux-i586.bin


You can delete the j2sdk-1_4_1_01-linux-i586.bin file now, or put it someplace safe for future use; you're done with it.


Java is now installed, but you need to do a few things to settle it smoothly into your system. First, make a symbolic link so you can access the Java installation with the easier-to-type name /usr/local/java:


[root@sp local]# ln -s j2sdk1.4.1_01 java


Next, make links to the important programs that make up the Java SDK in the /usr/local/bin directory, so you can run them from anywhere:


[root@sp local]# ln -s java/bin/java /usr/local/bin/java

[root@sp local]# ln -s java/bin/javac /usr/local/bin/java

[root@sp local]# ln -s java/bin/jar /usr/local/bin/java


Now, we need to edit a couple of environment variables on your system. Still as root, open the /etc/profile on your system in your preferred text editor. Look for a line beginning with PATH=. When you type a program name at the command prompt, this line sets the directories that your system checks for that program. Check to make sure /usr/local/bin is in the list. If it isn't, add the following line right after the existing PATH= line:


PATH="$PATH:/usr/local/bin"


You also need to set the JAVA_HOME environment variable to point to your Java installation. To do this, scroll right down to the bottom of the file and add these two lines to the end:


JAVA_HOME="/usr/local/java"

export JAVA_HOME


Save the changes, log out, and then log back in. Java should now be nicely installed and integrated with your system. To test it, you can type java -version at the command prompt and make sure that it reports your Java version correctly.


Now you're ready to install Tomcat. Download the latest release from the Jakarta Project Web site. As of this writing, the latest version was 4.1.18, and was available at http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.1.18/bin/. The file should be called jakarta-tomcat-4.1.18-LE-jdk14.tar.gz, and is about 5.1MB.


Once you've downloaded the file, move it to your /usr/local directory and extract it:


[kevin@sp kevin]$ su

Password: ********

[root@sp kevin]# mv jakarta-tomcat-4.1.18-LE-jdk14.tar.gz /usr/local/

[root@sp local]# tar xfz jakarta-tomcat-4.1.18-LE-jdk14.tar.gz


You can delete the jakarta-tomcat-4.1.18-LE-jdk14.tar.gz file now, or put it somewhere safe for future use; you're done with it.


As with the Java SDK, create a link to the installation directory with a neater name for convenience:


[root@sp local]# ln -s jakarta-tomcat-4.1.18-LE-jdk14 tomcat


Like Java, Tomcat requires an environment variable of its own to be set for it to run properly. Open up your /etc/profile file one more time and add these two lines to the end:


CATALINA_HOME="/usr/local/tomcat"

export CATALINA_HOME


Once again, save your changes, log out, then log back in for changes to take effect.


You're ready to start Tomcat! Log in as root and type the following command to launch it:


[root@sp kevin]# $CATALINA_HOME/bin/startup.sh


If all goes well, the startup.sh script should display the values of a few environment variables, and then Tomcat will launch quietly in the background. Congrats -- Tomcat is up and running!


As I said before, Tomcat provides its own simple Web server, and we can use this to test that it is working properly. Tomcat's Web server is set up to use port 8080 by default, so open your Web browser and load http://localhost:8080/. You should see the Tomcat home page, with links to some examples that come preinstalled on the server.


Have a play with these examples if you're curious, and notice how the JSP examples take considerable time to load the first time you use them, but then work very quickly upon subsequent accesses. This happens because the first time a JSP page is accessed, Tomcat needs to compile it into a Java Servlet, which is a piece of pure Java code that can be run very quickly to process requests. That Servlet is then held in memory to process subsequent requests for the same JSP at blazing speed.


When you're done playing with Tomcat, shut it down by running the shutdown.sh script the same way you ran the startup.sh script above ($CATALINA_HOME/bin/shutdown.sh). Tomcat will shut down after a few moments.


Running Tomcat Automatically

If you've got Apache running automatically at system start-up, you'll probably want to do the same for Tomcat. Start by copying (not linking) the catalina.sh script that came with Tomcat to your /etc/init.d/ directory:


[root@sp kevin]# cd /etc

[root@sp etc]# cp /usr/local/tomcat/bin/catalina.sh init.d/


This script relies on the JAVA_HOME and CATALINA_HOME environment variables we set earlier, but these variables do not exist while the system is starting up. We must therefore edit the script a little so that it can serve as a system startup script.


Open the file in your favourite text editor, and scroll down to the first blank line following the opening comments (any line that begins with a # is a comment). Add these two lines at that point:


JAVA_HOME="/usr/local/java"

CATALINA_HOME="/usr/local/tomcat"


This script actually makes an attempt at detecting CATALINA_HOME itself, but this attempt backfires if the script is not actually in the Tomcat directory (which in this case it isn't). Scroll down and look for a line that begins with CATALINA_HOME=. It should look like this:


CATALINA_HOME=`cd "$PRGDIR/.." ; pwd`


Comment it out by putting a # at the start of that line:


#CATALINA_HOME=`cd "$PRGDIR/.." ; pwd`


That completes the adjustments to catalina.sh. You can now assign this as a start-up script in the various runlevels (operating modes) of your computer:


[root@sp etc]# ln -s init.d/catalina.sh rc2.d/S90tomcat

[root@sp etc]# ln -s init.d/catalina.sh rc3.d/S90tomcat

[root@sp etc]# ln -s init.d/catalina.sh rc4.d/S90tomcat

[root@sp etc]# ln -s init.d/catalina.sh rc5.d/S90tomcat

[root@sp etc]# ln -s init.d/catalina.sh rc0.d/K19tomcat


That should do it! To try it out, shut down your computer, start it back up again, and see if you can load the Tomcat home page at http://localhost:8080/.


 

No comments: