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

js code to realize multi-person chat room

This article example shares the specific code of ...

Docker-compose image release process analysis of springboot project

Introduction The Docker-Compose project is an off...

Notes on upgrading to mysql-connector-java8.0.27

Recently, an online security scan found a vulnera...

WeChat Mini Program Basic Tutorial: Use of Echart

Preface Let’s take a look at the final effect fir...

How to Set Shortcut Icons in Linux

Preface Creating shortcuts in Linux can open appl...

A complete guide to CSS style attributes css() and width() in jQuery

Table of contents 1. Basic use of css(): 1.1 Get ...

MySQL SQL statement to find duplicate data based on one or more fields

SQL finds all duplicate records in a table 1. The...

25 CSS frameworks, tools, software and templates shared

Sprite Cow download CSS Lint download Prefixr dow...

HTML tags list and usage instructions

List of HTML tags mark type Name or meaning effec...

Summary of 11 common mistakes made by MySQL call novices

Preface You may often receive warning emails from...

Configure VIM as a C++ development editor in Ubuntu

1. Copy the configuration file to the user enviro...

CSS to achieve fast and cool shaking animation effect

1. Introduction to Animate.css Animate.css is a r...

How to use Docker to build enterprise-level custom images

Preface Before leaving get off work, the author r...