404 when connecting tomcat in Servlet

404 when connecting tomcat in Servlet

404 error will appear after creation, as shown in the following figure:

 

 

Analyze the reasons:

    1. Configuring web XML is created by yourself in Eclipse, so misplacing the path will also lead to the failure to find the tomcat server and 404 The form needs to be stored in the following way:

    2. Sometimes tomcat server startup fails, which may be due to writing web When writing XML, the servlet name is not written correctly, which leads to the failure of the server to open, or the URL pattern and servlet class are not written correctly, which leads to the failure of the path location. Therefore, the following points need to be paid attention to when writing these elements:

  <servlet>
    <servlet-name>Pac_info</servlet-name><!-- The name of the program to execute -->
    <servlet-class>test_my_1.Pac_info</servlet-class><!-- If the package name needs to be negotiated under the same directory+name -->
  </servlet>
  <servlet-mapping>
    <servlet-name>Pac_info</servlet-name><!-- The image name cannot be wrong. It must be the name of the program being executed -->
    <url-pattern>/Pac_info</url-pattern><!-- The image path is not limited in the back and front -->
  </servlet-mapping>

 

    3. Let's talk about the matching rules of URL pattern:

When the servlet container receives a url request initiated by the browser, the container will subtract the context path of the current application from the url, and take the remaining string as the servlet mapping, if the url is http://localhost:8080/appDemo/index.html , the application context is appdemo, and the container will http://localhost:8080/appDemo Remove and use the remaining / index The HTML part is used for servlet mapping and matching, and the url pattern mapping and matching process also has priority

Matching method:

  1. Exact match: the items configured in < url pattern > must exactly match the url

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/user/users.html</url-pattern>
    <url-pattern>/index.html</url-pattern>
    <url-pattern>/user/addUser.action</url-pattern>
</servlet-mapping>

When the following URLs are entered in the browser, they will be matched to the servlet
  http://localhost:8080/appDemo/user/users.html
  http://localhost:8080/appDemo/index.html
  http://localhost:8080/appDemo/user/addUser.action

 

  2. Path matching: string starting with "/" and ending with "/ *" is used for path matching

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/user/*</url-pattern>
</servlet-mapping>

The path starts with / user /, and the following path can be arbitrary. For example, the following URLs will be matched.
  http://localhost:8080/appDemo/user/users.html
  http://localhost:8080/appDemo/user/addUser.action
  http://localhost:8080/appDemo/user/updateUser.actionl

 

  3. Extension matching: strings starting with "/" and ending with "/ *" are used for path matching

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

Then any url request with jsp or action extension will be matched. For example, the following URLs will be matched
  http://localhost:8080/appDemo/user/users.jsp
  http://localhost:8080/appDemo/toHome.action

  

  4. Default match

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

 

Posted by Mobile on Sat, 14 May 2022 08:54:29 +0300