Section 4. Servlet Technology

Posted by stalione on Tue, 21 Dec 2021 19:21:33 +0100

Servlet, features and lifecycle

Servlet is a server-side application written by Java classes. It is usually a program running on the server side. You can call one by opening the browser. It can be regarded as an intermediate layer between the client and the server, which is responsible for receiving and requesting the response of the client user.
Servlet can provide the following functions:
(1) Read and intercept the data sent by the client
(2) Read implicit data requested by the client
(3) Run results or generate results
(4) Send response data

characteristic:
High efficiency, simple and convenient, poor readability and difficult to maintain.
Servlet lifecycle:

The init() method and destroy() method are also called only once throughout the life cycle. When the service() method is rewritten, the doPost and doGet methods will not be processed, and the service() will manage them and turn to the corresponding methods.

Writing and deploying servlets

Create a Java project and deploy the Web module. src stores the source code Package. Create a new Package under src and name it com yxtech. Then create a Servlet class under the Package_ FirstServlet needs to inherit from HttpServlet class so that specific functions can be developed.

Now realize the cycle of outputting 0 ~ 10 in the page:
Because Java16 does not have Javax, you need to import the external library Tomcat 10.1 0-M4.


This contains all the files to be imported.

package com.yxtech;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

//Configuring servlets by injection
@WebServlet(
        urlPatterns = { "/FirstServlet " },
        name = " firstServlet "
)
public class FirstServlet extends HttpServlet {
    //Serialization, which can be automatically generated and self-defined
    private static final long serialVersionUID = 1L;

    public void init() throws ServletException {
        System.out.println("initialization init()");
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException,IOException
    {
        System.out.println("call doGet method");
        //Set the corresponding page category and page code
        response.setContentType("text/html;charset=gbk");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Test cycle results from 0 to 10</title>");
        out.println("<body>");
        out.println("Start execution......");
        int count=0;
        for(int i=0;i<=10;i++){
            count+=i;
        }
        out.println("Program execution results:"+count);
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
            throws ServletException,IOException{
        doGet(request,response);
    }
    public void destory(){
        System.out.println("call destory()method");
    }
}


Deploy Tomcat:


To create a Servlet:
1. Introduce the corresponding package. This is Tomcat 10.1 Imported from 0-m4. FirstServlet has two request processing methods: one is the doGet() method, which responds to the HTTP Get request; The other is the doPost() method, which responds to HTTP Post requests.
2. Create an extension class, in this case FirstServlet.
3. Refactor doPost() or doGet(). In this method, the request is completed and output to the HTML page.
4. Annotations can also be used to configure the Web.
5. Access the corresponding extension class.

Differences between Servlet and JSP:
(1) Servlet s are Java code and JSPS are page code (HTML and JSP expressions)
(2) Servlet is running faster than JSP
(3) Servlet s need to be compiled manually, and JSP S are compiled automatically by the server
(4) The edit HTML tool does not support editing servlets

Most servlets are responsible for processing client requests and calling JavaBeans; JavaBeans are responsible for providing reusable data and data access data. The JSP page is mainly responsible for displaying the page and displaying the dynamic data to customers

Topics: Java Tomcat servlet