In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about the method of tomcat directory mapping. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Concept of web application: a web application contains many web resources that we have done well, which may include multiple static web resources and dynamic web resources, such as html, css, js files, jsp files, java programs, jar packages and configuration files, etc. For example, my custom directory [fjdingsdapp] in the [webapps] directory of tomcat contains a 1.html file. Then all the resources in the entire runnable file of [fjdingsdapp] can be called a web application I developed, referred to as web application, while [fjdingsdapp] which contains all the web resources I developed is called the directory where my web application is located.
After we have developed the web application, if we want to provide access to the outside world, we need to give the directory where the web application resides to the server for a long time. This process is called virtual directory mapping.
There are three ways to map the virtual directory of the server:
The first (not recommended):
Although this method is not recommended, it can be used to set the home page of your own web application without typing the virtual directory name and resource name as the url path.
Find the host tag element in the server.xml file and use the context tag under it. Note that this is manipulating the xml file, the xml file is case-sensitive, and a context tag represents a web application.
Suppose my tomcat server is on the [f] disk and its [webapps] directory does not have my web application, while my web application is placed in the [mywebapp] directory of the [d] disk. There is a 1.html file in this directory. In order to access my web application in the browser, I find the host tag in the tomcat server.xml file, add the context tag and configure the attribute (note that there is no closing tag, pay attention to the ending method /) The following is an example:
Hostappbase= "webapps" autodeploy= "true" name= "localhost" unpackwars= "true" contextpath= "docbase=" d:\ mywebapp "/ / host
Path attribute: the name of the virtual directory, that is, the external access path, which may not exist on the local hard disk, but if the path attribute has a value, it must be entered in the browser address bar. This is the default.
Docbase attribute: the directory where the web application is located, which must exist on the hard disk. In this directory are our web resources.
Each time you configure the server.xml file, you must restart the tomcat server.
Because we set the path property of context to the default value, we only need to enter the resource name after the address and port:
If the path attribute has a value, such as path=123 (randomly written)
Contextpath= "123" docbase= "d:\ mywebapp" /
Then the browser must have this path when typing in order to access the corresponding file:
Ending: a context tag represents a web application, so there can be multiple web applications under the host tag, but only one of the path attributes in these context is the default. In addition to mapping virtual directories in the configuration file, the context element can also be used to configure resources for web applications, such as configuring database connection pooling used by web applications, javamailsession, and so on (these configurations will be described later).
This method is not recommended because the description of the tomcat server indicates that this method requires frequent changes to the server.xml file, and each change requires a restart of the server
See the following instructions:
=
The second kind:
Also in tomcat's description of the context tag, there is a way:
This is another way to define context tags without having to define them under the host tag. The advantage of this approach is that you don't have to restart tomcat every time, so what should you do with this approach?
First of all, we need to customize a xml file, and the file name of the xml file (excluding the ".xml" part) will be used as the path name entered by the browser in the future, similar to the file name is the virtual directory name (that is, the external access path), we need to add the context element in this custom xml file, this way there is no need to add the path attribute, because the file name has been replaced.
Secondly, there is such a path in the figure above: $catalina_base/conf/ [enginename] / [hostname] / directory. I set the home directory of tomcat, and conf happens to be the name of the configuration file directory under tomcat, so what are the directories corresponding to [enginename] and [hostname]? We open the server.xml file and see that there are two tags, engine and host (host is the one mentioned in the first way)
Enginedefaulthost= "localhost" name= "catalina"
... (other content is omitted here)
Hostappbase= "webapps" autodeploy= "true" name= "localhost" unpackwars= "true" / host/engine
You can see that there happens to be a name attribute in both tags. The name attribute value of the engine element is the name attribute value of the catalina,host element is localhost, which happens to be the [catalina] directory under the [conf] directory and the [localhost] directory under the [catalina] directory:
In the [localhost] directory is the custom xml file we want to place.
Now, my web application is located in the [mywebapp] directory in the [d] disk, and this time there is no need to configure the path in the server.xml file. I create a rr.xml file under [conf]-[catalina]-[localhost] under the tomcat directory:
Place my web application under the d disk
Placing my xml profile in the specified tomcat path will map to my web application
The code in the rr.xml file is just one line:
Contextdocbase= "d:\ mywebapp" /
As I said before, the custom xml file name is used as the external access path, so if you want to access my web application in the browser, you should enter it like this, and you can see:
Of course, this is not over. Keep the server on and do not turn it off. We create a "dsd.xml" file under [conf]-[catalina]-[localhost] in the tomcat directory. The content is the same as the rr.xml file. Modify the browser path, and you can see:
Yes, this approach avoids frequent restarts of the tomcat server.
Customized xml files can add multiple levels of access paths by file name, separated by #, for example:
Then the browser address bar should be entered like this:
Of course, the first way also has a default type, so can the second way be the same? then you need to define a root.xml file. Yes, the file name is root, which must be all uppercase, and the content in root.xml is still
Contextdocbase= "d:\ mywebapp" /
Then the browser can omit the virtual directory name (external access path), as follows:
The last part of context:
The context tag has a reloadable attribute that can run tomcat to automatically load updated web applications, especially when there are new java program updates in web applications, but this attribute should be avoided. Web applications are usually large, and if this attribute is set, the tomcat server is easy to get stuck, so it is not recommended.
The third kind:
Let tomcat map automatically, and tomcat will automatically manage all web applications under its [webapps] directory and map these web applications to virtual directories. So we just need to say that the web application we developed is placed in the [webapps] directory of tomcat. The path name for external access is the name of the directory where the web application is located.
For example, I create a custom directory [mywebapp] under the [webapps] directory, which contains a 3.html file.
Then enter the address in the browser:
In this way, there is no need to restart the server.
In this way, you can specify the default page of the directory where the web application is located, and define an index.html under the directory [mywebapp]. The file name must be index, and other file names will not be found. In this case, you only need to type the address and the directory where the web application is located (as a virtual directory) in the browser. You do not need to type the corresponding resource name (no more index.html is required):
These are all the contents of this article entitled "tomcat Directory Mapping method". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.