IDEA deploys Tomcat and solves the problem of Servlet 404

​ I have been working on a web project these few days. I haven’t touched a servlet for almost a year. Today I deployed a 404 for more than two hours. Then I was crazy about Baidu and configured crazy. It was a bit like the first time I deployed Tomcat. , Hhh~~ The deployment was successful in the end, so I will write a blog to record my mistakes and summarize my experience by the way.

​ The first step is to create a new Java web project.
Insert picture description here
Then we need to create two new folders, one is classes and the other is lib . Lib is used to store jar packages, and classes are used to access servlets. Note that they must be placed in web/WEB-INF
Insert picture description here
After that, we can import the package . After the
Insert picture description here
blue, we will create a new servlet and configuration xml file.

Configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>control.HelloServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

servlet class

public class HelloServlet extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.getWriter().println("<h1>Hello Servlet</h1>");
        resp.getWriter().println(new Date());
    }
}

Then we start to deploy tomcat, click this,
Insert picture description here
and then click local. Local means local.
Insert picture description here
Here, change the name and other defaults.
Insert picture description here
Mainly the second page
Insert picture description here
. If you want to visit the index and also want to visit the official website of tomcat, you must add tomcat Put ROOT in the installation directory of

Finally apply and then start tomcat.

The result is shown in the figure.
Insert picture description here
Insert picture description here
Insert picture description here
Basically configure tomcat and access servlet like this. Then let's go to the sauce, come on, code farmers~

Guess you like

Origin blog.csdn.net/weixin_44475741/article/details/112630801