Servlet Environment Setup
The development environment is where you can develop, test, and run Servlets.
Like any other Java program, you need to compile the Servlet using the Java compiler javac. After compiling the Servlet application, deploy it in the configured environment for testing and running.
If you are using the Eclipse environment, you can directly refer to: Eclipse JSP/Servlet Environment Setup.
This development environment setup includes the following steps:
Setting Up the Java Development Kit (JDK)
This step involves downloading the Java Software Development Kit (SDK) and properly setting the PATH environment variable.
You can download the SDK from Oracle's Java website: Java SE Downloads.
Once you have downloaded the SDK, follow the given instructions to install and configure it. Finally, set the PATH and JAVA_HOME environment variables to point to the directories containing java
and javac
, typically java_install_dir/bin
and java_install_dir
respectively.
If you are running Windows and installed the SDK in C:\jdk1.5.0_20
, you need to add the following lines to your C:\autoexec.bat
file:
set PATH=C:\jdk1.5.0_20\bin;%PATH%
set JAVA_HOME=C:\jdk1.5.0_20
Alternatively, in Windows NT/2000/XP, you can right-click "My Computer", select "Properties", then "Advanced", "Environment Variables". Update the PATH value and press the "OK" button.
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.5.0_20
and you are using the C shell, add the following lines to your .cshrc
file:
setenv PATH /usr/local/jdk1.5.0_20/bin:$PATH
setenv JAVA_HOME /usr/local/jdk1.5.0_20
Additionally, if you are using an Integrated Development Environment (IDE) such as Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, compile and run a simple program to confirm that the IDE knows your installed Java path.
For more detailed content, refer to: Java Development Environment Setup
Setting Up the Web Application Server: Tomcat
There are many Web application servers that support Servlets. Some of these servers are available for free download, and Tomcat is one of them.
Apache Tomcat is an open-source implementation of Java Servlet and JavaServer Pages technologies, which can act as a standalone server for testing Servlets and can be integrated into the Apache Web application server. Here are the steps to install Tomcat on your computer:
Download the latest version of Tomcat from http://tomcat.apache.org/.
Once you have downloaded Tomcat, unzip it to a convenient location. For example, if you are using Windows, unzip it to
C:\apache-tomcat-5.5.29
, and if you are using Linux/Unix, unzip it to/usr/local/apache-tomcat-5.5.29
, and create aCATALINA_HOME
environment variable pointing to these locations.
On Windows, you can start Tomcat by executing the following command:
%CATALINA_HOME%\bin\startup.bat
or
C:\apache-tomcat-5.5.29\bin\startup.bat
On Unix (Solaris, Linux, etc.), you can start Tomcat by executing the following command:
$CATALINA_HOME/bin/startup.sh
or
/usr/local/apache-tomcat-5.5.29/bin/startup.sh
After Tomcat starts, you can access the default application in Tomcat by entering http://localhost:8080/ in the browser's address bar. If everything goes smoothly, the following result will be displayed:
For further information on configuring and running Tomcat, refer to the application installation documentation or visit the Tomcat website: http://tomcat.apache.org.
On Windows, you can stop Tomcat by executing the following command:
C:\apache-tomcat-5.5.29\bin\shutdown
On Unix (Solaris, Linux, etc.), you can stop Tomcat by executing the following command:
/usr/local/apache-tomcat-5.5.29/bin/shutdown.sh
Setting CLASSPATH
Since Servlets are not part of the Java Platform Standard Edition, you must specify the path to the Servlet classes for the compiler.
If you are running Windows, you need to add the following lines to your C:\autoexec.bat file:
set CATALINA=C:\apache-tomcat-5.5.29
set CLASSPATH=%CATALINA%\common\lib\servlet-api.jar;%CLASSPATH%
Alternatively, on Windows NT/2000/XP, you can right-click "My Computer," select "Properties," then "Advanced," "Environment Variables." Update the CLASSPATH value and click "OK."
On Unix (Solaris, Linux, etc.), if you are using the C shell, you need to add the following lines to your .cshrc file:
setenv CATALINA=/usr/local/apache-tomcat-5.5.29
setenv CLASSPATH $CATALINA/common/lib/servlet-api.jar:$CLASSPATH
Note: Assuming your development directory is C:\ServletDevel (on Windows) or /user/ServletDevel (on UNIX), you also need to add these directories to the CLASSPATH in a similar manner to the above.