Tomcat Detailed usage Learning (iii)

Source: Internet
Author: User

This article is followed by a "Tomcat detailed usage Learning (ii)", which focuses on the organizational structure of Web applications required by the server.

The previous article talked about how to use a server to map your Web app to a virtual directory so that you can access your Web resources that you develop in your browser. However, when developing your own Web application, you also need to have a server-specific structure, so that the server will know what to do as accessible content and what configuration files do not need to be exposed to the outside world when making virtual directory mappings for outside access.

If we have a web App with the name "Mail" (and also the name of the directory where the Web app resides), then the different types of files within their directory should be subject to the following rules:

  

Generally speaking:

For HTML, JSP, CSS, JS files, etc., can be placed directly in the Web application in the directory, of course, if some files are more, you can create some additional directories to store .

In addition to the above several files, some do not want to be accessible to the outside world files must be placed in the "Web-inf" directory, the file name is not allowed to change, pure capitalization, there can be no difference, otherwise it must be wrong, In this directory, we will store some of the Java files we need to develop (should be placed in the "classes" directory), Java run the required jar package (placed in the "Web-inf" directory of the "Lib" directory, can also be placed in the Tomcat home directory in the "Lib" directory), There is also a very important "Web. xml" file, although this file is written by us, but the filename must be "web. xml" Cannot change, this file can have us to our own development of the Web application configuration.

In the last "Tomcat detailed usage study (ii)", it was said that the question of the home page, in fact, because there is no custom Web. xml file in the custom website, so my web App with Tomcat's "conf" Web. XML in the directory is configured, let's take a look at how Tomcat's "Web. xml" file is configured:

<welcome-file-list>        <Welcome-file>Index.html</Welcome-file>        <Welcome-file>Index.htm</Welcome-file>        <Welcome-file>index.jsp</Welcome-file></welcome-file-list>

At the end of Tomcat's "web. xml" file, there are three sentences above, meaning that the home page is "index.html", if it does not exist then the first page is "index.htm", if it does not exist, then the first page is "index.jsp". So even if my web app does not have a dedicated custom Web. xml file to set up my home page, I can open it as the homepage of my web app as long as I have a custom "index.html" in the same directory as my web App.

So in general we have to include their own web application in the "XML" file, then how to write it?

  The simplest way to write is to get useful information from the Web. XML in the Tomcat "conf" directory, such as the head and tail, plus the functionality that you need, which can be extracted from this "template" file.

For example, I want to customize my home page, no longer use the Web application in the same directory to define a index.html file, only need to remove the header and tail from the template, plus set the home page function.

If my Web application is in the directory "Mail", under which I have customized the "pages" directory to hold all HTML pages, I would like to use "1.html" as the homepage of my app, then the custom Web. XML content in my app is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.1"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">    <welcome-file-list>        <Welcome-file>/pages/1.html</Welcome-file>    </welcome-file-list></Web-app>

Notice how this path is written, and if you start with a directory, you must have a "/" before the table of contents. After you've configured the home page, you can simply type the Web app's virtual directory as a path by opening it from the browser:

  

So you can use this method to set up your Web app's home page in the future.

But will complain, not still have to enter the path of the Web application, this does not have to lose to calculate the true meaning of the home Ah!!!

Yes, so we need to use the first method of virtual directory mapping mentioned in "Tomcat Detailed usage Learning (ii)", adding <Context> tags to the <Host> tag in the Server.xml file, and set the Path property to the default value.

In the Server.xml file, modify:

 <  host  appbase  = "WebApps"   Autodeploy  = "true"   name  = "localhost"   Unpackwars  = "true"     >  <  context  path  = ""   DocBase  = "F:\TomCat_8.0.24\webapps\mail"  />  </ host  >  

Combine the home page set by the custom Web. xml file in the browser as follows:

  

Homepage Complete!! In addition to the Tomcat "Server.xml" file to change the port to 80, you do not need to enter the port, the direct IP address can access the home page!

Of course, the Web. xml file is not just used to configure a website as the homepage of a site, it can also map a servlet program to a URL address, configure a listener for a web app, configure a filter for a web app, and so on. So the Web. xml file is the most important configuration file for the entire development of the application!

  

Tomcat Detailed usage Learning (iii)

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.