Debugging analysis of 404 errors - Tomcat always reports 404 errors when running JSP dynamic web pages (detailed explanation)

1. When developing JSP dynamic web pages, when we request a certain resource on the server through the browser, we may often encounter bugs reporting 404 errors.

       Problem Analysis: The cause of this bug may be in the JSP webpage or in the Servlet. If the resource to be accessed does not exist, a 404 error will be generated.

       (1) The 404 error may be a problem with the application itself. For example, there is no normal deployment, and the Servlet name is wrongly written when web.xml is deployed.

       (2) It may also be a file problem, the JSP file does not exist, the JSP name is wrong, or the Servlet is not configured



2. About several different representations of servlet configuration parameter url-pattern (Servlet path), or several access methods of Servlet, the following shows and introduces in the form of code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.ccsu.zlWeb.TestServlet</servlet-class>
  </servlet>
  
<!-- 默认方式访问,使用单个斜杠("/")作为url-pattern参数值 -->
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
<!-- url-pattern参数值为[*.扩展名 ](扩展名是任意的)访问
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/zl.ksgfj</url-pattern>
  </servlet-mapping>
-->
  
<!-- 路径映射访问,url-pattern参数值为[/*/*],"*"可取任意值
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/xdy/fhdsdjklf</url-pattern>
  </servlet-mapping>
 -->
  
<!-- 精确匹配路径访问,url-pattern参数值为对应于web应用程序上下根的路径
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/servlet/TestServlet</url-pattern>
  </servlet-mapping>
-->	

<!-- 注意,采用默认方式访问,需把此参数删掉,另外还需把index.jsp也删掉
  <welcome-file-list>
  	<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 -->
</web-app>

3. Common cases of 404 errors:


[1] The url-pattern tag/parameter is incorrect. At this time, check the work\Catalina\localhost directory of tomcat, and you cannot see the java source file and class file compiled by the JSP. like:

         "<url-pattern>/ShowContact</url-pattern>"误写成"<servlet-url>/ShowContact</servlet-url>"

(ps: In general IDE tools, the web.xml file is automatically generated when the project is created, and the Servlet configuration is also automatically generated when the Servlet is created. Maybe sometimes we configure it ourselves, or we may accidentally change it. got something)


[2] When web.xml configures the Servlet, the name of the Servlet is wrong, which is inconsistent with the name of the Servlet file (more common)


[3] When web.xml configures the Servlet, the servlet-class parameter (should be the full class name of the Servlet) is wrong, that is, the full path of the Servlet class is wrong

        Let me share with you the shortcut to get the full class name of the Servlet:

         1. Open the Servlet file that needs to configure the Servlet, and find the servlet class name (modified by Public class)

         2. Move the cursor to the servlet class name , double-click it (it will change color at this time)

         3. Then right-click the mouse, a drop-down menu will appear, click "Copy Qualified Name" (similar to copy operation)

        4. Then go back to the place where you want to configure its class path, and just paste it (in fact, the full class name of the Servlet class will appear if you paste it anywhere).


(Operation example: double-click the class name ShowContact of a Servlet file (ShowContact.java) --- > right -click it ---> click "Copy Qualified Name" ---> paste. The Servlet class will appear next The full class name "cn.ccsu.web.ShowContact", not the name of the Servlet)


[4] The welcome-file-list in web.xml is not configured; or it is configured, but the parameter of welcome-file is wrong. That is, the name of its parameter value *.jsp or *.htm or *.html is wrong, and it is inconsistent with the name of the web page file


【5】找不到资源,即文件不存在(较为常见),即路径中的资源名字不对,打错了;或者该资源本来就没有。

Guess you like

Origin blog.csdn.net/u014698745/article/details/45935021