IDEA2021 tomcat10 servlet newer version pitfalls

IDEA2021 tomcat10 servlet newer version pitfalls

Because the version I used when I was learning was relatively new, and the tutorials on the Internet were all old versions, many problems occurred. The following is a summary to help other beginners avoid pitfalls.
Without further ado:

1:
File->new->project creates a normal Java project:

The project name can be named arbitrarily

2:
Right click on the project name->Add Framework Support:

Check Web Application and click OK

3:
Expand the project name->web->WEB-INF, and create two new folders under WEB-INF, namely classes and lib:

4:
Press ctrl+alt+shift+S to bring up Project Structure.
Select Modules->Paths, select use module xxxxx in the radio button, and change the two paths to the classes just created.

Then select Dependencies, click the + sign below, select jars or dirxxxxxxxx, select the lib directory you just created, and if you want to select the directory to use, select jar direxxxxxxx, check it, click apply, OK

5:
Copy the servlet-api.jar in the tomcat/lib directory to the lib directory we created.

6:
Click Add Configuration next to the hammer in the upper right corner, click the plus sign, and select tomcat server->local. Be careful not to select tomEE here. The two icons are the same, but they are not the same thing. Other configurations remain unchanged. Click fix on aplly. The application context can be named as you like. It is recommended that a / is sufficient. Then aplly, OK.

7:
Change the title and end in index.jsp and run it. If it is similar to the following, it is basically OK.

8:
Create a new java class in src and try to write a servlet:
This is also different from other versions. The old versions all import javax.servlet.xxxxx, here it is import jakarta.servlet.xxxxx. You can expand servlet-api.jar to see the specific package that should be imported.

insert image description here

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "login")
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML>");
        out.println("<HTML>");
        out.println(" <HEAD><TITLE>login</TITLE></HEAD>");
        out.println(" <BODY>");
        out.print(" this is login page");
        out.print(this.getClass());
        out.println(" </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML>");
        out.println("<HTML>");
        out.println(" <HEAD><TITLE>login</TITLE></HEAD>");
        out.println(" <BODY>");
        out.print(" this is login page");
        out.print(this.getClass());
        out.println(" </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}

Then modify the web.xml file as follows:

servlet

-name can be any name as long as the upper and lower parts are consistent. servlet-class should be the same as the class name. url-pattern should be the same as xxxx in @WebServlet(name="xxxx") in java class, where xxxx is the path.
Compile and run at this time, enter the URL we wrote in the address bar, and you can access the dynamic resources:

This is the end of this article about the pitfalls of the newer version of IDEA2021 tomcat10 servlet. For more related idea2021 tomcat servlet content, 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:
  • IntelliJ IDEA2021.1 Configuration Complete (Super Detailed Tutorial)
  • The latest idea2021 latest activation super detailed tutorial
  • The latest idea2021 registration code is permanently activated (activated until 2100)
  • About the latest IDEA2020.2.1, 2.2, 3 and above cracking, activation failure, reactivation issues
  • IntelliJ IDEA 2020.2.3 permanent cracking and activation tutorial (tested and effective)
  • Detailed explanation of configuration and reinstallation issues after installation of IDEA2021.2

<<:  Example code for hiding element scrollbars using CSS

>>:  Optimizing JavaScript and CSS to improve website performance

Recommend

Method of building redis cluster based on docker

Download the redis image docker pull yyyyttttwwww...

How to communicate between WIN10 system and Docker internal container IP

1. After installing the Windows version of Docker...

Spring Boot layered packaging Docker image practice and analysis (recommended)

Table of contents 1. Prepare the springboot proje...

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

Win7 installation MySQL 5.6 tutorial diagram

Table of contents 1. Download 2. Installation 3. ...

Detailed steps for debugging VUE projects in IDEA

To debug js code, you need to write debugger in t...

Mac+IDEA+Tomcat configuration steps

Table of contents 1. Download 2. Installation and...

JS realizes the automatic playback effect of pictures

This article shares the specific code of JS to ac...

Native js implementation of magnifying glass component

This article example shares the specific code for...

How to implement mobile web page size adaptation

I finally finished the project at hand, and the m...