The Tomcat ROOT directory resource cannot be accessed (404 is reported for access)

foreword

Recently, I wrote a project by myself. The background server uses tomcat. I saved the user avatar in the user_head folder of the tomcat ROOT directory. I can access projects deployed in the same directory as ROOT under tomcat, but I cannot access the resources in ROOT. Access reports 404, like this:

http://localhost:8989/Serve/GetUserHabit   can access the interface in this project

http://localhost:8989/user_head/10034.jpg  Accessing resources in this ROOT directory reports 404

This kind of problem feels very weird. I checked a lot of information on the Internet and it didn’t match my problem, so I wrote this article to record it and make a contribution to the brothers’ quick solution to the problem.

Problem Cause and Solution

First talk about how I found the problem.

I saw several error messages in the tomcat startup log, which is the following:

The reason why I cannot access the resources in the ROOT directory is caused by this error. If you have the same problem as me, it is better to check the tomcat startup log to see if there is any error.

My tomcat log is viewed directly in the eclipse console, or it can be viewed directly in the logs folder under the tomcat installation directory

F:\Program Files\Apache Software Foundation\Tomcat 7.0\logs (my logs are in this location)

Find this file in the logs directory. If there is time on the file, you can check it from the most recent one.

Click to see the error log.

ok, back to the topic, let me talk about the reasons for the above problems:

After I added a listener to the servlet and configured the listener in web.xml, this error occurred. If you remove this configuration item, it will run normally, but if you open this configuration, you will not be able to access it. I don't know the specific reason. My solution is that the listener is not configured in web.xml, and the listener annotation is directly added to the class.

This is the listener configuration in web.xml. The above problem occurs when I use this configuration. If anyone knows the cause of the problem, please help explain it.

 <web-app>
 <listener>
   <listener-class>com.WebInitListener</listener-class>
</listener> 
</web-app>

This is how the annotation is done (solved my problem)

@WebListener
public class WebInitListener implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {	
	}

}

Guess you like

Origin blog.csdn.net/gaoqingliang521/article/details/118458744