Java Web Learning II (Tomcat server learning and using 1)

Source: Internet
Author: User
Tags registering domain name server port tomcat server

One, the configuration of the Tomcat server port

All of Tomcat's configuration is placed in the Conf folder, where the Server.xml file is the core file of the configuration.

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

Example: Change the boot port of the Tomcat server from the default 8080 to 8081 port

Tomcat Server boot port default configuration

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

Modify the Tomcat server boot port to port 8081

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

This will change the original default Tomcat default 8080 port to 8081 port, it should be noted that once the server *.xml file changes, the Tomcat server must be restarted, after restarting will re-read the new configuration information. Because the Tomcat boot port has been modified to 8081 in the Server.xml file, the Tomcat server starts with Port 8081, as shown in:

  

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

  

Ii. How to map the Tomcat server virtual directory

  after the development of Web applications, if you want to be accessible to the outside world, 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 virtual directories in a tomcat server? There are a total of several ways:

2.1, Virtual directory mapping method one: In the host element of the Server.xml file is configured

Locate the host element of the Server.xml file, as shown in:

  

In <Host></Host> the label is added <context path= "/javawebapp" docbase= "F:\JavaWebDemoProject"/> You can map the Javawebdemoproject javaweb application under the 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 the hard disk, is a directory we write freely, that is, a virtual directory, so called "virtual directory", the code is as follows:

1 

Where the context represents a Javaweb application, which has two attributes,

Ⅰ.path: Used to configure virtual-like directories, you must start with "/".

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

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

  

1.JSP can be accessed normally, which means that we have successfully mapped the Javawebdemoproject javaweb application under the F-disk to the Javawebapp virtual directory, and access to "/javawebapp/1.jsp" is equivalent to "f : \javawebdemoproject\1.jsp "

Note: After Tomcat6, It is no longer recommended to add mappings for virtual directories by using the Configure context element in the Server.xml file, because the Tomcat server must restart after each modification of the Server.xml file to reload the Server.xml file. There is a description in the Tomcat server document http://localhost:8080/docs/config/context.html:

It isn't recommended to place <Context> elements directly in the Server.xml file. This was because it makes modifying the Contextconfiguration More invasive since the main conf/server.xml file cannot be rel oaded without restarting Tomcat.

Individual Context elements may be explicitly defined:

    • In a individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's Copyxml attribute) This is the copied to and $CATALINA_BASE/conf/[enginename]/[hostname]/ 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 would be 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 Meta-inf directory.
    • Inside a HOST element in the main conf/server.xml .
2.2, Virtual directory mapping method Two: Let the Tomcat server automatically map

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

For example: Copy the Javawebdemoproject from the F-disk Javaweb application directly to the Tomcat server WebApps directory, as shown in:
  

At this point the Tomcat server will automatically map a virtual directory "/javawebdemoproject" with the same name for Javawebdemoproject, which will then use the browser to access the Javaweb app's resources, as shown in:

  

2.3, the virtual directory mapping method three

Refer to the Tomcat Server documentation:

In individual files (with a ". xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version would be 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 Meta-inf directory.

This means: In the Tomcat server \conf\catalina\localhost directory to add an XML as the extension of the file, the name of the XML file can be arbitrarily taken, such as the following aa.xml, note that the word "thecontext Path and versionwould be derived from the base name of the "file", which translates to the meaning of "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_BASERefers to the Tomcat server root directory, which refers to the [enginename] name of the engine used by the Tomcat server, the engine used by Tomcat is Catalina

  

Add the Context element map Javaweb application in the Aa.xml file, as follows:

1 <context docbase= "F:\JavaWebDemoProject"/>

Note: The path attribute is not specified in the Context element to set the name of the virtual directory, so the virtual directory name of the "F:\JavaWebDemoProject" Map is God horse, which is the name of the XML file that is currently being edited AA.

  

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

  

Third, the Tomcat server configuration virtual like host 3.1, configure the web host

Configuring a virtual-like host is a site configuration.
Configure a virtual host (Web site) on the Tomcat server, You need to modify the Server.xml configuration file under the Conf folder, configure it with the host element, open Server.xml, and you can see a virtual host (Web site) with the Tomcat server that comes with a name called LocalHost, as shown in:

  

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

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

1 

Here we configure a new 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, is actually in access to a name is" www.baidu.com "the virtual host, so when we want to access the name is" www.gacl.cn "of this virtual host, you can use the" Domain name ( www.gacl.cn) "Go to visit, notice appbase=" F:\JavaWebApps ", where the Javawebapps folder represents not the root directory of an item, but a folder that holds one or more javaweb apps, as shown in:

   

It is like the WebApps folder of the Tomcat server, which contains a lot of javaweb applications.

   

3.2. Registering domain name in Windows system

Configured host (website) to be accessed externally through a domain name, you must register the domain name used to access the site in a DNS server or Windows system, and locate the hosts file under the "C:\Windows\System32\drivers\etc" directory. As shown in the following:

  

Edit this file, bind the domain name and IP address of the newly added website, so that we can use the domain name of www.gacl.cn in the browser to access the Web application that is 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 JavaWebDemo1 this Web application 1.jsp this Web resource, "www.gacl.cn" This virtual host opened a 8080 port, The user can only access the JAVAWEBDEMO1 Web application 1.jsp This Web resource through this 8080 port.

Iv. browser-to-server interaction 4.1, browser and server interaction diagram

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 the server on the 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 resource content?

The browser and server do several things:

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

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 "www.gacl.cn" host corresponding IP address.

3. After the browser finds the IP address of the host "www.gacl.cn", it uses the IP address to connect to the Web server.

4. After the browser connects to the Web server, it sends a request to the server using the HTTP protocol, and during the sending of the request, the browser transmits the data to the Web server as stream (stream), telling the Web server to access the Web resource under the Web application in the server, as shown in:

  

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

    get: Tells the Web server that the browser is sending a request to the server in a GET way.

    /javawebdemo1/1.jsp: Tell the Web server that the browser wants to access the 1.jsp Web resource inside the JavaWebDemo1 app.

http/1.1: tell the Web server that the browser was requested in the HTTP protocol, using the 1.1 version.

5, the browser after the completion of the above 4 steps to work, 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, begins to parse the received data, the server resolves "get/javawebdemo1/1.jsp http/1.1" inside the content to know the client browser to visit is JavaWebDemo1 Apply the 1.jsp Web resource inside, then the server reads the contents of the 1.jsp Web Resource, and then transmits the read content to the browser in the form of stream (stream), as shown in:

  

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

7, the browser to the server to the data transmitted to it, you can show the data to the user, as shown in:

  

See this "JavaWebDemo1" is the browser parsing server sent back the data after the effect

Data sent back by the server:

http/1.1 Okserver:apache-coyote/1.1content-type:text/html;charset=iso-8859-1content-length:102date:mon, 14:25:14 GMT<HTML>    <Head>        <title>JavaWebDemo1</title>    </Head>    <Body>JavaWebDemo1</Body></HTML>

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

V. Composition structure of JAVAWEB application

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

  

Webroot→web the directory where the application is located, in general, the virtual directory to 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, format required, this file must be named in this form, and must be placed in the Web-inf folder.

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

Xml:

<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee 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

Java Web Learning II (Tomcat server learning and using 1)

Related Article

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.