A,
When we use Idea to develop Web projects, we need to configure the server. I believe that the partners who have used Idea have experienced the process of configuring the server. However, this blog post is not to teach you how to configure the server on Idea, but an unexpected problem will be encountered when Idea runs normally. Although not everyone will encounter this problem, I believe that those who have encountered it will have a confused mood.
Second, the problem of
Let me cut the crap and get right to the point. We use Idea to develop the project and generally configure the Tomcat server. When we start the project normally, we can access the project through the URL in the browser. Yes, everything is normal. Unfortunately, if I want to access the existing resources of Tomcat at this time, or there is a function in the project to upload a file (images and other resources) to the root directory of Tomcat, I find that I cannot access the resources, even the main page of Tomcat. Is this the time to begin to doubt life? Do you feel that the Tomcat you used for so long is not configured? Do you feel the urge to reconfigure Tomcat? Ha ha, don’t be impulsive, in fact, this is a problem related to the operation mechanism of Idea, as long as a little configuration can be solved.
3. Reasons and solutions
Didn’t we start Tomcat when we ran the project? Why can’t I access Tomcat resources? In fact, Idea creates a directory for Tomcat in another place when starting the project, running the virtual Tomcat. In simple terms, it is a clone of a virtual Tomcat, and your project is running on the virtual server, so when you want to access the real server, Of course, you can’t access the resources in its directory.
So where is this “virtual server”? Look at the picture
In the second figure, the “Output directory” is the project’s running path. Only the resources in the virtual server running path can be accessed. What if we want to access the resources in the legitimate Tomcat? Very simple, as long as the legitimate Tomcat resources into the running path can be loaded, operation to see the figure.
Four,
One might ask, why should I access Tomcat’s existing resources when STARTING a project? Don’t I just care about the resources under the running project? No, no, no, no. Think about it, when a running project needs to upload a file, where should I upload this file? The files can be uploaded to the Tomcat root directory or to the project directory, but generally speaking, during the development phase of the project, the project is not packaged into a WAR package, so the project should be uploaded to the project directory. When the project is complete and packaged as a WAR package, the files should be uploaded to the Tomcat root directory. The difference is that Idea starts the project during the development phase and, after packaging, directly starts the Tomcat server access project.