Tomcat is another excellent JSP server developed on the basis of Sun's JSWDK, which not only supports servlet, but also provides the functionality of the web server. As an open source software, Tomcat has its own unique advantage, working with most mainstream servers, and has a considerable operating efficiency.
Through this chapter, the reader will see:
· Tomcat's basic concept
· How to make Tomcat and IIS work together
· How to make Tomcat and Apache work together
· How to set the JSP virtual directory
· How to set Tomcat to Windows service
JAKARTA-TOMCAT Overview: Tomcat is a servlet container with a JSP environment. The Servlet container is a shell that manages and activates the servlet based on the user's behavior.
We can roughly divide the servlet container into several categories:
The stand-alone servlet container has part of the web server. Refers to the use of Java-based web servers, such as a Servlet container is a part of JavaWebserver. The stand-alone servlet container is the default mode of Tomcat.
However, most web servers are not based on Java. Therefore, we can draw the models of the following two containers:
The Servlet container servlet container in the process is the implementation of the plugin of the web server and the implementation of the Java container. The web server plugin opens a JVM (Java Virtual Machine) to enable the Java container to run internally. If a request to call the servlet, the plugin will acquire the control of this request and deliver it (using JNI) to the Java container. Process Content For multi-threaded, single-process server is very suitable and provides a good running speed, but lack of scalability.
The Servlet container Servlet container runs in the address space other than the web server and the combination of the plugin of the web server and the implementation of the Java container. The web server plug-in and Java container JVM communicates with the IPC mechanism (usually TCP / IP). When a request for calling a servlet arrives, the plugin will acquire the control of this request and deliver it (using IPC, etc.) to the Java container, the process outer container is not as good as the process instrument, but the process outer container The engine is better (scalability, stability, etc.) in many other comparable ranges.
Tomcat can be used as a stand-alone container (mainly for development and commissioning), but also as an additional server (currently supporting Apache, IIS, and Netscape servers). So when you configure Tomcat, you must decide how to apply it. If you select the second or third mode, you also need to install a web server interface.
What is the difference between Tomcat and JSERV? Is Tomcat JSERV? This is a common misunderstanding. Jserv is compatible with Servlet API2.0 and is used with Apache. Tomcat is a fully rewritten and container compatible with Servlet API2.2 and JSP1.1.
In addition, Tomcat uses some code written for JSERV, especially Jserv's Apache interface, but this is the same thing.
How to install Tomcat's binary version? Very simple, just: download zip / tar.gz any compressed file, from http://jakarta.apche.org/download/binindex.html to a directory (such as: foo), will generate one Subdirectory, named "Tomcat", convert to "Tomcat" directory Setting up a new environment variable (Tomcat_home) point to the Tomcat of the Tomcat: Win32 Platform, type: set tomcat_home = foo / Tomcat
UNIX platform: such as a Bash / SH environment, type: tomcat_home = foo / tomcat; export tomcat_home "If the TCSH environment, type: setenv tomcat_home foo / tomcat Setting the environment variable java_home points to your JDK directory, then add the Java interpreter to your PATH Environmental variables. Ok! You can now run Tomcat and start the Script of Script in Tomcat using the "bin" directory as a startup Tomcat. Startup:
UINX: BIN / Startup.sh Win32: BINSTARTUP
shut down:
UNIX: bin / shutdown.sh win32: binshutdown Tomcat directory structure
Suppose the reader has decompressed Tomcat, and will get the following directory structure:
Directory name
description
BIN contains start / turn off script. Conf contains different profiles, including server.xml (main configuration files for Tomcat) and file web.xml for default values for web applications configured for different Tomcat. The DOC contains a variety of Tomcat documents. LIB contains the JAR file used by Tomcat. All files in this directory are added to the classpath of Tomcat in this directory. Logs Tomcat places where log files are placed. SrcServletAPI source file. Don't happy, there are only empty interfaces and abstractions that must be implemented in the servlet container. WebApps contains a web project example. Worktomcat automatically generates, place TMCAT runtime temporary files (such as compiled JSP files), if this directory is deleted when the Tomcat is run. The JSP page will not be able to run. Classes You can create this directory to add some additional categories to classpaths, any class you add to this directory to find itself in the Tomcat's classpath.
Tomcat's script Tomcat is a Java program, so you can run the way to run the command line after setting several environment variables. However, each environment variable used in Tomcat and the following command line parameters are bored and easily errors. Therefore, the Tomcat development group provides some scripts to make the startup and close Tomcat very easily. Note: These scripts are just a convenient way to start and close Tomcat. You can modify them to customize ClassPath, environment variables such as Path, LD_Library_Path, etc., as long as it produces a correct command line. What is these scripts? The most important script for the general user is listed below. Tomcat: The main script. Set the appropriate environment variable, including ClassPath, Tomcat_Home, and Java_Home and start Tomcat with the appropriate command line parameters. STARTUP: Start Tomcat in the background. It is an alternative to the "Tomcat Start" command. Shutdown: Close Tomcat. It is an alternative to the "Tomcat Stop" command. The most important script for the user is Tomcat (Tomcat.bat / Tomcat.sh). Other Tomcat-related scripts as a simple-oriented entry to Tomcat scripts (setting different command line arguments, etc.). Carefully examine tomcat.bat / tomcat.sh, which follows the steps below: The steps under UNIX: If not specified, it is specified that tomcat_home is not specified, and the java_home setting class_path contains: 1. $ {Tomcat_home} / class directory (if there is) 2. $ {Tomcat_Home} / lib 3. $ {Java_home} /lib/tools.jar (This JAR file contains tool javac, we need JAVAC processing JSP files) Run the java command with command line parameters with setting Java environment variables, transfer Tomcat.home and Org.Apache .tomcat.startup.tomcat acts as start classes. At the same time, command line parameters are also transmitted to org.apache.tomcat.startup.tomcat, such as operations such as start / stop / run. Alternatively, this Tomcat process uses a path to Server.xml, such as server.xml placed in ETC / Server_1.xml and the user is intended to start apache in the background, type the following command line: bin / tomcat.sh start -f / etc / server_1. The XML Win32 version of Tomcat.Bat is almost consistent with UNIX version. In particular, it does not speculate the value of Tomcat_Home and Java_Home, and does not place all JAR files into the class path. This section of Tomcat's configuration file will tell how to use the configuration file server.xml and web.xml. We will not include the internal mechanism of Web.xml, these internal mechanisms go deep into the details of the Servlet API, so we will discuss the usage of these two configuration files in the Tomcat relationship environment. Tomcat configuration is based on two profiles: 1. Server.xml - Tomcat's global configuration file, complete two goals: (1) Provide initial configuration (2) of Tomcat components (2) Describe the structure of Tomcat, so that Tomcat is started and constructs itself through instantiation components. Structure, meaning of meaning is achieved by elements, and these elements will be described below. Server: The most important element in the Server.xml file.
Server defines a Tomcat server. General readers don't have to worry too much. Server elements can contain Logger and ContextManager elements. Logger: This element defines a logger object, each logger has a name to logo, and there is a record Logger output and redundancy level (describing this log level) and a path to the log file, usually has a servlet's Logger (servletContext. Logger at the log (), JSP and Tomcat. ContextManager: It illustrates a set of ContextInterceptor, RequestInterceptor, Context, and the configuration and structure of their Connectors. CONTEXTManager has several features that are provided: a. Used to record debug level B. WebAPPS /, CONF /, LOGS / and basic locations of all defined environments. Used to make Tomcat can start in other directories outside Tomcat_home. C. The name of the working directory ContextInterceptinter: These listeners: These listeners are listened to events that occur in ContextManager, for example, ContextInterceptor listens for Tomcat's launch and termination event, and Requestinterceptor monitors different phases required to pass the user during its service. Tomcat administrators don't have to know how much about listeners; in addition, developers should know how this is how to implement a "global" operation in Tomcat (such as security and each request log). Connector: Represents a connection to the user, whether it is through the web server or directly to the user browser (in a stand-alone configuration). The Connector is responsible for managing Tomcat's working threads and read / write to request / respond to ports of different users. The configuration of the Connector contains information: a. Handle B. The handle of the TCP / IP port C. The TCP / IP of the handle server port: Each context provides a subordinate directory that pointing you to the Tomcat of your Web project. Each context contains the following configuration: a. The path placed in the Context can be the path B related to the ContextManager home directory. Record debugging level C. Overloaded sign. When developing servlets, overloading the renaissable servlet, this is a very convenient feature 2. Web.xml - Configuring different relationship environments in Tomcat has a small "feature" related to Tomcat related to Web.xml. Tomcat allows users to define the default values of all the Web.xml of all relationship environments by putting the default web.xml into the confed directory. When establishing a new relationship environment, Tomcat uses the default web.xml file as basic settings and application project-specific web.xml (placed on the application item's web-inf / web.xml file) to overwrite these defaults. How to put Tomcat into IIS
IIS, ie Internet Information Server, is a powerful web server provided by Microsoft, which has a good support for ASP, which believes that many readers have already known. Now, JSP has been popular, how can IIS support this technology? The reader will get answers from this section.
First, install the environment Windows 2000, IIS 5.0
Tomcat 3.1 download address
http://jakarta.apache.org/builds/tomcat/release/v3.1/bin/isapi_redirect.dll download address
http://jakarta.apache.org/builds/tomcat/release/v3.1/bin/win32/i386/
Put it to {Tomcathome} /bin/iis/i386/isapi_redirect.dll
Second, first to ensure that Tomcat can be implemented correctly
Third, put the jakarta.reg file between Begin and End into the machine, my Tomcat is installed in C: // Jakarta-Tomcat, you can modify the jakarta.reg file according to your environment
Jakarta.reg: --- begin --- Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE / SOFTWARE / Apache Software Foundation / Jakarta Isapi Redirector / 1.0] "extension_uri" = "/ jakarta / isapi_redirect.dll" "log_file" = "c: //jakarta-tomcat//logs//isapi.log "" log_level "=" debug "" worker_file "=" c: //jakarta-tomcat// -///workers.properties "" worker_mount_file "=" C: / /jakarta-tomcat//conf//uriwortrap.properties --- end ---
Fourth, according to the above configuration IIS, after installing Isapi_Redirect.dll with the author's experience, you need to restart the machine (restart IIS is not good), if you don't work, it is installed once.
5. Start Tomcat, and type http: // baryon / example in IE, look at how the effect is available, and you can use JSP and servlet with the 8080 port. Be careful not to start Tomcat, and IIS is not possible. Note that http: // baryon / example / final "/" must have, otherwise it will be found.
How to configure Apache Tomcat in a Windows 9X environment?
If the reader uses the Windows 9x operating system, then after reading the following, build your own JSP running environment will become very simple.
The system is configured as follows:
Windows 98 (NT is mainly different for environmental variables, and the settings of the following programs are independent)
JDK1_2_2.exe Apache_1_3_9_win32.exe apachemodulejserv.dll Tomcat.zip (3.1)
First, install
1. Install JDK1.2.2 and add the directory of JDK1.2.2 to the environment variable Path (AutoExec.bat);
2. Install Apache_1_3_9_win32.exe;
3. Copy ApachemodulejServ.dll to the modules subdirectory of the directory where Apache is located;
4. Install Tomcat 3.1 (extract and copy to a directory);
The required components are already installed, the following is the configuration process.
Second, the configuration
1. First edit the http.conf file under Apache's Conf subdirectory to make it function and cooperate with your machine configuration 2. Set the following environment variables (can be placed in autoexec.bat):
Set tomcat_home = C: / Progra ~ 1 / Tomcat
(Note: If there is a Windows style directory, be sure to write into a DOS directory format, otherwise it is easy to issue problems)
Set java_home = c: /progra ~ 1/jdk1.2.2
3. Open the Tomcat.conf file under the Tomcat's Conf subdirectory, remove the comments before the following row:
LoadModule Jserve Jserve_Module Modules / ApachemodulejServ.dll
A similar line of the corresponding Linux will then be blocked. /
4, open the httpd file of Apache, in the final join:
INCLUDE C: /PROGRA ~ 1/tomcat/conf/tomcat.conf
Third, start and test:
1. First start Apache, if it is correct, it should be displayed:
Apache / 1.3.9 (Win32) Tomcat / 1.0 Running ...
2. Enter the execution directory (bin) of Tomcat.
Note 1: This directory must be in the Windows style directory, you must use the Windows directory representation to enter, such as Program ~ 1 must write as Program Files
Note: The operating environment variable size of the command is usually required to be 4096
3. Start Tomcat with Tomcat.bat Start or Tomcat.Bat Run, you should see the correct output ClassPath, indicating that Tomcat is successful!
4. Enter http: // localhost / example in your browser /, you should see the three directory lists returned by Server. You can see the example of clicking on the JSP.
As long as the program downloaded by the official station, the above step should be able to properly start the JSP environment correctly, the next question is how to join the context to map to its own work directory, please read the reader to refer to 1.2.5.
How to install Apache Servlet JSP in Windows NT 4.0
Environmental configuration has always been a more annoying job! The purpose of this section is to easily install and configure the execution environment of the Servlet and JSP for readers.
First, go to java.apache.org to download a Tomcat3.1 or Tomcat3.2. Download a zip file. Differently extract it below a directory. For example, C: / below. A Jakatar-Tomcat directory will appear under the C drive. The reader can change this directory, assuming to Tomcat. Open the server.xml file in the C: / Tomcat / Conf directory now. Find the ContextManager tag, you will see:
Now go to the C: / Tomcat / bin directory, the reader can see a batch file for startup.bat. Before executing this file, if you use JDK1.2.1 or JDK1.2.2, put the jdk_home / lib / Tools.jar this file in the classpath. (Note: Setting ClassPath: Right-click My Computer -> Select Properties -> Environment, if there is no ClassPath, add one, if you have it, add JDK_HOME /LIB/Tools.jar).
You can now execute the STARTUP.BAT file. (If the OUT OF ENVIRONMENT SPACE is executed, the buffer size of the DOS window is made large. Specific method: attributes in the menus of the DOS window -> Memory -> Total and initial environment
After execution, Startup.bat will open Start Tomcat in another window. Open the browser and enter: http: // localhost: 9000 /
Then use it to enter. At this time, a Jakatar-Tomcat should be seen. Now type in the address bar: http: // localhost: 9000 / example / servlet /, Enter, you should see a servlet's INDEX page, click on the example servlet to see it. Now type http: // localhost: 9000 / examples / jsp /, Enter, readers will also see a page similar to the JSP page, click on the JSP example. If you have a 500 error after clicking a JSP, if there is no other error message, in IE5 Tools Menu -> Internet Options -> Advanced, remove the option to the friendly HTTP error, then IE will report Specific error information. The reason is that you didn't put Tools.jar in ClassPath.
Now you have to connect Tomcat to Apache. First go to www.apache.org to download an Apache Web Server. Go again to java.apache.org to download a JSERV. Install Apache Web Server first (such as below the C: / Apache directory). After installation, you need to modify the c: /apache/conf/httpd.conf file, find ServerName, modify the name you like, find the port, and modify the port that is not occupied. Install JSERV (for example, installed below the C: / Jserv directory). Install JSERV Need to specify the directory where JVM is located, you can specify the JDK_HOME / BIN directory. Need to specify the directory where JSDK is located. (In fact, it is going to find the servlet.jar file, you can specify Tomcat / lib / servlet.jar to try a try) At this time, Apache has already supported servlet, but JSP is not supported. So you also need to modify the C: /Apache/conf/httpd.conf file. Use the text editor to open httpd.conf, in the file last coupled: include "Tomcat_Home / conf / tomcat-apache.conf". (Please note that Tomcat-apache.conf is automatically generated by Tomcat after running in Tomcat. So just open Tomcat, then open Apache)
Now start Tomcat (c: /tomcat/bin/startup.bat), after another window appears, start apache (c: /apache/bin/apache.exe or through the Start Menu -> Program -> Apache Web Server -> Start Apache starts). If you start Apache by the latter, there is a DOS window to flash after flash, indicating that there is a problem with the configuration of Apache Jserve Tomcat, and you should start Apache through the first method, you can see it on the screen. Specific error information.
If the error message is: Apj *: can't be specified more Than'. Once Per host. (* Represents some strings, such as APJSERVLOGLEVEL, etc. If Apache is also started, now you can type http: // localhost: apache_port / example debug servlet in the address bar, here apache_port is the port number of Apache, can be modified C: / Apache / Conf / httpd.conf This file modifies the port number; Type http: // localhost: Apache_port / Examples / JSP / to debug JSP in the address bar.
How do I set up a JSP virtual directory?
If the reader is careful, it will find that the previous test is an Examples / JSP directory. Now let's talk about how to create your own work catalog.
First create a MyWeb directory below D: / Tomcat / WebAPPS (assuming installation directory to D: / Tomcat) directory, then create a JSP directory and classes directories below the MyWeb Directory. Then use the text editor to open the server.xml below the D: / Tomcat / conf directory, find the
Where the value of PATH is a virtual directory, the value of DOCBase is the absolute path of the directory of your hard drive.
Then put the d: / tomcat / webapps / myweb / class directory into the ClassPath environment variable. Edit the following file with a text editor.
<% for (int i = 1; i <3; i ) {OUT.PRINTLN ("
Save c: /tomcat/webapps/myweb/jsp/helloWorld.jsp.
Type: http:// localhost: http: // localhost: apache_port / testdir / jsp / helloWorld.jsp Look at the results.
Test the servlet again. Edit the following file with a text editor.
import Java.io. *; import Java.text *;. import Java.util *;. import Javax.servlet *;. import Javax.servlet.http *;. public class HelloWorld extends HttpServlet {public void service (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {response.setContentType ( "text / html"); ServletOutputStream out = response.getOutputStream (); out.println ( ""); out.println ( "
"); String title = "Hello World"; Out.println ("