[JavaWeb] Servlet

1. Introduction to Servlet

 1.1 What is Servlet (server applet): It is a server-side Java application program independent of platform and protocol, which can generate dynamic web pages through Servlet. The client's request can also be processed on the server side to control the execution of the program.

  Function: Interactively browse and update data, and generate dynamic page content display.

  Servlet processing web request process:

  

 

  1. The server accepts the request sent from the client
  2. The server sends the request information to the servlet
  3. After the servlet is processed, the content of the response is generated
  4. The server returns the content of the response to the client

  Jsp and Servlet relationship: When Jsp is parsed by the web container, it will eventually be compiled into a Servlet class  

1.2 Servlet API

name illustrate Package names
Servlet interface The basic interface of Java Servlet, which defines the methods that the class Servlet must implement javax.srevlet
GenericServlet class Inherited from the Servlet interface, it is a general-purpose, protocol-independent Servlet javax.srevlet
HttpServlet class Inherited from the GenericServlet class, it is a Servlet that extends the HTTP protocol on its basis javax.srevlet.http
HttpServletRequest interface Inherited from the ServletRequest interface, used to obtain the reading of request data javax.srevlet.http
HttpServletResponse interface Inherited from the ServletResponse interface for returning response data javax.srevlet.http

1.3 Servlet life cycle

  Servlet life cycle: the process of servlet from creation to destruction.

  Servlet container: A container used to load servlet objects, which is a type of component responsible for managing servlets.

  It includes the following parts:

  1. Load and instantiate.         

    The servlet container is responsible for loading and instantiating the servlet. When the client sends a request, the container checks whether there is a corresponding servlet instance, creates one if not, and takes out the instance to respond to the request

  2. Initialization.         

     After the container completes the instantiation of the servlet, the container will call the init() method of the servlet to initialize the servlet. The purpose of initialization is to allow the servlet object to complete some initialization work before processing the client request, such as setting database connection parameters, establishing a JDBC connection, or establishing a connection to the servlet. References to other resources. The init() method is defined in the java.servlet.Servlet interface. (For each Servlet instance, the init() method is called only once)

  3. Provide services and request processing.

         After a servlet is initialized, it is in a ready state to respond to requests. When the servlet container receives a client request, it calls the service() method of the servlet to process the client request. The servlet instance obtains the client's request through the ServletRequest object. Set the response information through the methods of the ServletResponse object.

  4. Destruction.

    When the container determines whether a servlet should be released (the container is closed or needs to recycle resources), the container will call the destroy() method of the servlet. The destroy() method knows which resources can be reclaimed by the system, instead of being reclaimed directly by the destroy() method.

2. Servlet application

 2.1 Three ways to create Servlet

  • Implement the Servlet interface
  • Inherit the GenericServlet class
  • Inherit HttpServlet class
package myproject;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet{
    protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        System.out.println( "Call the doGet method" );
    }
    
    protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        System.out.println( "Call doPost method" );
    }
    
    public void destroy(){
        System.out.println( "Servlet was destroyed" );
    }
    
    public void init(ServletConfig config)throws ServletException{
        System.out.println( "Servlet initialization" );
        String initParam = config.getInitParameter("charSetConfig");
        System.out.println(initParam);
    }
}

 

2.2 Deployment and operation of Servlet

  Deploying servlets requires configuring the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>myproject</display-name>
  
  <servlet>
      <servlet-name>myServlet</servlet-name>
      <servlet-class>myproject.MyServlet</servlet-class>
      <init-param>
          <param-name>charSetContent</param-name>
          <param-value>utf-8</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
     <servlet-name>myServlet</servlet-name>
     <url-pattern>/test</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
  
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

The commonly used <url-pattern> settings in web.xml have 3 forms

  • <url-pattern>/xxx</url-pattern>. Exact match, for example: <url-pattern>/helloserlvet</url-pattern>
  • <url-pattern>/xxx/*</url-pattern>. Match the path, if there is no exact match, the request to the /xxx/ path will be processed by the servlet, for example: <url-pattern> /helloservlet/*</url-pattern>
  • <url-pattern>*.do</url-pattern>. If there is no exact match and path match, all requests for the .do extension will be handled by this servlet

Run: browser input http://localhost:8080/myproject/test

console:

 

 

 

 

 

 

 

   

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643504&siteId=291194637