JavaWeb-1-Tomcat server deployment Servlet program example-basic

table of Contents

1. Introduction to Servlet

2. Manually implement the Servlet program

2.1 The steps of developing a Servlet program are as follows

3. Project structure diagram: Tomcat server deployment Servlet program project structure diagram

4. Start writing Serlvet processor

4-1. Write the first Servlet handler-HelloServlet implents Servlet

4-2, write a second Servlet handler-HttpServletProcessor extends HttpServlet

4-3 Support automatic and fast generation of a Servlet processor in IDEA (simplified 4-1 and 4-2)

4-4. Configure the servlet processor just written in the web.xml configuration file

4-5. Write a front-end submit button to test whether the Servlet processor is working properly

4-6, test results display

4.6.1 The Tomcat server started successfully: the war package was successfully deployed

4.6.2 The browser sends an HTTP request

4.6.3 Tomcat server responds

5. Web project structure analysis-understanding and mastering

5.1 Introduction to the entire dynamic Web project catalog (dynamic resources-Servlet processor; static resources-jpg, etc.)

5.2 The overall process from HTTP request to HTTP response of B/S architecture-1

5.3 The overall process from HTTP request to HTTP response in B/S architecture-2

5.4 Servlet program access principle: How does the HTTP request access a certain servlet processor in the Tomcat server

5.5 Extra content-WebFlux framework in Spring5

6. Common mistakes of Servlet program

6.0 Standard configuration of web.xml file

6.1 web.xml file: The path configured in url-pattern does not start with a slash\

6.2 web.xml file: the two mapping values ​​in servlet-name are inconsistent

6.3 web.xml file: The full class name of the servlet-class tag is incorrectly configured

7, Servlet life cycle

Reference learning video

1. Introduction to Servlet

(1) Servlet is one of the JavaEE specifications (interface).

(2) Servlet is one of the three major components of JavaWeb. The three major components are: Servlet program, Filter, Listener.

(3) Servlet is a java program running on the server, it can receive the request from the client, and respond to the data to the client to analyze and display.

2. Manually implement the Servlet program

(1) Write a class to implement the Servlet interface and rewrite the service() method in the Servlet interface. The service() method is used to process the request sent by the client and respond to the data.

(2) Configure the access address of the Servlet program in the web.xml file in the WEB-INF folder.

2.1 The steps of developing a Servlet program are as follows

3. Project structure diagram: Tomcat server deployment Servlet program project structure diagram

4. Start writing Serlvet processor

4-1. Write the first Servlet handler-HelloServlet implents Servlet

(1) Create a HelloServlet class in the src directory and inherit the Servlet interface.

package com.wind.servlet;

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

/**
一个servlet程序
 */
public class HelloServlet implements Servlet {

    public HelloServlet() {
        System.out.println("1.Servlet的无参构造器执行了.");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("2.Servlet的init方法执行了.");
    }

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

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("3.Servlet的service方法执行了.");
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String method = httpServletRequest.getMethod();
        if ("GET".equals(method)) {
            this.doGet();
        }
        if ("POST".equals(method)) {
            this.doPost();
        }
    }

    private void doPost() {
        System.out.println("3.1 Servlet的service方法-doPost-执行了.");
        System.out.println("3.1 Servlet的service方法-doPost-执行了2.");
    }

    private void doGet() {
        System.out.println("3.1 Servlet的service方法-doGet-执行了.");
        System.out.println("3.1 Servlet的service方法-doGet-执行了2.");
    }

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

    @Override
    public void destroy() {
        System.out.println("4.Servlet的destroy方法执行了.");
    }
}

4-2, write a second Servlet handler-HttpServletProcessor extends HttpServlet

(1) Create another HttpServletProcessor class in the src directory and inherit the HttpServlet interface.

(2) In actual projects, the HttpServlet interface is more commonly used. Note: HttpServlet interface is a sub-interface of Servlet.

package com.wind.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 通常会使用实现HttpServlet这个抽象类的方式来实现Servlet处理器
 */
public class HttpServletProcessor extends HttpServlet {

    private static final long serialVersionUID = -7511536601794982778L;

    public HttpServletProcessor() {
        System.out.println("1.HttpServletProcessor extends HttpServlet 的无参构造器执行了...");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("2.HttpServletProcessor extends HttpServlet 的init方法执行了...");
    }

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

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("3.HttpServletProcessor extends HttpServlet, doGet done...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("4.HttpServletProcessor extends HttpServlet, doPost done...");
    }

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

    @Override
    public void destroy() {
        System.out.println("5.HttpServletProcessor extends HttpServlet 的destroy方法执行了.");
    }

}

4-3 Support automatic and fast generation of a Servlet processor in IDEA (simplified 4-1 and 4-2)

(1) Create another HttpServletProcessorAuto class again in the src directory and inherit the HttpServlet interface.

4-4. Configure the servlet processor just written in the web.xml configuration file

(1) Configure in web.xml each Servlet processor deployed in the Tomcat server and its corresponding access path URL

<?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">

    <!--1.使用servlet标签给Tomcat配置动态servlet程序-->
    <servlet>
        <!--servlet-name标签:给servlet程序起一个别名(一般就是类名)-->
        <servlet-name>HelloServlet</servlet-name>
        <!--servlet-class标签:servlet程序的全类名-->
        <servlet-class>com.wind.servlet.HelloServlet</servlet-class>
    </servlet>

    <!--2.使用servlet-mapping标签给动态servlet程序配置访问地址-->
    <servlet-mapping>
        <!--servlet-name标签:告诉Tomcat服务器,我当前配置的访问地址是给哪个servlet程序使用-->
        <servlet-name>HelloServlet</servlet-name>
        <!--url-pattern标签:配置访问地址.
        (1)/        :斜杠表示,在服务器解析的时候,表示访问地址是:http://ip:port/工程路径
        (2)/hello   : 表示访问地址是:http://ip:port/工程路径/hello 比如:http://localhost:8080/javaWeb/hello -->
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <!--3.使用a.html访问-->

    <!--1.使用servlet标签给Tomcat配置动态servlet程序-->
    <servlet>
        <servlet-name>HttpServletProcessor</servlet-name>
        <servlet-class>com.wind.servlet.HttpServletProcessor</servlet-class>
    </servlet>

    <!--2.使用servlet-mapping标签给动态servlet程序配置访问地址-->
    <servlet-mapping>
        <servlet-name>HttpServletProcessor</servlet-name>
        <url-pattern>/httpServlet</url-pattern>
    </servlet-mapping>
    <!--3.使用b.html访问-->

</web-app>

4-5. Write a front-end submit button to test whether the Servlet processor is working properly

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/javaWeb/hello" method="POST">
    <!--action属性值与web.xml中的<url-pattern>标签内容一致,用于访问到servlet程序的service方法-->
    <input type="submit">
</form>
</body>
</html>

4-6, test results display

4.6.1 The Tomcat server started successfully: the war package was successfully deployed

4.6.2 The browser sends an HTTP request

4.6.3 Tomcat server responds

5. Web project structure analysis-understanding and mastering

5.1 Introduction to the entire dynamic Web project catalog (dynamic resources-Servlet processor; static resources-jpg, etc.)

5.2 The overall process from HTTP request to HTTP response of B/S architecture-1

5.3 The overall process from HTTP request to HTTP response in B/S architecture-2

5.4 Servlet program access principle: How does the HTTP request access a certain servlet processor in the Tomcat server

5.5 Extra content-WebFlux framework in Spring5

6. Common mistakes of Servlet program

6.0 Standard configuration of web.xml file

6.1 web.xml file: The path configured in url-pattern does not start with a slash\

6.2 web.xml file: the two mapping values ​​in servlet-name are inconsistent

6.3 web.xml file: The full class name of the servlet-class tag is incorrectly configured

7, Servlet life cycle

After the servlet program is accessed for the first time, it will be executed in the following order:

(1) Constructor method of executing Servlet program. (2) Execute the init() initialization method.

(3) Execute business logic, that is, service() method. (4) Execute the destroy() method.

Among them, step (1) and step (2) will be executed when the Servlet program is created for the first time (each time the service is started), step (3) will be executed every refresh (each visit), (4) Step will be executed once when the Tomcat server is stopped.

Reference learning video

(1) Full version of javaWeb learning video at station B: https://www.bilibili.com/video/BV1Y7411K7zz?p=99

(2) java8 online API: https://www.matools.com/api/java8

(3) Blog site: https://blog.csdn.net/weixin_49343190/article/details/107878144

 

Guess you like

Origin blog.csdn.net/cmm0401/article/details/111408300