Introduction to Javaweb Development The second Tomcat Server configuration tutorial _java

Source: Internet
Author: User
Tags documentation server port tomcat server

One, Tomcat server port configuration

All of Tomcat's configurations are in the Conf folder, where the Server.xml files are the core files of the configuration.

If you want to modify the boot port of the Tomcat server, port modifications can be made to the connector node in the Server.xml configuration file

For example, change the boot port of the Tomcat server from the default of 8080 to 8081 ports

Tomcat Server boot Port default configuration

<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"
  redirectport= "8443"/>

Modify Tomcat server boot port to 8081 port

 <connector port= "8081" protocol= "http/1.1" connectiontimeout= "20000"
        redirectport= "8443"/>


This changes the original default tomcat default of 8080 ports to 8081 ports, and it should be noted that once the *.xml files in the server have changed, the Tomcat server must be restarted and the new configuration information will be re-read after reboot. Since Tomcat's boot port has been modified to 8081 in the Server.xml file, the Tomcat server starts with Port 8081, as shown in the following illustration:

Access to the Tomcat server must also be accessed with a new access port: http://localhost:8081/, as shown in the following illustration:

Two, the Tomcat server virtual directory mapping method

After web application development, if you want to provide outside access, you need to give the Web application directory to the Web server management, this process is called virtual directory mapping. So how do you map a virtual directory in a tomcat server? There are several ways in total:

2.1, the virtual directory mapping method one: In the Server.xml file in the host element configuration

Locate the host element for the Server.xml file, as shown in the following illustration:

  

In <Host></Host> this pair of tags plus <context path= "/javawebapp" docbase= "F:\JavaWebDemoProject"/> You can map the Javawebdemoproject this javaweb application under F disk to the Javawebapp virtual directory, Javawebapp This virtual directory is managed by the Tomcat server. Javawebapp is a directory that does not exist on a hard disk, is a directory we write casually, that is, a virtual directory, so called "virtual directory", the code is as follows:

  
 

The context represents a Javaweb application, with two attributes in the contextual element,

Ⅰ.path: Used to configure virtual directories, must begin with "/".

Ⅱ.docbase: Configure this virtual directory to correspond to the directory where the Web application on the hard disk is located.

Using the browser to access the 1.JSP Web resource under the virtual directory of "/javawebapp", the results are as follows:

  

1.jsp can be normal access, which means that we have successfully javawebdemoproject under the F-disk of this Javaweb application map to the Javawebapp virtual directory, access to "/javawebapp/1.jsp" is equivalent to access "f : \javawebdemoproject\1.jsp "

Note: After the TOMCAT6, It is no longer recommended to add a virtual directory mapping using the configuration context element in the Server.xml file, because the Tomcat server must reboot before the Server.xml file can be reloaded after each Server.xml file is modified. This is indicated in the documentation http://localhost:8080/docs/config/context.html for the Tomcat server:

 It isn't recommended to place <Context> elements directly in the Server.xml file. This is because it makes modifying the context configuration more invasive since the main conf/server.xml file cannot eloaded without restarting Tomcat.

Individual context elements may explicitly defined:

In an individual file At/meta-inf/context.xml inside the application files. Optionally (based on the Host's Copyxml attribute) This is copied to $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 the $CATALINA _base/conf/[enginename]/[hostname]/directory. The context path and version is derived from the base name of the file (the file name less the. xml extension). This file would always take precedence over any context.xml file packaged in the Web application ' s directory.
Inside a HOST element in the main conf/server.xml.

2.2, the virtual directory mapping mode two: Let Tomcat server automatically map

The Tomcat server automatically manages all Web applications under the WebApps directory and maps them to virtual directories. In other words, the Tomcat server WebApps the Web application in the directory and can be accessed directly by the outside world.

For example: Use the javawebdemoproject of the F-disk to copy the Javaweb directly to the Tomcat server WebApps directory, as shown in the following illustration:

  

At this point, the Tomcat server automatically maps a virtual directory "/javawebdemoproject" with the same name to javawebdemoproject this Javaweb application, and then you can use the browser to access the Javaweb application's resources. As shown in the following illustration:

2.3, the virtual directory mapping mode three

Refer to Tomcat Server documentation:

  In individual files (with a ". xml" extension) in the $CATALINA _base/conf/[enginename]/[hostname]/directory. The context path and version is derived from the base name of the file (the file name less the. xml extension). This file would always take precedence over any context.xml file packaged in the Web application ' s directory.

Add a file with XML as an extension in the \conf\catalina\localhost directory of the Tomcat server, and the name of the XML file can be arbitrarily taken, such as the following aa.xml, note "The context path and Version'll is derived from the base name of the file, which translates as "The path attribute of the context element originates from the name of the XML file", as mentioned above, The Path property of the context element is used to configure the name of the virtual directory, so the name of the virtual directory is the name of the XML file.

$CATALINA _base refers to the Tomcat server root directory, [enginename] refers to the engine name used by the Tomcat server, and Tomcat uses an engine that is CATALINA

  

Add the context element mapping Javaweb application in the Aa.xml file, the code is as follows:

<context docbase= "F:\JavaWebDemoProject"/>
Note: The path attribute is not specified in the context element to set the name of the virtual directory, then the virtual directory name of the "F:\JavaWebDemoProject" Map is God horse, which is the name AA of the XML file that is currently being edited.

The biggest benefit of mapping a virtual directory in this way is that you do not have to restart the Tomcat server after modifying the configuration file, such as modifying aa.xml to Bb.xml,tomcat server automatically undeploying context [/AA] and automatically information: Deploying Configuration Descriptor D:\apache-tomcat-7.0.53\conf\Catalina\localhost\bb.xml

Three, Tomcat server configuration virtual host

3.1, configure the virtual host

Configuring a virtual host is the configuration of a Web site.
Configure a virtual host (Web site) on the Tomcat server, Need to modify the Conf folder under the Server.xml this profile, using the host element to configure, open server.xml, you can see the Tomcat server with a name called the localhost virtual host (web site), as shown in the following figure:

  

Usually we will develop a good javaweb application in the WebApps folder, and then you can use the "http://localhost: Port number/javawebappname" way to visit, in fact, is the name is "localhost" The virtual host (host), which manages all Web applications under the WebApps folder.

For example: http://localhost:8080/JavaWebDemoProject/1.jsp, This URL address is the 1.jsp Web resource in the Javawebdemoproject application under the virtual host named localhost.
We can configure a virtual host using the following methods, such as:

  
 

Here we have a new configuration of a virtual host, the name of the virtual host is "www.gacl.cn", the virtual host "www.gacl.cn" now manages all the Web applications under the Javawebapps folder, usually we use the domain name on the Internet. Www.baidu.com "Visit Baidu's website, in fact, is in the access to a name is" www.baidu.com "virtual host, so when we want to access the name is" www.gacl.cn "This virtual host, you can use the domain name ( www.gacl.cn) "To visit, pay attention to appbase=" F:\JavaWebApps ", where the Javawebapps folder is not the root of a project, but a folder that holds one or more javaweb applications, as shown in the following illustration:

   

It's like the WebApps folder of a Tomcat server, which contains a lot of javaweb applications

   

3.2, the Windows system registered domain name

Configured hosts (Web sites) to be externally accessible through a domain name, you must register the domain name used to access the Web site in the DNS server or Windows system, and locate the hosts file in the "C:\Windows\System32\drivers\etc" directory, as shown in the following illustration:

  

Edit this file to bind the domain name and IP address of the newly added web site so that we can use the www.gacl.cn domain name in the browser to access the Web apps that are managed by www.gacl.cn that virtual host.

  

Using the browser through the domain name "www.gacl.cn" access to the "www.gacl.cn" under the virtual host of the JavaWebDemo1 under this Web application 1.jsp this Web resource, "www.gacl.cn" This virtual host opened a 8080-port, Users can only access the 1.JSP Web resource in this Web application via this 8080 port JavaWebDemo1

Four, browser and server interaction process

4.1, browser and server interaction diagram

Browser and server interaction diagram

When we open the browser, enter the URL address "http://www.gacl.cn:8080/JavaWebDemo1/1.jsp" in the browser's address bar. To access the server 1.jsp this Web resource process, the browser and server have done God horse operation, how do we see in the browser 1.jsp this web resources inside the content of it?

The browser and the server do several things:

1, the browser according to the host name "www.gacl.cn" to the operating system of the hosts file to find the corresponding IP address of the host name.

2, the browser if the host file in the operating system did not find the corresponding IP address, go to the Internet DNS server to find "www.gacl.cn" the corresponding IP address.

3, the browser to find the "www.gacl.cn" the corresponding IP address of this host, the use of IP address to connect to the Web server.

4, browser connected to the Web server, the use of the HTTP protocol to send requests to the server, the process of sending a request, the browser to the Web server in the form of stream (streaming) of the data, tell the Web server to access the server in which Web application under the Web resources, as shown in the following figure

  

This is the data that the browser transmits to the server when it sends a request to the Web server, explaining the contents of "Get/javawebdemo1/1.jsp http/1.1".

Get: Tells the Web server that the browser sends the request to the server in a get way.

/javawebdemo1/1.jsp: Tell the Web server that the browser will access the 1.JSP Web resource in the JavaWebDemo1 application.

http/1.1: Tell the Web server that the browser is requested with the HTTP protocol and uses a version of 1.1.

5, after the browser finished the above 4 steps, began to wait, waiting for the Web server to access the 1.jsp this Web resource to it.

6, the server receives the data that the browser transmits, starts to parse receives the data, the server resolves "get/javawebdemo1/1.jsp http/1.1" The contents of the inside know that the client browser to access the JAVAWEBDEMO1 application of the 1.jsp Web resources, and then the server to read the content of the Web resources 1.jsp, the content will be read to stream (stream) the form of transmission to the browser, as shown in the following figure:

  

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

7, the browser to the server transmitted to its data, you can show the data to the user to see, as shown in the following figure:

The "JavaWebDemo1" you see is the result of the browser parsing the data sent back by the server

Data sent back by the server:

http/1.1 OK
server:apache-coyote/1.1
content-type:text/html;charset=iso-8859-1
content-length: 102
Date:mon, May 2014 14:25:14 GMT

 
 

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

V. Composition and structure of javaweb application

When developing javaweb applications, different types of files have strict rules of deposit, which can not only make Web applications inaccessible, but also cause the Web server to start an error

Webroot→web the directory where the application resides, typically the virtual directory will be configured in this folder.

┝web-inf: This folder must be in the Webroot folder, and must be named in this form, with uppercase letters.

┝web.xml: Configuration file, with format requirements, this file must be named in this form and must be placed in the Web-inf folder.

The Web.xml format can be directly referenced from Tomcat: Find the webapps\root\ in the Tomcat directory Web-inf This directory web.xml file, copy this file to our new Web-inf folder, and modify the Web.xml file, delete the comments inside, leaving only the following code:

Xml:

<?xml version= "1.0" encoding= "iso-8859-1"?> <web-app xmlns=
"Http://java.sun.com/xml/ns/javaee"
 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemalocation= "http://java.sun.com/xml/ns/ Java ee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
 version=" 2.5 "

 > <display-name>welcome To tomcat</display-name>
 <description>
 Welcome to Tomcat
 </description>

</ Web-app>

This is the format of the Web.xml file.

The above is the entire content of this article, I hope to help you learn.

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.