Learning the mental journey of front-end web -- Introduction to web server software Tomcat and Servlet


web server software

Common concepts:

  • Server: the computer on which the server software is installed
  • Server software: receive the user's request, process the request and respond
  • web server software: receive the user's request, process the request and respond.
    • In the web server software, web projects can be deployed so that users can access these projects through a browser
    • web container
  • Common java related web server software:
    • webLogic: oracle company, a large Java EE server, supports all Java EE specifications and charges.
    • webSphere: IBM, a large Java EE server, supports all Java EE specifications and charges.
    • JBOSS: a large-scale Java EE server of JBOSS company, which supports all Java EE specifications and charges.
    • Tomcat: Apache foundation, a small and medium-sized Java EE server, only supports a small number of Java EE specification servlets / JSPS. Open source, free.

Tomcat

Baidu Encyclopedia

  Tomcat is a core project of the Jakarta project of the Apache Software Foundation, which is jointly developed by Apache, Sun and some other companies and individuals. With the participation and support of Sun, the latest Servlet and JSP specifications can always be reflected in Tomcat. Tomcat 5 supports the latest Servlet 2.4 and JSP 2.0 specifications. Because Tomcat technology is advanced, stable and free, it is loved by Java enthusiasts and has been recognized by some software developers. It has become a popular Web application server.

step

  • 1. Download: http://tomcat.apache.org/

  • 2. Installation: decompress the compressed package.
    Note: it is recommended that the installation directory should not contain Chinese and spaces

  • 3. Uninstall: just delete the directory

  • 4. Startup:
      * bin/startup.bat, double-click to run the file
       * access: Browser input: http://localhost:8080 Enter to visit yourself
                http: / / others' ip:8080 visit others

      * Possible problems:
      	1. The black window flashed past:
      		* Cause: not configured correctly JAVA_HOME environment variable
      		* Solution: correct configuration JAVA_HOME environment variable
    
      	2. Startup error:
      		1. Violence: find the occupied port number, find the corresponding process, and kill the process
      			* netstat -ano
      		2. Gentle: modify your own port number
      			* conf/server.xml
      			* <Connector port="8888" protocol="HTTP/1.1"
                     connectionTimeout="20000"
                     redirectPort="8445" />
      			* Usually tomcat Change the default port number to 80. 80 port number is http The default port number for the protocol.
      				* Benefit: when accessing, you don't have to enter the port number
    
  • 5. Close:
    1. Normal closing:
       bin/shutdown.bat
       ctrl+c
    2. Forced shutdown:
      click on the start window ×

  • 6. Configuration:
    How to deploy the project:

        1. Put items directly into webapps Directory.
      		* /hello: Access path to the project-->Virtual directory
      		* Simplify deployment: break the project into one war Bag, and then war Package placed in webapps Directory.
      			* war The package is automatically decompressed
      				
      	2. to configure conf/server.xml file
      		stay<Host>Configuration in label body
      		<Context docBase="D:\hello" path="/hehe" />
      		* docBase:Path of project storage
      		* path: Virtual directory
      	3. stay conf\Catalina\localhost Create a with any name xml File. Write in document
      		<Context docBase="D:\hello" />
      		* Virtual directory: xml Name of the file
      
      * Static and dynamic projects:
      	* directory structure
      		* java Directory structure of dynamic project:
      			-- The root directory of the project
      				-- WEB-INF catalog:
      					-- web.xml: web Core profile of the project
      					-- classes Directory: the directory where bytecode files are placed
      					-- lib Directories: placing dependent jar package
    

Servlet

  • Concept: a small program running on the server side
    • Servlet is an interface that defines the rules for Java classes to be accessed by the browser (recognized by tomcat).
    • In the future, we will customize a class to implement Servlet interface and replication method.

Quick start:

  1. Create a Java EE project
  2. Define a class to implement the Servlet interface
       public class ServletDemo1 implements Servlet
  3. Implementing abstract methods in interfaces
  4. Configure Servlet
 stay web.xml Configuration in:
	    <!--to configure Servlet -->
	    <servlet>
	        <servlet-name>demo1</servlet-name>
	        <servlet-class>cn.itcast.web.servlet.ServletDemo1</servlet-class>
	    </servlet>
	
	    <servlet-mapping>
	        <servlet-name>demo1</servlet-name>
	        <url-pattern>/demo1</url-pattern>
	    </servlet-mapping>

Execution principle:

  1. When the server receives the request from the client browser, it will parse the request URL path and obtain the resource path of the accessed Servlet
  2. Find web XML file, whether there is corresponding label body content.
  3. If yes, find the corresponding full class name in the
  4. tomcat loads the bytecode file into memory and creates its object
  5. Call its method

Life cycle method in Servlet

1. Created: executed init Method, executed only once
	* Servlet When was it created?
		* By default, when accessed for the first time, Servlet Created
		* Execution can be configured Servlet When to create.
			* stay<servlet>Configuration under label
				1. When first accessed, create
            		* <load-on-startup>The value of is negative
	            2. When the server starts, create
	                * <load-on-startup>The value of is 0 or a positive integer
	* Servlet of init Method, executed only once, describing a Servlet There is only one object in memory, Servlet It's a singleton
		* When multiple users access at the same time, there may be thread safety problems.
		* Solution: try not to Servlet Member variables are defined in. Do not modify the value of a member variable even if it is defined
2. Provision of services: Implementation service Method, execute multiple times
	* Every visit Servlet When, Service Methods are called once.
3. Destroyed: executed destroy Method, executed only once
	* Servlet Executed when destroyed. When the server shuts down, Servlet Destroyed
	* It will be executed only when the server is shut down normally destroy method.
	* destroy Method in Servlet Before being destroyed, it is generally used to release resources

Keywords: Java Front-end Tomcat

Added by tams on Mon, 10 Jan 2022 10:11:55 +0200