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

Installation and basic usage of Tomcat

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the installation and basic use of Tomcat". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the installation and basic use of Tomcat".

Install jdk install jdk download address: http://www.oracle.com/technetwork/java/javase/downloads/index.html1) extract and configure the environment variable # cd / usr/local/src/#tar xf jdk-8u231-linux-x64.tar.gz#ln-s jdk1.8.0_231/ jdk#cat > / etc/profile.d/jdk.sh / usr/lib/systemd/system/tomcat.service directoryAJP (Apache Jserv protocol) is a binary communication protocol based on TCP. Core component description

Tomcat starts a Server process. You can start more than one Server, but generally only one

Create a Service to provide services. You can create multiple Service, but generally only one

In each Service, is the associated configuration of Engine and its connector Connector

Multiple connectors Connector can be provided for this Server, which use different protocols and bind different ports. Its function is to handle different connection requests or responses from the client.

Engine is also defined inside Service. The engine is the real entry for processing requests, and multiple virtual hosts Host are defined internally.

Engine analyzes the request header and sends the request to the corresponding virtual host

If there is no match, the data is sent to the defaultHost default virtual host on Engine

The default virtual host on Engine can be modified

Host defines a virtual host. The virtual host has a name name, which is matched by name

Context defines the application's separate path mapping and configuration

Tomcat's process of processing requests assumes that the request from the customer is: http://localhost:8080/test/index.jsp 1) the request on the browser side is sent to the server port 8080 tomcat process listens on this port. This request is obtained through the listening HTTP/1.1 Connector. 2) Connector hands the request to the Engine of the Service where it is located and waits for the response of Engine. 3) Engine gets the request localhost:8080/test/index.jsp, matching all its virtual hosts Host 4) Engine matches to the Host named localhost. Even if there is no match, the request is handed over to the Host for processing, because the Host is defined as the default host of the Engine. 5) localhost Host gets the request / test/index.jsp, matches all the Context it owns. 6) Host matches to Context 7) path=/test with path / test get request / index.jsp, look in its mapping table for corresponding servlet 8) Context matches servlet with URL PATTERN = * .jsp, corresponding to JspServlet class to construct HttpServletRequest object and HttpServletResponse object Call the doGet or doPost method of JspServlet as a parameter. 9) Context returns the HttpServletResponse object after execution to Host 10) Host returns the HttpServletResponse object to Engine 11) Engine returns the HttpServletResponse object to Connector 12) Connector returns the HttpServletResponse object to the browser application deployment root directory Tomcat the default website root directory is CATALINA_BASE/webapps/, in the Tomcat webapps directory, there is a very special directory ROOT, which is the website default root directory. The directory of each virtual host can be configured with its own site directory using appBase, and the ROOT directory can be used as the home directory. JSP WebApp directory structure home page configuration: generally specified as the private resource path of the current WebApp of index.jsp or index.htmlWEB-INF/:, and usually stores the web.xml and context.xml configuration files used by the current application META-INF/: is similar to the WEB-INFclasses/: class file, the current webapp needs class lib/: the current application depends on the jar package to change the default home page file, by default, visit the home page of Tomcat The returned content is provided by the CATALINA_BASE/webapps/ROOT/index.jsp file. Here, an index.html file will be added under CATALINA_BASE/webapps/ROOT/, as follows: # cat/ usr/local/src/tomcat/webapps/ROOT/index.html tomcat index.html add the contents of the file before visiting the Tomcat default page The returned result is as follows: # curl localhost:8080tomcat index.html produces the above result because of the tag content (default page) in the CATALINA_BASE/conf/web.xml file. Copy it to CATALINA_BASE/webapps/ROOT/WEB-INF/web.xml. After the following Welcome to Tomcat Welcome to Tomcat index.jsp index.htm index.html configuration is modified, visit the home page again. The result is as follows

From the above verification results, it can be found that the default page access order of Tomcat is the webapp archive format matched from top to bottom. War: WebApp packaged .jar: EJB class packaged file .rar: resource adapter class packaged file .ear: enterprise WebApp packaged traditional application development testing, usually packaged into war format, this kind of file is deployed under the webapps of Tomcat, and can also be automatically expanded. Deploy Deploy

Deployment: place the source files of the webapp in the target directory, access the webapp through the paths configured in the web.xml and context.xml files, and load its unique and dependent classes into the JVM through the class loader.

Automatically deploy the Auto Deploy:Tomcat and find that the application is loaded and started.

Manual deployment

Cold deployment: put webapp in the specified directory before starting Tomcat

Hot deployment: Tomcat service does not stop, relying on manager, ant scripts, tcd (tomcat client)

Tools such as deployer)

Anti-deployment undeploy: stop running webapp, remove loaded classes from JVM, and remove deployed files from the Tomcat application directory

Launch start: webapp can access

Stop stop:webapp cannot be accessed and cannot provide services, but JVM does not clear it

Experiment 1 adds a test.jsp file, followed by a jsp example that dynamically generates strings on the server side and finally splices them together.

Put test.jsp under ROOT, and then visit http://YourIP:8080/test.jsp. You can immediately see that this is converted to test_jsp.java after finding the corresponding test.jsp through path mapping. Then compiled into the test_jsp.classCATALINA_BASE/work/Catalina/localhost/ROOT/org/apache/jsp/ directory with converted files 2 add an application simulation deployment an application common development project directory composition structure # mkdir projects/myapp/ {WEB-INF,classes,lib}-p#tree projects/projects/ └── myapp ├── classes ├── lib └── WEB-INF4 directories, 0 files common application home page Use the above test.jsp#cp webapps/ROOT/test.jsp projects/myapp/index.jsp to copy the project directory to the webapps directory # cp-r projects/myapp/ / usr/local/src/tomcat/webapps/ access test http://YourIP:8080/myapp main configuration file

CATALINA_BASE/conf/server.xml

1) Port 8005 this port is the management port of Tomcat, and the default listening is 127.0.0.1. When the string SHUTDOWN is received, the Server is closed. # ss-nlt | grep 8080LISTEN 0 100 [:]: 8080 [:]: * # telnet 127.0.0.1 8005Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is'^ '.SHUTDOWNConnection closed by foreign host. This management function is recommended to be disabled, changing SHUTDOWN to a string that cannot be guessed, such as 2) user authentication

CATALINA_BASE/conf/tomcat-users.xml

If you want to use Manager App, you need a role manager-gui, which is added in the CATALINA_BASE/conf/tomcat-users.xml configuration file, as follows: after Tomcat starts loading, these contents are resident in memory. If a new user is configured, you need to restart Tomcat. Visit Manager App

When visiting manager, tell 403.The prompt tells manager to modify in the context.xml, and the file path CATALINA_BASE/webapps/manager/META-INF/context.xml can see from the configuration file that the regular expression after allow only allows local access, but my current address is 192.168, so you need to add regular expressions to support access. After configuration, the following allow= "127\.\ d +\.\ d + |: 1 | 0VOVOVOV 0VOV 0VERV 0VERV 00VERV 00VERV 0l1 | 192\ .168\.\ d +\.\ d +" access the test again after reboot.

3) Service in general, a Server instance is configured with a Service,name attribute that is equivalent to the ID of the Service. 4) the connector is configured with redirectPort, and automatically redirects to this connector if you access the HTTPS protocol. However, most of the time, Tomcat does not enable HTTPS, because Tomcat is often deployed internally, and HTTPS performance is poor. 5) engine configuration defaultHost points to define a virtual host internally. The default virtual host can be changed, the default localhost. 6) Virtual host configuration description: name # must be a hostname, which is matched by a hostname. AppBase # the web page root directory of the current host is relative to CATALINA_HOME, and you can also use the absolute path unpackWARs # whether to automatically extract the war format autoDeploy # hot deployment, automatically load and run the application virtual host configuration experiment to try to configure another virtual host, and deploy myapp to / data/webapps directory # mkdir / data/webapps-p#cp-r projects/myapp/ / data/webapps/ROOT just defined www.hechunping.com in the host name in the virtual host All domain name resolution access tests need to be added to the hosts file of the host

Context configure Context role

Path mapping

Apply independent configuration, such as configuring application log and application access control separately

Configuration instructions: path # refers to the access path docBase # can be an absolute path or a relative path (relative to the appBase of Host) reloadable # true means that if the .class file in the WEB-INF/classes or META-INF/lib directory is changed, the WEB application will be reloaded, which will consume a lot of performance. In a production environment, false is used to disable it. Copy the project file under projects/myapp to / data/ # cp-r projects/myapp/ / data/myapp_v1#cd / data/#ln-s myapp_v1/ test add the following content access test http://www.hechunping.com:8080/test/ to the main configuration file server.xml of Tomcat

Note: soft links are especially used here because when future versions are upgraded, you need to point the soft link to myapp_v2 and restart Tomcat. If there is a problem after the new version is launched, modify the soft link to the previous version of the directory, and restart, you can achieve rollback. At this point, I believe you have a deeper understanding of the "installation and basic use of Tomcat". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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