Tomcat server learning and use (1), tomcat server

Source: Internet
Author: User
Tags web hosting

Tomcat server learning and use (1), tomcat server
1. Tomcat server port configuration

All Tomcat configurations are placed in the conf folder. The server. xml file in it is the core configuration file.

To modify the startup port of the Tomcat server, you can modify the port of the Tomcat ctor node in the server. xml configuration file.

For example, change the Start port of the Tomcat server from 8080 to 8081 by default.

Default Tomcat server startup port configuration

1 <Connector port="8080" protocol="HTTP/1.1"2                connectionTimeout="20000"3                redirectPort="8443" />

 

Change the Tomcat server startup port to port 8081.

1 <Connector port="8081" protocol="HTTP/1.1"2                connectionTimeout="20000"3                redirectPort="8443" />

In this way, the default Tomcat port 8080 is changed to port 8081. Note that once *. if the xml file changes, the Tomcat server must be restarted and the new configuration information will be read again after the restart. Because the Tomcat startup port has been changed to 8081 in the server. xml file, the Tomcat server starts with port 8081 at startup, as shown in:

  

To access the Tomcat server, you must use a new access port to access: http: // localhost: 8081/, as shown in:

  

Ii. Tomcat ing of Tomcat server virtual directories

  After a Web application is developed, if you want to provide external access, You need to hand over the directory where the web application is located to the web server for management. This process is called a directory-like ing.. So on the Tomcat server, how does one map virtual directories? There are several methods in total:

2.1. virtual directory ing method 1: configure it in the host element of the server. xml file

Find the host element of the server. xml file, as shown in:

  

Add <Context path = "/JavaWebApp" docBase = "F: \ JavaWebDemoProject "/> You can map the JavaWebDemoProject JavaWeb application under drive F to the JavaWebApp virtual directory. The JavaWebApp virtual directory is managed by the Tomcat server, javaWebApp is a directory that does not exist on the hard disk. It is a virtual directory that we can write on our own. The Code is as follows:

1 <Host name="localhost"  appBase="webapps"2              unpackWARs="true" autoDeploy="true"3              xmlValidation="false" xmlNamespaceAware="false">4 5          <Context path="/JavaWebApp" docBase="F:\JavaWebDemoProject" />6  </Host>

Context indicates the Context, which indicates a Java Web application. The Context element has two attributes,

I. path: used to configure virtual directories. It must start.

Pai.doc Base: this virtual directory corresponds to the directory where the Web application on the hard disk is located.

Use a browser to access the web Resource 1. jsp under the virtual directory "/JavaWebApp". The access result is as follows:

  

1. jsp can be accessed normally, which means that we have successfully mapped the JavaWebDemoProject on drive F to the JavaWebApp virtual directory, accessing "/JavaWebApp/1.jsp" is equivalent to accessing" F: \ JavaWebDemoProject \ 1.jsp"

Note: After Tomcat 6, we do not recommend that you use the server. in the xml file, the context element is configured to add the ing of the virtual directory. after the xml file, the Tomcat server must be restarted before it can be reloaded. xml file. In the Tomcat server documentation http: // localhost: 8080/docs/config/context.html, the following descriptions are provided:

It is NOT recommended to place <Context> elements directly in the server. xml file.This is because it makes modifyingContextConfiguration more invasive since the mainconf/server.xmlFile cannot be reloaded without restarting Tomcat.

IndividualContextElements may be explicitly defined:

  • In an individual file/META-INF/context.xmlInside the application files. Optionally (based on the Host's copyXML attribute) this may be copied$CATALINA_BASE/conf/[enginename]/[hostname]/And renamed to application's base file name plus a ". xml" extension.
  • In individual files (with a ". xml" extension) in$CATALINA_BASE/conf/[enginename]/[hostname]/Directory. the context path and version will be derived from the base name of the file (the file name less. xml extension ). this file will always take precedence over any context. xml file packaged in the web application's META-INF directory.
  • Inside a Host element in the mainconf/server.xml.
2.2. virtual directory ing method 2: Allow tomcat server to automatically Map

The tomcat server automatically manages all web applications under the webapps directory and maps them to virtual directories. In other words, web applications in the webapps directory of the tomcat server can be directly accessed from outside.

For example, copy the Java Web application JavaWebDemoProject under drive F to the tomcat server webapps directory, as shown in:
  

In this case, the Tomcat server automatically maps a virtual directory "/JavaWebDemoProject" for the JavaWeb application, and then you can access the resources of the JavaWeb application using a browser, as shown in:

  

2.3. virtual directory ing method 3

Refer to the Tomcat server documentation:

In individual files (with a ". xml" extension) in$CATALINA_BASE/conf/[enginename]/[hostname]/Directory. the context path and version will be derived from the base name of the file (the file name less. xml extension ). this file will always take precedence over any context. xml file packaged in the web application's META-INF directory.

Add a file with xml extension under the \ conf \ Catalina \ localhost directory of the tomcat server. The name of the xml file can be arbitrary, such as the following aa. xml, pay attention to this sentence"The context path and version will be derived from the base name of the file", The meaning of this sentence is translated as" The path attribute of the context element is derived from the name of this xml file ", as mentioned above, the path attribute of the Context element is used to configure the name of the imaginary directory. Therefore, the name of the imaginary directory is the name of the xml file.

  $CATALINA_BASEIt refers to the tomcat server root directory,[enginename]The name of the engine used by the Tomcat server. The engine used by Tomcat is Catalina.

  

AddContextThe code for element ing to a Java Web application is as follows:

1 <Context docBase="F:\JavaWebDemoProject" />

Note: InContextThe path attribute is not specified in the element to set the name of the virtual directory. The virtual directory name mapped to "F: \ JavaWebDemoProject" is Shenma, is the name aa of the xml file being edited.

  

The biggest advantage of ing a virtual directory in this way is that you do not need to restart the Tomcat server after modifying the configuration file. For example. modify xml to bb. xml, the Tomcat server automatically Undeploying context [/aa], and then the information is: Deploying configuration descriptor D: \ apache-tomcat-7.0.53 \ conf \ Catalina \ localhost \ bb. xml

  

3. Tomcat server configuration is similar to host 3.1 and Virtual Host Configuration

A website is configured as a virtual host.
To configure a virtual host (website) on the Tomcat server, you must modify the server in the conf folder. the xml configuration file uses the Host element for configuration to open the server. xml, you can see that the Tomcat server comes with a virtual host (website) named localhost, as shown in:

  

We usually put the developed Java Web application under the webapps folder, and then we can use"Http: // localhost: Port Number/JavaWebAppNameIn fact, the access is the virtual Host whose name is "localhost". This virtual Host manages all web applications in the webapps folder.

For example: http: // localhost: 8080/JavaWebDemoProject/1.jsp. this URL accesses 1 in the application JavaWebDemoProject under the virtual host named localhost. jsp web resource.
You can configure a virtual host as follows:

1 <Host name="www.gacl.cn" appBase="F:\JavaWebApps">2       3 </Host>

Here we configure a new virtual host. The name of the virtual host is "www.gacl.cn", and the virtual host "www.gacl.cn" manages all web applications in the JavaWebApps folder, when we use the domain name "www.baidu.com" on the Internet to access Baidu's website, we actually access a VM named "www.baidu.com, so when we want to access this virtual host named "www.gacl.cn", we can use "Domain name (www.gacl.cn)" to access it. Note that appBase = "F: \ JavaWebApps ", the JavaWebApps folder represents not the root directory of a project, but a folder that stores one or more Java Web applications, as shown in:

   

Like the webapps folder on the Tomcat server, it stores a lot of Java Web applications.

   

3.2. Register a domain name in windows

To access the configured host (website) externally through the domain name, you must register the domain name used to access the website on the DNS server or windows system and find"C: \ Windows \ System32 \ drivers \ etc"The hosts file in the directory, as shown in:

  

Edit this file to bind the domain name and IP address of the newly added website, in this way, we can use the domain name www.gacl.cn in the browser to access the web applications managed in the virtual host www.gacl.cn.

  

Use a browser to access 1 of JavaWebDemo1 web application under the "www.gacl.cn" web Hosting "www.gacl.cn. for jsp web resources, the "www.gacl.cn" virtual host opens port 8080. You can only access 1 of the web application JavaWebDemo1 through port 8080. jsp web resource

Iv. process of interaction between the browser and the server 4.1. Diagram of interaction between the browser and the server

When we open the browser, enter the URL address "http://www.gacl.cn: 8080/JavaWebDemo1/1.jsp" in the address bar of the browser to access 1. in the process of jsp web resources, the browser and the server have all done the magic horse operation. How can we see 1 in the browser. what about content in jsp web resources?

The browser and server perform the following operations:

1. the browser searches for the IP address corresponding to the host name in the Hosts file of the Operating System Based on the host name "www.gacl.cn.

2. If the browser does not find the corresponding IP address in the Hosts file of the operating system, go to the DNS server on the Internet to find the IP address of the Host "www.gacl.cn.

3. After finding the IP address of the Host "www.gacl.cn", the browser uses the IP address to connect to the Web server.

4. After the browser connects to the web server, it uses the http protocol to send a request to the server. when the request is sent, the browser transmits data to the Web server in the form of Stream, tell the Web server to access the Web Resources of the Web application in the server, as shown in:

  

This is the data transmitted to the server when the browser sends a request to the Web server"GET/JavaWebDemo1/1.jsp HTTP/1.1"The content here,

    GET: Tell the Web server that the browser sends a request to the server in GET mode.

    /JavaWebDemo1/1.jsp: Tell the Web server that the browser wants to accessJavaWebDemo1In the Application1. jspThis Web resource.

HTTP/1.1:Tell the Web server that the browser requests HTTP and uses version 1.1.

5. After the browser completes the above four steps, it starts to wait, waiting for the Web server1. jspThis Web resource is transmitted to it.

6. After the server receives the data transmitted by the browser, it starts to parse the received data"GET/JavaWebDemo1/1.jsp HTTP/1.1"What the client browser wants to access isJavaWebDemo1In the Application1. jspThis Web resource is then read by the server1. jspThe content in the Web resource is transmitted to the browser in the form of Stream, as shown in:

  

This is the data that the Web server transmits to the browser.

7. After the browser obtains the data transmitted to the server, it can display the data to the user, as shown in:

  

The "JavaWebDemo1" is the result of the browser parsing the data sent by the server.

Data sent from the server:

 1 HTTP/1.1 200 OK 2 Server: Apache-Coyote/1.1 3 Content-Type: text/html;charset=ISO-8859-1 4 Content-Length: 102 5 Date: Mon, 19 May 2014 14:25:14 GMT 6  7 

This is the interaction process between the browser and the server.

V. Composition Structure of JavaWeb applications

When developing a Java web application, different types of files have strict storage rules. Otherwise, the web application may not be accessible, but an error may occur when the web server starts.

  

WebRoot → directory where the Web application is located. Generally, the virtual directory must be configured in this folder.

License WEB-INF:This folder must be locatedThe WebRoot folder must be named in this form, and all letters must be capitalized.

Web. xml:The configuration file must be named in this format and placed inIn the WEB-INF folder.

Web. the xml format can be obtained directly from the Tomcat reference: Find webapps \ ROOT \ WEB-INF under the Tomcat Directory web. xml file, copy this file to our new WEB-INF folder, and modify this web. xml file, delete the comment in it, and leave only the following code:

Web. xml:

 1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5    version="2.5"> 6  7   <display-name>Welcome to Tomcat</display-name> 8   <description> 9      Welcome to Tomcat10   </description>11 12 </web-app>

This is the format of the web. xml file.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.