Crazy God said - JavaWeb servlet learning notes

1. Introduction to Servlet

Servlet is a technology for sun company to develop dynamic web
Sun provides an interface called Servlet in these API s. If you want to develop a Servlet program, you only need to complete two small steps:
1. Write a class to implement the Servlet interface.
2. Deploy the developed java classes to the web server.
The Java program that implements the Servlet interface is called Servlet

2.HelloServlet

1. Build a normal maven project, delete the src folder, and create a new Module
2. Understanding of Maven father son project:
pom.xml
The parent project will have:

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

There will be:

<parent>
  <artifactId>servlet</artifactId>
  <groupId>com.jinmp</groupId>
  <version>1.0-SNAPSHOT</version>
</parent>

jar subprojects in the parent project can be used directly

3.Maven environment optimization

1. Modify web.xml to the latest

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

2. Complete the structure of maven, and add java and resources folders under the main folder, as shown in the following figure:

4. Write a Servlet program

1. Create a Package named com.jinmp.servlet under the java folder, and then create a common class

2. Implement the Servlet interface. Here we directly inherit HttpServlet

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
    //Because get or post are only different ways of request implementation, they can call each other, and the business logic is the same
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        PrintWriter writer=resp.getWriter();//Response flow
        writer.print("Hello,Servlet");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

3. Write Servlet mapping
Why mapping is needed: we write a JAVA program, but we need to access it through the browser, and the browser needs to connect to the web server, so we need to register the Servlet we write in the web service and give it a path that the browser can access
Configure in web.xml:

<!--register Servlet-->
    <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.jinmp.servlet.HelloServlet</servlet-class>
</servlet>
    <!--Servlet Request path for-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

4. Configure Tomcat
Note: configure the path for project publishing
5. Start test
6. Existing problems and precautions:
In a java EE project, a Tomcat 10 server was deployed. During the running process, it was found that the projects in webapp could run normally, but the servlet files written did not respond. After repeated inspection, it was confirmed that the code was OK.
Cause analysis

After checking the data, we found that the package name in Tomcat 10 was changed from the previous version of javax to jakarta, and maven still used the original javax when guiding the package, resulting in the mismatch between the servlet interface of Tomcat 10 server and the servlet file I wrote, so it can not run normally.
resolvent:
The first is to reduce tomcat to a lower version
Second:

Import the jakarta.servlet-api corresponding to Tomcat 10 in maven
Open the pom.xml file in the project, find the javax.servlet-api in, and comment or delete it

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

Then add a new Jakarta. Servlet API

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>4.0.4</version>
    <scope>provided</scope>
</dependency>

Then click Load Maven Changes to update maven

7.Servlet principle
Servlet is called by the web server. After receiving the browser request, the web server will:
8.Mapping problem
1. A Servlet can specify a mapping path

<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>

2. A Servlet can specify multiple mapping paths

<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello1</url-pattern>
</servlet-mapping>

3. A Servlet can specify a common mapping path

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

4. Default request path

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

5. Specify some suffixes or prefixes......

<!--You can customize the suffix to implement request mapping-->
<!--Note:*A path that cannot be preceded by an item mapping, such as:/hello/*.do-->
<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

6. Priority issues
The inherent mapping path is specified with the highest priority. If it is not found, the default processing request will be taken
This is the default processing request 404

<servlet>
    <servlet-name>error</servlet-name>
    <servlet-class>com.jinmp.servlet.ErrorServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>error</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Keywords: Java

Added by desoto0311 on Tue, 16 Nov 2021 23:29:59 +0200