Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Tomcat environment

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to build the Tomcat environment". In the daily operation, I believe that many people have doubts about how to build the Tomcat environment. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to build the Tomcat environment". Next, please follow the editor to study!

No.1 build environment

Download the tomcat source code

Go to the tomcat official website: https://tomcat.apache.org/ to download the source code of the corresponding version

1.2.Import Eclipse

Create a new Java project

Copy the files under the java directory in the Tomcat source package to the src directory

Import external dependency packages

Ant.jar ecj-4.4.jar jaxrpc.jar wsdl4j-1.5.2.jar

No.2 Tomcat top-level structure

The above figure roughly shows the structure of tomcat, which mainly includes the following modules:

Server:

Server means that it represents the entire tomcat server. There is only one Server in a tomcat.

Service:

A logical function layer in Server where a Server can contain multiple Service

Connector:

Called connector, it is one of the core components of Service. A Service can have multiple Connector, mainly connecting client requests.

Container:

Another core component of Service, according to the level, there are four kinds of Engine,Host,Context,Wrapper, a Service has only one Engine, its main function is to execute business logic

Jasper:

JSP engine

Session:

Session management

No.3 Server

Server is the top-level container of Tomcat, representing the entire server, that is, a Tomcat has only one Server,Server that contains at least one Service component to provide specific services.

This is also well reflected in the configuration file (port= "8005" shutdown= "SHUTDOWN" hears the "SHUTDOWN" command on port 8005 and the server stops).

A Server interface is defined in tomcat, which is declared as follows:

Public interface Server extends Lifecycle {

It inherits the Lifecycle interface so that when the start () and stop () methods are called, all defined Services is also started or stopped.

Its standard implementation is the org.apache.catalina.core.StandardServer class.

The Server element represents the entire Catalina servlet container.

Its properties represent the characteristics of the entire servlet container.

The server may contain one or more services, as well as * named resource sets.

Its implementation should use the ServerFactory class to register (singleton) instances in its constructor.

No.4 Service

As we mentioned earlier, a Server contains at least one Service component to provide specific services.

The basic function of Service is to receive the request from the client, then parse the request, complete the corresponding business logic, and then return the processed result to the client.

Generally speaking, two saving methods are provided: one start opens the service Socket connection, listens to the service port, and one stop stops the service to release network resources.

A Service interface is defined in tomcat, which is declared as follows:

Public interface Service extends Lifecycle {

A Service is a group of one or more Connectors that share a Container to process requests.

Connector is responsible for processing request monitoring, Container is responsible for handling request processing

As you can see from the configuration of the conf/server.xml file, Service is the wrapper for Connector and Engine components, associating one or more Connector with an Engine. In the default configuration file, a service called Catalina is defined, which associates the Connector of HTTP/1.1 and AJP/1.3 with an Engine named Catalina.

A Server can contain multiple Service (they are independent of each other, but share a common JVM and class library), and a Service is responsible for maintaining multiple Connector and a Container.

No.5 Connector

Connector is a connector that accepts requests and encapsulates them into Request and Response, which are then handed over to Container for processing, and returned to the client after Container processing is handed over to Connector.

Server.xml is configured with two Connector by default:

Listen on port 8080, which can be modified. ConnectionTimeout defines the connection timeout (in milliseconds). RedirectPort defines the redirection interface of ssl. According to the above configuration, Connector forwards the ssl request to port 8443.

Listening port 8009 Apache http represents Apache Jserv Protocol, and it will handle the interaction between Tomcat and Apache http server. This connector is used to handle the situation when we use Tomcat and Apache http server together, such as deploying an Apache http server and multiple Tomcat servers on the same physical AJP, handling static resources and load balancing through Apache server, AJP needs to listen on different ports for different Tomcat instances.

The design of Connector in tomcat is roughly as follows:

Connector uses ProtocolHandler to process requests, and different ProtocolHandler represents different connection types

ProtocolHandler consists of three parts: Endpoint, Processor and Adapter.

Because Endpoint handles the underlying Socket network connection, Endpoint is used to implement the TCP/IP protocol.

Processor is used to encapsulate the Socket received by Endpoint into Request,Processor to implement the HTTP protocol.

Adapter acts as an adapter to convert Request to ServletRequest and give it to Container for specific processing

No.6 Container

Container is used to encapsulate and manage Servlet, as well as to handle Request requests. There are four sub-containers in Container. The functions of the four sub-containers are:

Engine:

Engine, used to manage multiple sites, a Service can only have at most one Engine

Host:

Represents a site, also known as a virtual host, which can be added by configuring Host

Context:

Represents an application, corresponding to a set of programs normally developed, or a WEB-INF directory and the following web.xml file

Wrapper:

Each Wrapper is encapsulated with a Servlet

No.7 tomcat startup process

The startup process of tomcat is standardized, and the entry is BootStrap, which is launched uniformly according to the definition of the lifecycle management interface Lifecycle.

First, the init () method is called to initialize step by step, then the start () method is called to start, and each call is accompanied by a life cycle state change event.

The program entry main method of org.apache.catalina.startup.Bootstrap, which is implemented as follows:

Public static void main (String args []) {if (daemon = = null) {/ / Don't set daemon until init () has completed Bootstrap bootstrap = new Bootstrap (); try {bootstrap.init ();} catch (Throwable t) {handleThrowable (t); t.printStackTrace () Return;} daemon = bootstrap;} else {/ / When running as a service the call to stop will be on a new / / thread so make sure the correct class loader is used to prevent / / a range of class not found exceptions. Thread.currentThread (). SetContextClassLoader (daemon.catalinaLoader);} try {String command = "start"; if (args.length > 0) {command = args [args.length-1];} if (command.equals ("startd")) {args [args.length-1] = "start" Daemon.load (args); daemon.start ();} else if (command.equals ("stopd")) {args [args.length-1] = "stop"; daemon.stop ();} else if (command.equals ("start")) {daemon.setAwait (true) Daemon.load (args); daemon.start ();} else if (command.equals ("stop")) {daemon.stopServer (args);} else if (command.equals ("configtest")) {daemon.load (args) If (null==daemon.getServer ()) {System.exit (1);} System.exit (0);} else {log.warn ("Bootstrap: command\"+ command +"\ "does not exist.") }} catch (Throwable t) {/ / Unwrap the Exception for clearer error reporting if (t instanceof InvocationTargetException & & t.getCause ()! = null) {t = t.getCause ();} handleThrowable (t); t.printStackTrace (); System.exit (1) }}

The initialization method of org.apache.catalina.startup.Bootstrap, which is implemented as follows:

Public void init () throws Exception {/ / 1, set the configuration of catalina.home: set the catalina.home system property to the current working directory (if it is not already set). SetCatalinaHome (); / / 2. Set the configuration of catalina.base: if not, setCatalinaBase () the current working directory for the setting of catalina.base; / / 3. Initialize the class loader: commonLoader, catalinaLoader, sharedLoader initClassLoaders (); Thread.currentThread (). SetContextClassLoader (catalinaLoader); SecurityClassLoad.securityClassLoad (catalinaLoader) / / load our startup class and call its process () method if (log.isDebugEnabled ()) log.debug ("Loading startup class"); / / 4, load the startup class Class startupClass = catalinaLoader.loadClass ("org.apache.catalina.startup.Catalina"); / / 5, instantiate the startup class Object startupInstance = startupClass.newInstance () If (log.isDebugEnabled ()) log.debug ("Setting startup class properties"); / / 6. Set the method parameter String methodName = "setParentClassLoader"; Class paramTypes [] = new Class [1]; paramTypes [0] = Class.forName ("java.lang.ClassLoader"); Object paramValues [] = new Object [1]; paramValues [0] = sharedLoader Method method = startupInstance.getClass (). GetMethod (methodName, paramTypes); / / 7. Call the setParentClassLoader method of the startup class to set the shared extension class loader method.invoke (startupInstance, paramValues); catalinaDaemon = startupInstance;}

The start () method of org.apache.catalina.startup.Bootstrap, which is implemented as follows:

/ * Start the Catalina daemon. * / public void start () throws Exception {/ / if the startup class is instantiated, call the init () method if (catalinaDaemon==null) init (); / / get the start method Method method = catalinaDaemon.getClass () .getMethod ("start", (Class []) null) of the startup class. / / call the start method of the startup class, that is, call the start () method method.invoke (catalinaDaemon, (Object []) null) of org.apache.catalina.startup.Catalina.} at this point, the study on "how to build a Tomcat environment" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report