Servlet learning notes

Posted by ramonekalsaw on Sun, 16 Jan 2022 09:20:50 +0100

Servlet learning notes

1. Introduction to Servlet

Servlet (Server Applet) is the abbreviation of Java Servlet, which is called small service program or service connector. The server-side program written in Java is independent of platform and protocol. Its main function is to interactively browse and generate data and generate dynamic Web content. Servlet in a narrow sense refers to an interface implemented in Java language, A generalized servlet refers to any class that implements the servlet interface.

Developing a Servlet program requires two steps:

  • Write a class to implement the Servlet interface
  • Deploy the written Java classes to the Web server

2. HelloServlet

  1. Create a normal Maven project and delete the src folder as the Maven parent project

  2. Maven and son project

    POM of parent project XML will have

    <modules>
        <module>servlet-01</module>
    </modules>
    

    POM of subproject XML will have

    <parent>
        <artifactId>javaweb-02-servlet</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    

    The child project can directly use the jar package of the parent project.

  3. Maven environment optimization

    3.1 modify web XML is up to date

    3.2 complete Maven's structure

    <?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"
            metadata-complete="true">
    </web-app>
    
  4. Write a Servlet

    4.1 create a common class

    4.2 implementation of Servlet interface

    Inheritance relationship: httpservlet - > genericservlet - > Servlet

    Among them, both Servlet and GenericServlet only define the service method and do not implement it. They are only implemented in HttpServlet. The specific implementation is to call different methods according to different parameters, such as "GET" calling doGet, "POST" calling doPost, etc.

    Therefore, after inheriting HttpServlet, our class should rewrite doGet and other methods to achieve the desired effect.

    Tips: Ctrl + o = > select methods to override / implement quick override method

    Alt + enter = > introduce local variable quick declare variable

    public class HelloServlet extends HttpServlet {
    
        // doGet and doPost are only different ways to implement requests. They can call each other, and the business logic is the same
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            PrintWriter writer = resp.getWriter();// Response flow
            writer.print("Hello,Servlet");
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req, resp);
        }
    }
    
  5. Write the mapping of servlets

    Why mapping is needed: the written Java program needs to be accessed through the browser. The browser needs to connect to the Web server, so it needs to register the Servlet in the Web server and give it a path that the browser can access.

    <servlet>
        <!--register Servlet-->
        <servlet-name>hello</servlet-name>
        <servlet-class>com.qiyuan.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <!--Request path-->
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    
  6. Configure Tomcat

    Port number, JRE, etc

Add Artifact

The application context is the access path.

  1. Start the server

    Path: localhost:8080/s1/hello

    Start Cheng Kun!

3. Servlet principle

4. Mapping

Now they are basically annotations, but we still need to understand.

  1. A Servlet can specify a mapping path

    <servlet>
        <!--register Servlet-->
        <servlet-name>hello</servlet-name>
        <servlet-class>com.qiyuan.servlet.HelloServlet</servlet-class>
    </servlet>
    <!--Request path-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    
  2. Servlet s can specify multiple mapping paths

    <!--Request path-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello2</url-pattern>
    </servlet-mapping>
    

    Because the name corresponds to, they all point to one.

  3. Servlet can specify a general mapping path, and the * sign is a wildcard

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello/*</url-pattern>
    </servlet-mapping>
    
  4. You can customize the suffix to implement the request, * cannot be preceded by a path

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.qiyuan</url-pattern>
    </servlet-mapping>
    

    With Requests with qiyuan as the suffix will jump to the servlet mapped to hello.

  5. priority

    The mapping of the specified path has higher priority than the generic mapping,

    <servlet>
        <!--register Servlet-->
        <servlet-name>error</servlet-name>
        <servlet-class>com.qiyuan.servlet.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>error</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    If you visit localhost:8080/s1/hello/suibianxie, you will still enter the Servlet of Hello.

Not finished.

Topics: servlet