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

Basic usage of JS date control My97DatePicker

My97DatePicker is a very flexible and easy-to-use...

Vue+echarts realizes stacked bar chart

This article shares the specific code of Vue+echa...

Example code for configuring monitoring items and aggregated graphics in Zabbix

1. Install Zabbix Agent to monitor the local mach...

Detailed explanation of MySQL 8.0.18 commands

Open the folder C:\web\mysql-8.0.11 that you just...

JavaScript to achieve full or reverse selection effect in form

This article shares the specific code of JavaScri...

Put frameset in body through iframe

Because frameset and body are on the same level, y...

MySQL index for beginners

Preface Since the most important data structure i...

A brief analysis of the game kimono memo problem

Today, after the game was restarted, I found that...

Differences in the hr separator between browsers

When making a web page, you sometimes use a dividi...

Four ways to switch tab pages in VUE

Table of contents 1. Static implementation method...

WML tag summary

Structure related tags ---------------------------...

Solution to transparent font problem after turning on ClearType in IE

The solution to the transparent font problem after...

How to configure multiple projects with the same domain name in Nginx

There are two ways to configure multiple projects...