This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

Introduction: Functions of Servlet:

  • Servlets are interfaces that are part of the JavaEE specification. Interfaces serve as specifications.
  • Servlets are one of the three components of JavaWeb. The three components are Servlet program, Filter Filter, Listener Listener.
  • A Servlet is a Java applet running on a server that can receive requests from the client and respond with data to the client.
  • That is, the Servlet interface implementation class waits for the Tomcat server to call the methods in the class, which applies to Java polymorphism.

The role of the ServletConfig interface

Knowledge:

  • The implementation class of the ServletConfig interface is the configuration information class of the Servlet program.
  • The Servlet program and ServletConfig object are created by Tomcat and used by us.
  • Servlet programs are created for the first time by default, and a ServletConfig object is created for each Servlet program created.

Function:

  • You can get the value of the alias servlet-name for the Servlet program
  • Gets the initialization parameter init-param
  • Get the ServletContext object

As follows:

Ps: How can both methods get objects from the ServletContext class

This is becausegetServletContext()The method itself is calledconfig.getServletContext(), we can click ingetServletContext()Method body, as follows:

The web.xml content is as follows:Output result:

The ServletContext interface

Knowledge:

  • ServletContext represents the Servlet context object
  • A Web project has only one instance of the ServletContext object.
  • The ServletContext object is a domain object. Domain objects, which can access data like maps, are called domain objects. By domain, I mean the scope of operations that access data, the entire Web project.
  • The implementation class for the ServletContext interface is created by Tomcat when the Web project deployment is started. Destroy it when the Web project stops.

Function:

  • Gets the context parameter context-param configured in web.xml
  • Gets the current project path in the format of/Project path
  • Obtain the absolute path on the hard disk of the server after project deployment
  • Access data like a Map
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletContext servletContext = getServletContext();

    // Get the context parameter context-param configured in web.xml
    System.out.println(servletContext.getInitParameter("contextName01"));
    // Get the current project path in the format of/project path
    System.out.println(servletContext.getContextPath());
    // Obtain the absolute path on the hard disk after the project is deployed
    System.out.println(servletContext.getRealPath("/"));

    // Access data like Map
    servletContext.setAttribute("key1"."value1");
    System.out.println(servletContext.getAttribute("key1"));
}
Copy the code

Web. XML: