[Jsp&Servlet] 1-1 creation, configuration, and problems encountered

1. IDEA creates Servlet project

①Create a Java Enterprise project and use Maven as the build tool

② Select Web Profile for libraries and frameworks

③Set path, name, etc.

The created new project structure is as shown in the figure:

2. Implement Servlet interface

For example, create a new Java class ServletDemo1:

package com.example ...;

public class ServletDemo1 {
}

Implement the Servlet interface and use the prompt to add the five methods of the Servlet:

package com.example ... ;

import javax.servlet.*;
import java.io.IOException;

public class ServletDemo1 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

3. Two methods of configuring Servlet

Method 1: Configure web.xml file

Add the following in the tag of the folder webapp/WEB-INF/web.xml in the project:

 <servlet>
        <servlet-name>demo2</servlet-name> <!--servlet name (customized by yourself)-->
        <servlet-class>com.example.ServletBase... </servlet-class> <!--The address of the Java class in the project-->
    </servlet>
    <servlet-mapping>
        <servlet-name>demo2</servlet-name> <!--The name of the servlet, the same as the --> in the servlet tag
        <url-pattern>/demo2</url-pattern> <!--Access path of the servlet-->
    </servlet-mapping>

Access Servlet:

This will be accessible via “localhost:8080/ServletBase_war_exploded/demo2“.

Among them, the name ServletBase_war_exploded can be changed in the following ways:

Run–>Edit Configuration

Select your Tomcat and click Deploy.

Select the Application Context below and change it to the desired name.

tips:

You can add the tag to the servlet tag and add numbers in it. When this value is greater than or equal to 0, the servlet is created when the server starts; when this value is less than 0, it is created when it is accessed for the first time.

Method 2: Use WebServlet annotation

package com.example.ServletBase;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet(urlPatterns = "/demo3")

public class ServletDemo3 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("Init ... ");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Service ... ");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("Destroy ... ");
    }
}

As shown in the seventh line of the above code, @WebServlet(urlPatterns = “/demo3”) is added, so that it can be accessed through “localhost:8080/ServletBase_war_exploded/demo3“. (As mentioned above, the middle ServletBase_war_exploded can be changed)

Advantages and points to note when using annotations
Using the WebServlet annotation, you can access the servlet without writing a web.xml file.
Most of the WebServlet configurations are default. I only need to configure urlPatterns, as shown in the above code.

Of course, it can also be written as (value = "/demo3") or directly ("/demo3"), and there is no problem.
ah? Why, you ask?
(1) Obviously, value saves the most important content in WebServlet, and obviously, urlPatterns is the most important content, so it is allowed to be written like this;
(2) When only the value is written but not the variable, the value is automatically assigned to value. 

4. Problems encountered and solutions

Tomcat log garbled phenomenon

Garbled characters are generally caused by different encoding formats of files, but I don’t know where the configuration file of Tomcat log is. Finally, I referred to this article:

(If you have a lot of experience, you should be able to find it, haha, I have to work hard)

When running Tomcat – Qi℃伅 (solved)_tomcat information_God_The Blog of Dreams as Horses-CSDN Blogicon-default.png?t=N7T8https ://blog.csdn.net/God_stf/article/details/122500542

Before solving:

After solving:

The red color is not because of the error, but because these logs are this color hahaha~