|
Java All-In-One Desk Reference For Dummies Authors: Lowe D. Published year: 2004 Pages: 239/332 |
Unfortunately, you can't just run servlet programs on any old computer. First, you have to install a special program called a servlet engine to turn your computer into a server that's capable of running servlets. The best-known servlet engine is called Tomcat, and it's available free from the Apache Software Foundation at http://www.tomcat.apache.org.
Tomcat can also work as a basic Web server. In actual production environments, Tomcat is usually used in combination with a specialized Web server, such as Apache's HTTP Server.
Installing Tomcat isn't rocket science, but it's not as easy as making toast . Here are the steps you can follow to set up Tomcat 5.5 on a Windows XP system:
Download the Tomcat Zip file.
You find the Zip file on the Apache Web site. Although Apache also offers an executable setup file for installing Tomcat, I suggest you download the Zip file instead.
Extract the contents of the Zip file by right-clicking the file and choosing Extract All. Then specify c:\ as the location to extract the files to.
I know you don't want to clutter up your root directory with a bunch of files, but the Tomcat Zip file contains a single folder named apachetomcat-5.5.20 (the version number may vary), so only this one folder is created. After all the files are extracted, rename this folder to something a little easier to type. I suggest c:\tomcat .
Create an environment variable named JAVA_HOME that points to the location of your JDK.
To create an environment variable, open the Control Panel, double-click the System icon, click the Advanced tab, and then click Environment Variables. Then click New and create a variable named JAVA_HOME . The value of this variable needs to be the complete path to your JDK installation folder. For example: c:\Program Files\Java\jdk1.6.0 .
| Tip |
A common mistake is to set this variable to the bin directory or to the directory for the JRE, not the JDK. If Tomcat doesn't start up later, double-check the JAVA_HOME directory. |
Copy the servlet-api.jar file to the jre\lib\ext folder in your JDK root.
For example, if your JDF is installed in c:\Program Files\Java\ jdk1.6.0 , copy this file to c:\Program Files\Java\jdk1.6.0\ jre\lib\ext .You find the servlet-api.jar file in c:\tomcat\ common\lib , assuming you extracted the Tomcat files to c:\tomcat .
| Tip |
If you skip this step or copy the servlet-api.jar file to the wrong place, you can't compile your servlet programs. If you get compiler messages complaining that the javax.servlet package doesn't exist, double-check that you performed this step right. |
Edit the context.xml configuration file and add reloadable=“true” to the <context> tag.
The context.xml file is located in c:\tomcat\conf . The second line is initially this:
<Context>
Change it to:
<Context reloadable="true">
Modify the web.xml file to enable the invoker servlet.
Like context.xml , the web.xml file is located in c:\tomcat\conf . It contains two groups of lines that configure a Tomcat feature called the invoker servlet that you need to modify. These lines are initially commented out to disable the invoker servlet; all you have to do is remove the comment lines that appear before and after each group of lines.
The first group you want to de-comment looks like this:
<!-- <servlet> <servlet-name>invoker</servlet-name> <servlet-class> org.apache.catalina.servlets.InvokerServlet </servlet-class> <init-param> <param-name>debug</param- name > <param-value>0</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> -->
Simply remove the first ( <!– ) and last ( –> ) of these lines.
The second group looks like this:
<!-- <servlet-mapping> <servlet-name>invoker</servlet-name> <url-pattern>/servlet/*</url-pattern> </servlet-mapping> -->
Once again, you must remove the first and last lines so these lines aren't treated as comments.
| Tip |
You can quickly find these lines by searching for the word invoker . |
Create the classes directory.
By default, Tomcat looks for the class files for your servlets in the directory c:\tomcat\webapps\ROOT\WEB-INF\classes . Unfortunately, the classes directory is missing. So you must navigate to c:\tomcat\ webapps\ROOT\WEB-INF and create the classes directory. (Of course, the c:tomcat part of these paths varies if you installed Tomcat in some other location.)
After you install and configure Tomcat, you can start it by opening a command window, changing to the c:\tomcat\bin directory, and typing startup . A batch file runs that starts Tomcat. When Tomcat starts, it opens up a second command window that displays various status messages. Figure 2-1 shows both of these windows in action.
Figure 2-1:
Starting up Tomcat.
| Tip |
You know that Tomcat has successfully started up when you see a line such as the following indicating how long the startup took: INFO: Server startup in 2817 ms |
| Tip |
If the Tomcat window appears for a few seconds, and then an exception message flies by quickly and the window closes , the most likely problem is that you already have a Web server running on your system and that the server has already laid claim to the port Tomcat wants to use for HTTP communication. The solution to that problem is to edit the server.xml file in c:\tomcat\conf and look for this tag: <Connector port="8080" ... /> |
Change the port number from 8080 to some other number, such as 18080. Later, when you display servlets in a browser window, you have to specify this number as the HTTP port number instead of 8080.
You don't need to shut down Tomcat once you start it up unless you make a change to one of its configuration files. If you do, you can shut down Tomcat by running the shutdown batch file from the c:\tomcat\bin directory. Then you can run the startup batch file to get Tomcat going again.
To find out if you have installed Tomcat correctly, you can try running the test servlets that are automatically installed when you install Tomcat. Open a browser window and type this address:
http://localhost:8080/servlets-examples/index.html
(If you changed the port number by editing the server.xml file, use the port number you specified instead of 8080.) The page shown in Figure 2-2 appears. (If it doesn't, go to the earlier section "Installing and configuring Tomcat" and double-check that you did each step correctly.)
Figure 2-2:
Testing Tomcat.
| Note |
If you scroll down this page, you find links to a variety of sample servlets you can run along with links to each servlet's source code. By all means play around with these samples to get an idea of how servlets work and what you can do with them. |
|
Java All-In-One Desk Reference For Dummies Authors: Lowe D. Published year: 2004 Pages: 239/332 |
![]() C++ All-In-One Desk Reference For Dummies | ![]() Android Application Development For Dummies | ![]() Head First Java | ![]() Beginning Programming with Java For Dummies |
![]() C++ All-In-One Desk Reference For Dummies | ![]() Android Application Development For Dummies |
![]() Head First Java | ![]() Beginning Programming with Java For Dummies |