Detailed explanation of the solution to Tomcat's 404 error

Detailed explanation of the solution to Tomcat's 404 error

The 404 problem occurs in the Tomcat test. The problems are as follows:

HTTP Status 404 - Not Found Type Status Report Message The requested resource [/chapter06/IndexServlet] is not available Description The origin server was unable to find a representation for the target resource or is unwilling to expose an existing representation for the resource.
Apache Tomcat/9.0.37

The problem is that the browser cannot directly access the Java file.

Solution to the problem

1. This problem occurs when submitting a form

<form name = "reg" action="/login" method="post">
 <meta charset="UTF-8">
 Account: <input type="text" name="username"/><br>
 Password: <input type="password" name="passward"/><br>
 <input type="submit" value="Submit" id="bt">
</form>

The reference in <form name = "reg" action="/login" method="post"> action=" " must be a routing address (this routing address needs to be configured by yourself (for example, the route in the Java class responsible for receiving the form I configured myself is /login, not the access path of my own Java class /chapter06/src/LoginServlet)
The way to configure routing for Java files is as follows:

2. Accessing the Java file in the src path reports an error

You need to configure routing for the target Java file. There are two ways to configure it: (This is just a brief description. If you want to know more details, click here)

(1): Configuration based on annotations

Only applicable to Tomcat 3 and above (I think it is 3, I can't remember)
Add in the Java class

package cn.itcast.chapter06.session.example;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/logout") //Here is the routing configuration public class LogoutServlet extends HttpServlet {
 @Override
 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  req.getSession().removeAttribute("user");
  resp.sendRedirect("/wel");
 }

 @Override
 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  doGet(req, resp);
 }
}

**@WebServlet("/logout") //Here is the routing configuration, which is also the most commonly used method. **The quotation marks are for routing

(2) Configuration based on XML file

Click to open the web.xml file, then modify the web.xml code and add a mapping before < /app >

<servlet> 
	 <servlet-name>hello</servlet-name> 
	 <servlet-class>com.southwind.servlet.HelloServlet</servlet-class>
 </servlet>
<servlet-mapping> 
	<servlet-name>hello</servlet-name> 
	<url-pattern>/demo2</url-pattern> 
</servlet-mapping>

Map demo2 and hello. You can map HelloServlet by directly accessing demo2 in the browser.

This is the end of this article on how to solve the problem of 404 error in Tomcat. For more information about 404 error in Tomcat, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • vue-cli history mode implements the solution of tomcat deployment reporting 404
  • Solve the problem of error 404 when starting tomcat after importing ssm project into idea
  • Solution to the problem of 404 error when Vue project webpack is packaged and deployed to Tomcat and refreshed
  • Detailed explanation of configuring 404 custom error pages in Tomcat
  • How to solve the 404 problem when starting eclipse's tomcat for access
  • Tomcat starts normally, but all pages you visit report 404 exceptions. Summary and analysis of 404 exceptions

<<:  Using js to achieve waterfall effect

>>:  Setting the engine MyISAM/InnoDB when creating a data table in MySQL

Recommend

MySQL column to row conversion and year-month grouping example

As shown below: SELECT count(DISTINCT(a.rect_id))...

Sample code for installing Jenkins using Docker

Two problems that are easy to encounter when inst...

Web design tips on form input boxes

1. Dashed box when cancel button is pressed <br...

How to process local images dynamically loaded in Vue

Find the problem Today I encountered a problem of...

ElementUI implements cascading selector

This article example shares the specific code of ...

JavaScript ES6 Module Detailed Explanation

Table of contents 0. What is Module 1.Module load...

Summary and analysis of commonly used Docker commands and examples

Table of contents 1. Container lifecycle manageme...

HTML markup language - table tag

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed explanation of mixins in Vue.js

Mixins provide distributed reusable functionality...

Circular progress bar implemented with CSS

Achieve results Implementation Code html <div ...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

Vue implements websocket customer service chat function

This article mainly introduces how to implement a...

How to build lnmp environment in docker

Create a project directory mkdir php Create the f...

Summary of front-end knowledge in the Gokudō game

background In the early stages of learning Japane...