I. Web basics

1. Software architecture

  1. C/S: client/server
  2. B/S: browser/server

2. Classify Web resources

  1. Static resourceEach user responds to a request with the same resource, which can be directly parsed by the browser, for exampleHTML, CSS, javascriptEtc.
  2. Dynamic resource: Different users may respond with different resources. You need to convert the resources to static resources and then return them to the browserServlet/JspEtc.

3. Three elements of network communication

  1. IP: unique identification of a computer on the Internet
  2. Port: the unique identifier of an application on the Internet. The value ranges from 0 to 65536
  3. Transfer protocol: Rules that different computers must follow when transferring data
    1. TCP: a security protocol with a slow three-way handshake
    2. UDP: an insecure protocol with high speed

4. Web server software

  • Server software: software that can receive requests, process requests, and generate responses
  • Web server software: software that can receive requests, process requests, and generate responses; You can also deploy Web projects that users can access through a browser, such as Tomcat
  • Server: A computer on which the server software is installed

2. The Servlet

1. The concept

  • A Server applet is a small program that runs on the Server and can be accessed through a browser

2. Execution principle

  1. After receiving the request from the client browser, the server resolves the URL path of the request and obtains the resource path of the accessed servlet
  2. Tomcat lookupweb.xmlFile, is there a correspondingurl-patternLabel body content
  3. If so, according toservlet-nameFind the correspondingservlet-classFull name of the class
  4. Load the bytecode file of the corresponding class into memory
  5. Creates its object and executes the corresponding method

3. Lifecycle methods in servlets

  1. void init(ServletConfig var1): Executes this method to create an object when a servlet is accessed for the first time, only once, indicating that a servlet has only one object in memory.loadOnStartupDefaults to -1, indicating that the servlet creates its object the first time it is accessed; If you change its value to 0 or a positive integer, this method creates its object when the server starts.
  2. void service(ServletRequest var1, ServletResponse var2): a method that provides a service and is executed each time a servlet is accessed.
  3. void destroy()Execute this method once to release resources before the servlet is destroyed while the server is down properly.

4.Servlet architecture

  1. Servlet(interface)
    1. void init(ServletConfig var1)
    2. void service(ServletRequest var1, ServletResponse var2)
    3. void destroy()
  2. GenericServlet (Abstract class) : The Service method in the Servlet interface is treated as an abstract method with empty implementations of the other methods
  3. HttpServlet (Abstract class: restricted instantiation) : The Http protocol is encapsulated, and the Servlet defined by itself can inherit HttpServlet

5. Java project directory structure in IDEA

  • The root directory of the project
    • SRC directory: Store the source files of the code written by yourself
    • A web directory: Directory of the deployed Web application
      • A WEB directory - INF
        • Classes directory: Places the bytecode file
        • Lib directory: Places dependent JAR packages
        • web.xml: the core configuration file for a Web project
  • Workspace project: D: Learning Materials subject Materials JAVA IdeaProjects test2
  • Tomcat deployed Web projects: D:\ study materials \ subject materials \JAVA\IdeaProjects\test2\out\artifacts\test2_war_exploded corresponds to all resources in the Web directory under the workspace project

  • Java files in the SRC directory of the IDEA project are compiled and stored in the Tomcat deployed Web project\WEB-INF\classesdirectory
  • Resources in the WEB-INF directory cannot be directly accessed by browsers