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

Blog    

Recommend

Docker installs Elasticsearch7.6 cluster and sets password

Starting from Elasticsearch 6.8, free users are a...

CSS3 animation – steps function explained

When I was looking at some CSS3 animation source ...

The difference between Display, Visibility, Opacity, rgba and z-index: -1 in CSS

We often need to control the hidden, transparent ...

Solution to MySQLSyntaxErrorException when connecting to MySQL using bitronix

Solution to MySQLSyntaxErrorException when connec...

mysql5.7.22 download process diagram

1. Go to the official website www.mysql.com and s...

How to use js to communicate between two html windows

Scenario: When page A opens page B, after operati...

Implementation code for adding slash to Vue element header

<template> <div class="app-containe...

Steps to install MySQL 5.7 in binary mode and optimize the system under Linux

This article mainly introduces the installation/st...

MySQL multi-table join query example explanation

In actual projects, there are relationships betwe...

Three Vue slots to solve parent-child component communication

Table of contents Preface Environment Preparation...

JavaScript implementation of the back to top button example

This article shares the specific code for JavaScr...

React hooks pros and cons

Table of contents Preface advantage: shortcoming:...