Idea web project starts with resources but still displays 404

I asked a friend for the project and when I tried to run it on my computer, the automatically opened "localhost/login.jsp" showed a 404 error.

background

The project was compressed and sent directly from a friend, so the project configuration file was also retained. You need to modify some configurations when using your own computer. Mainly modified Maven-related paths and local tomcat paths (versions are inconsistent).

question

After modifying the error-reported configuration, run the project directly. The URL that is automatically opened is "localhost/login.jsp", but a 404 error is displayed, that is, the path is incorrect.

So I did a preliminary check, changed the original port 80 to 8080, cleaned it with Maven and repackaged it. Rerunning still shows 404 error. Then Baidu checked and checked that all the resources in the target folder were complete. The current project was indeed deployed at runtime, but the result was the same. I have no clue, so I can only look for clues in the logs. I was confused about the path of CATALINA_BASE "C:\Users\Admin\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\login_jsp_project" in the log, so I continued to search for relevant information.

Finally, I discovered that my friend could use "localhost/xxx" on his computer to directly access project resources, which meant that his project was deployed to the root directory of tomcat. My project is deployed under "/project_war" and needs to access "localhost/project_war/xxx", so the next step is to find out where to set it.

Solution

1. There is no corresponding resource in the target directory (it is not a problem I encountered, and it is also recorded)

Running tomcat directly on Idea or packaging with Maven will generate a target folder in the project root directory. project-1.0-SNATSHOT and project-1.0-SNATSHOT.war under the target folder are the project folders running on tomcat

The file name is defined in pom.xml

 

 Check whether the resource you want to access exists in the project folder mentioned above. If not, just copy it from webapps.

 2. Configure the virtual directory (the problem I encountered)

 Open the folder according to the CATALINA_BASE path in the log information

Using CATALINA_BASE:   "C:\Users\Admin\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\login_jsp_project"

Using CATALINA_HOME:   "D:\Apache-tomcat-9.0.20"

(CATALINA_HOME in the log information represents the installation directory of tomcat, and CATALINA_BASE represents the working directory or project directory)

 Then open xxx.xml under "conf\Catalina\localhost" and you can see the following content

<?xml version="1.0"?>
<Context docBase="D:\project\target\project-1.0-SNAPSHOT.war" path="/project_war"/>

The context tag can be used to map a directory on the local system to a virtual directory that can be accessed by a web browser. The path attribute specifies the virtual path of the web application. The docBase attribute is used to specify the directory address in the local system where the virtual directory is located. You can use an absolute address or a relative address relative to the webapps directory.

Taking the above content as an example, accessing "localhost/project_war/xxx" will access the xxx resource in the directory "D:\project\target\project-1.0-SNAPSHOT.war" on the computer.

Then when you find the place, you can check whether the corresponding path is correct.

This path is set in Run -> Edit Configurations in Idea, click Deployment

 The following Application context is what idea will write in the path attribute in the above context tag when we run the project. When the value is empty, the project will be set as the default project, that is, just access "localhost/xxx" , at this time the path of the context tag=""

 Run the project again and the effect will be the same as when your friend opened it.

additional supplement

Context represents a single web application running on the virtual host host. Web applications can be flexibly published by configuring the context element. Context can be configured in many places, but tomcat will search for the element in a certain order. If it is found, it will use the element and will not continue to search. Therefore, context elements in different places have priority. The search order is as follows:

  1. Find the context element in the <CATALINA_HOME>/conf/context.xml file. This file is suitable for all web applications.
  2. Find the context element in <CATALINA_HOME>/conf/[enginename]/[hostname]/context.xml.default. Applicable to all web applications.
  3. Search in the <CATALINA_HOME>/conf/[enginename]/[hostname]/[contextpath].xml file. It only applies to a single web application.
  4. Find the context element in the META-INF/context.xml file of the web application. It only applies to the current web application.
  5. Find the context element in the host element in the <CATALINA_HOME>/conf/server.xml file. All web application context elements can be configured in serverl.xml, but since they have the lowest priority, they are likely to be overwritten by other context elements.

References and Citations

The maven project in IDEA successfully starts tomcat, but the jsp page displays 404_idea starts tomcat404_Learn well yyy's blog-CSDN blog

Some details of Idea configuring tomcat - Jianshu (jianshu.com)

The path and docBase attributes of Context in tomcat_context docbase_Qianshuang's blog-CSDN blog

tomcat basics_tomcat host context priority_Qianshuang's blog-CSDN blog

Infringement contact deleted

Feel free to point out errors

Guess you like

Origin blog.csdn.net/weixin_59537209/article/details/129804055