Tomcat - Context pathThe context path is a key element of a web application. It's also used for a virtual host. Virtual hosting can be defi ned as a method through which you can host multiple domain names on the same web server or a single IP. The context path is also used to defi ne the URL mapping for the .war fi les. Many people ask why we need the context path. Instead, can we deploy the application on one root directory? The answer is, by defi ning the context path, we minimize the load on the server. When the server gets the request with the URL, it will check the server.xml or context path for the defi ned URL. If it's found, then the URL will be served from here, otherwise the server has to search all the deployed WAR fi les. Hence, the context path reduces the CPU cycle. The second important advantage is, it gives us freedom to customize the application based on our requirement, such as logging, appBase, DB connection, and so on. Let's consider a scenario for a large enterprise where a single application needs to be deployed on 100 Tomcat servers. Now it's impossible to deploy the application on every server, and so, in that case Common NAS share is used for the application deployment. Enabling the context path The context path in Tomcat can be enabled in two ways:
For enabling the context path in the Tomcat Manager, you have to fi rst log in to the Tomcat Manager app using the URL http://localhost:8080. Then click on Manager App, as shown in the following screenshot: You can create the context path using the Deploy tab. Click on Browse and select the required WAR fi le. Then click on Deploy. It will take 10 to 15 seconds to deploy the application and you will see a page .
Downloading the example code
You can download the example code fi les for all Packt books you have
purchased from your account at http://www.packtpub.com. If you
purchased this book elsewhere, you can visit http://www.packtpub.
com/support and register to have the fi les e-mailed directly to you
Command-line confi guration in server.xml Another way of adding the context path in Tomcat 7 is by editing server.xml. But, you need to have a good understanding of XML. Let's quickly discuss the changes that need to be done on the Tomcat server.
<Context path="/sample" docBase="/opt/" reloadable="true"
swallowOutput="true"> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Logger className="org.apache.catalina.logger.FileLogger" prefix="www-sample-com-log." suffix=".txt" timestamp="true"/> </Context> Now, it's time to discuss the parameters defi ned in the context path. The previous screenshot shows the details for the context path.
It's always recommended to take the backup of the existing
confi guration fi le before performing changes in Tomcat.
|