Principles of Tomcat and HTTP server integration

Source: Internet
Author: User

The main function of Tomcat is to provide Servlet/jsp containers. Although it can also be used as an independent Java Web server, it can process static resources (such as HTML files or image files) at a high speed, and the Web server management functions provided are not as good as other professional HTTP servers, such as IIS and Apache servers.
  
Therefore, Tomcat is often integrated with other HTTP servers in practical applications. For HTTP servers that do not support Servlet/JSP, you can use the Tomcat server to run Servlet/jsp components.
  
When Tomcat is integrated with other HTTP servers, the working mode of the Tomcat server is usually the servlet container outside the process. The Tomcat server communicates with other HTTP servers through specialized plug-ins. For more information about the working mode of Tomcat server, see section 1.4.
  
This chapter first discusses the general principle of Tomcat and HTTP server integration, and then introduces the detailed steps of Tomcat and Apache and IIS integration.
  
22.1 principle of Tomcat and HTTP server integration
  
The Tomcat server establishes a connection with the customer program through the connector component. The connector component is responsible for receiving customer requests and sending the response results of the Tomcat server to the customer. By default, Tomcat configures two connectors in server. xml:
  
<! -- Define a non-ssl coyote HTTP/1.1
Connector on port 8080 -->
<Connection Port = "8080"
Maxthreads = "150"
Minsparethreads = "25"
Maxsparethreads = "75"
Enablelookups = "false"
Redirectport = "8443"
Acceptcount = "100"
DEBUG = "0"
Connectiontimeout = "20000"
Disableuploadtimeout = "true"/>
  
<! -- Define a coyote/JK2 AJP 1.3
Connector on port 8009 -->
<Connection Port = "8009"
Enablelookups = "false"
Redirectport = "8443" DEBUG = "0"
Protocol = "AJP/1.3" type = "codeph" text = "/codeph"/>
  
The first connector listens to port 8080 and establishes an HTTP connection. This connector is used to access the Web application of the Tomcat server through a browser.
  
The second connector listens to port 8009 and is responsible for establishing connections with other HTTP servers. This connector is used when Tomcat is integrated with other HTTP servers.
  
The two methods for Web customers to access JSP components on the Tomcat server are 22-1.



Figure 22-1 two methods for Web customers to access JSP components on the Tomcat server
  
In Figure 22-1, Web Client 1 directly accesses the JSP component on the Tomcat server. Its access URL is http: // localhost: 8080/index. jsp. Web Client 2 accesses JSP components on the Tomcat server through the HTTP server. Assume that the http port used by the HTTP server is the default port 80, then the URL accessed by Web Client 2 is http: // localhost: 80/index. JSP or http: // localhost/index. JSP.
  
Next, we will introduce how Tomcat communicates with the HTTP server.
  
22.1.1 JK plug-in
  
Tomcat provides a dedicated JK plug-in to communicate with the HTTP server. The JK plug-in should be placed on the HTTP server of the other party. When the HTTP server receives a customer request, it uses the JK plug-in to filter the URL. The JK plug-in determines whether to forward the customer request to the Tomcat server based on the pre-configured URL ing information.
  
Assume that all "/*. JSP URLs are all processed by the Tomcat server. In Figure 22-1, the JK plug-in forwards client requests to the Tomcat server, and the Tomcat server runs index. JSP, then send the response result to the HTTP server, and then the HTTP server sends the response result to Web Client 2.
  
Tomcat provides different JK plug-ins for different HTTP servers. This chapter uses the following JK plug-ins:
  
Integration with Apache HTTP Server in Windows: mod_jk_2.0.46.dll
  
Integration with Apache HTTP Server in Linux (redhet): mod_jk.so-ap2.0.46-rh72 .. 46-rh72
  
Integration with IIS server: isapi_redirect.dll
  
22.1.2 AJP Protocol
  
AJP is a custom protocol for communication between Tomcat and HTTP servers, providing high communication speed and efficiency. In the integration of Tomcat and HTTP server configuration, you do not have to worry about the details of the AJP protocol. For more information about AJP, see the following link:
  
Http://jakarta.apache.org/builds/jakarta-tomcat-connectors/jk2/doc/common/AJPv13.html
  
22.2 integration of Tomcat and Apache server in Windows
  
Apache HTTP Server is an open-source software provided by the Apache Software organization. It is an excellent professional web server and provides a variety of Web management functions for network administrators, including Directory Index, directory alias, content negotiation, configurable HTTP Error Report, setuid execution of CGI program, sub-process resource management, server-side image ing, URL rewriting, URL spelling check, and online manual.
  
The Apache HTTP server does not provide Servlet/jsp containers. Therefore, in practical applications, integrating Tomcat with Apache can create a practical and commercial web platform. In Windows NT/2000, Tomcat and Apache server are integrated. For the software to be prepared, see Table 22-1.
  
Table 22-1 software to be prepared for Tomcat and Apache server integration in Windows NT/2000
   
1. Install the Apache HTTP Server
  
When you run the apache_2.0.47-win32-x86-no_ssl.msi, you start the installation program for the Apache HTTP Server, as long as you install according to the default settings. If the installation is successful, the Apache HTTP service is automatically added to windows, as shown in 22-2.
   
Figure 22-2 add an apache service to a Windows Service
  
Assume that the root directory of Apache is, and there is a configuration file httpd. conf under its conf sub-directory. If Apache is installed on the local machine and the default port 80 is used as the http port, the following attributes are displayed in the httpd. conf file:
  
Listen 80
Servername localhost: 80
  
In the "Start"> "program"> "Apache HTTP Server 2.0.47"> "control Apache server" menu of the operating system, restart and start are provided) and stop the sub-menu of the Apache server.
  
Make sure that port 80 is not in use; otherwise, the Apache server cannot be started. After the Apache server is started, you can access the Apache test page to determine whether the installation is successful. Visit http: // localhost. If the webpage shown in 22-3 is displayed, Apache has been installed successfully.
   
Figure 22-3 Apache server test webpage
  
2. Add the JK plug-in to Apache
  
Add the JK plug-in to Apache and copy mod_jk_2.0.46.dll to the/modules directory.
  
3. Create the workers. properties file.
  
The workers. properties file is used to configure Tomcat information. Its storage location is/CONF/workers. properties. The workers. properties file is provided in the sourcecode/chapter22/windows_apache directory of the related CD files in this book. Its content is as follows ("#" is followed by a comment ):
  
Workers. tomcat_home = c: \ Jakarta-Tomcat
# Let the mod_jk module know about Tomcat
Workers. java_home = c: \ j2sdk1.4.2
# Let the mod_jk module know about j2sdk
PS = # specify the file path delimiter
Worker. List = worker1
Worker. worker1.port = 8009
# Working port. Do not modify it if it is not occupied
Worker. worker1.host = localhost
# Tomcat server address
Worker. worker1.type = ajp13
# Type
Worker. worker1.lbfactor = 1
# Load Balancing Factor
  
For the attribute descriptions in the preceding file, see Table 22-2.
  
Table 22-2 properties of the workers. properties File
   
4. modify the configuration file httpd. conf of Apache.
  
Open the/CONF/httpd. conf file and add the following content to the end:
  
# Using mod_jk2.dll
Redirect dynamic callto Tomcat
Loadmodule jk_module
Modules \ mod_jk_2.0.46.dll
Jkworkersfile
"Conf \ workers. properties"
Jklogfile "logs \ mod_jk2.log"
Jkloglevel debug
Jkmount/*. jsp worker1
Jkmount/helloapp/* worker1
  
The above content is provided in the sourcecode/chapter22/windows_apache/httpd_modify.conf file on the CD supporting this book. It instructs the Apache server to load the JK plug-in and set relevant attributes for the JK plug-in, for descriptions of these attributes, see Table 22-3.
  
Table 22-3 attributes of the JK plug-in
  
Jkmount is used to specify URL ing information, "jkmount /*. JSP worker1 "indicates "/*. JSP URLs are processed by the Tomcat server represented by worker1. jkmount/helloapp/* worker1 indicates that the URLs used to access the helloapp are processed by worker1.
  
5. Test Configuration
  
Restart the Tomcat server and Apache server and access http: // localhost/index. jsp through a browser. If the default homepage of Tomcat appears, the configuration is successful. If the helloapp application has been published on the Tomcat server, you can access http: // localhost/helloapp/index.htm. if you return to the index.htm page of The helloappapplication, the configuration is successful.
  
If the configuration is incorrect, you can view the log information generated by the JK plug-in, which helps you find the cause of the error. Set the log file storage location to/logs/mod_jk2.log in the Apache configuration file httpd. conf.
  
6. Load Balancing when Apache is integrated with multiple Tomcat servers
  
In practical applications, if the website has a high access volume, you can integrate multiple Tomcat servers with Apache to share the task of running Servlet/jsp components. The loadbalancer of the JK plug-in is responsible for allocating workloads to these Tomcat servers based on the pre-configured lbfactor (load balancing factor) in the workers. properties file to achieve load balancing.
  
Assume that Apache is integrated with two Tomcat servers. One Tomcat server and Apache are running on the same machine, and the JK port used is 8.

 

From: 1. http://www.vlan9.com/net-protocol/g107095.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.