Java Web Learning Summary (1) Tomcat use tutorial

Source: Internet
Author: User
Tags server port tomcat server

First, Introduction

Tomcat is the smallest Web server that implements the Java EE Standard and is a core project in the Apache Software Foundation's Jakarta Project, developed by Apache, Sun, and other companies and individuals. Because of advanced Tomcat technology, stable performance, and open source free, so deeply loved by Java enthusiasts and have been recognized by some software developers, become the most popular Web application server. Learning Javaweb development generally uses the Tomcat server, which supports all JSP and servlet specifications, starting the interface

The architecture of Tomcat

The Tomcat server is started based on a server.xml file, and when Tomcat starts it starts with a server,server inside it will launch Service,service inside will start multiple "Connector (connectors)", Each connector waits for the client to connect, when a user uses a browser to access the Web resources above the server, the first is to connect to the Connector (connector), Connector (connector) is not processing the user's request, but the user's request to an engine (engines) To deal with, the engine will parse the host that the user wants to access after receiving the request, and then hand over the request to the appropriate host,host to resolve the Web application that the user wants to access under the host, a Web application that corresponds to a context.

Full Server.xml configuration file contents:

<?XML version= ' 1.0 ' encoding= ' utf-8 '?><ServerPort= "8005"shutdown= "SHUTDOWN">  <Servicename= "Catalina">    <ConnectorPort= "8080"Protocol= "http/1.1"ConnectionTimeout= "20000"Redirectport= "8443" />    <ConnectorPort= "8443"Protocol= "Org.apache.coyote.http11.Http11Protocol"MaxThreads= "Max"sslenabled= "true"Scheme= "https"Secure= "true"ClientAuth= "false"Sslprotocol= "TLS"Keystorefile= "Conf/.keystore"Keystorepass= "123456"/>    <ConnectorPort= "8009"Protocol= "ajp/1.3"Redirectport= "8443" />    <Enginename= "Catalina"Defaulthost= "localhost">       <Hostname= "localhost"AppBase= "WebApps"Unpackwars= "true"Autodeploy= "true">        <ValveClassName= "Org.apache.catalina.valves.AccessLogValve"Directory= "Logs"prefix= "Localhost_access_log."suffix= ". txt"pattern= "%h%l%u%t &quot;%r&quot;%s%b" />      </Host>     </Engine>  </Service></Server>

two. Download and install the Tomcat server

official: http://tomcat.apache.org/ ,

Baidu Cloud Download (contains version 6-9): Link: https://pan.baidu.com/s/1nvspdd3 Password: QUWW

You can use it after you unzip it.

three, Tomcat directory hierarchy

Four, start / Test tomcat server

Double-click the Startup.bat file in the bin directory to start the Tomcat server

Open the browser, enter localhost:8080, can display the following interface to indicate the successful installation.

Five, Tomcat server startup FAQ

1, Java_home environment variable setting problem

To double-click the Startup.bat file in the bin directory to start the Tomcat server, the first thing to do is to set the JAVA_HOME environment variable in Windows, because the Tomcat server starts with this java_home environment variable, If the JAVA_HOME environment variable does not exist in Windows, the Tomcat server cannot be started.

2. Port occupancy Issues

Because the Tomcat server starts with a default of 8080 port, if the 8080 port is occupied by another application, then the Tomcat server will not start properly, the phenomenon is that "the Tomcat server boot interface will print an exception error message, and then automatically shut down the ", we go directly to log file Logs/catalina.xxxx-xx-xx.log,:

Log information is clearly recorded in the operation of the Tomcat server, because the 8080 port is occupied and unable to start normally, will record the exception information as shown, by viewing the exception information, you can know why the Tomcat server can not start properly!

six, Tomcat server port settings

The port modifications are made by 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

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

Modify the Tomcat server boot port to port 8081

<port= "8081"  protocol= "http/1.1"                connectiontimeout  = "20000"                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:

Seven, how the Tomcat server virtual directory is mapped

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 3 different ways to do it in total:

1, method One: configured in the host element of the Server.xml file

The host element of the Logs/server.xml file for Tomcat, as shown in:

<Hostname= "localhost"AppBase= "WebApps"Unpackwars= "true"Autodeploy= "true">     <!--Singlesignon valve, share authentication between Web applications documentation at:/docs/config/valve.html /c5> -    <!--<valve classname= "Org.apache.catalina.authenticator.SingleSignOn"/> -     <!--Access Log processes all example. Documentation at:/docs/config/valve.html note:the pattern used are equivalent to using pattern= "common" -    <ValveClassName= "Org.apache.catalina.valves.AccessLogValve"Directory= "Logs"prefix= "Localhost_access_log."suffix= ". txt"pattern= "%h%l%u%t &quot;%r&quot;%s%b" />  </Host>

In <Host></Host> the label is added <context path= "/javawebapp" docbase= "D:\JavaWebs"/> You can map the Javawebs in the D-disk javaweb application 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 our own random write a directory, that is, a virtual directory, so called "virtual directory", the code is as follows:

<Hostname= "localhost"AppBase= "WebApps"Unpackwars= "true"Autodeploy= "true">    <ValveClassName= "Org.apache.catalina.valves.AccessLogValve"Directory= "Logs"prefix= "Localhost_access_log."suffix= ". txt"pattern= "%h%l%u%t &quot;%r&quot;%s%b" />    <ContextPath= "/javawebapp"DocBase= "D:\JavaWebs" /></Host>

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:

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.

2, way 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 Javawebs from the D-disk javaweb application directly to the Tomcat server WebApps directory, as shown in:

The Tomcat server will now automatically map a virtual directory with the same name "/javawebs" for the Javawebs javaweb app

Background log:

As shown in the following:

3, way three

Add a file with XML as an extension in the \conf\catalina\localhost directory of the Tomcat server, and the XML file name can be arbitrarily taken, such as Javawebs.xml

Add the context element map Javaweb application in the Javawebs.xml file, as follows:

<docBase= "D:\JavaWebs"/>

Note: The path attribute is not specified in the context element to set the name of the virtual directory, so what is the name of the virtual directory for the "F:\JavaWebs" mapping, which is the name of the XML file that is currently being edited Javawebs.

Note: The biggest benefit of mapping a virtual directory in this way is that you do not have to restart the Tomcat server after you modify the configuration file

Java Web Learning Summary (1) Tomcat usage Tutorial

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.