15-day13 dark horse Java Web notes Tomcat&Servlet

15-day13 dark horse Java Web notes Tomcat & Servlet

Review of web related concepts

  1. Software architecture

    1. C/S: client / server
    2. B/S: Browser / server side
  2. Resource classification

    • Static resources: after all users access, the results are the same, which is called static resources. Static resources can be directly parsed by the browser

      Such as: html,css,JavaScript

    • Dynamic resources: each user may get different results after accessing the same resources. It is called dynamic resource. After dynamic resources are accessed, they need to be converted into static resources and returned to the browser
      Such as servlet/jsp,php,asp

  1. Three elements of network communication
	1. IP: Electronic equipment(computer)Unique identification in the network.
	2. Port: the unique identification of the application in the computer. 0~65536
	3. Transmission protocol: Specifies the rules for data transmission

Base agreement:

			1. tcp:Security protocol, three handshakes. The speed is a little slow
			2. udp: Unsafe protocol. Fast speed

web server software

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.

stay web In the server software, you can deploy web Projects, allowing users to access these projects through a browser

Common java related web server software:

	* webLogic: oracle Company, large JavaEE Server, supporting all JavaEE Standardized and charged.
	* webSphere: IBM Company, large JavaEE Server, supporting all JavaEE Standardized and charged.
	* JBOSS: JBOSS Corporate, large JavaEE Server, supporting all JavaEE Standardized and charged.
	* Tomcat: Apache IMF, small and medium-sized JavaEE Server, only a small number of JavaEE standard servlet/jsp. Open source, free.

Java EE: the sum of the technical specifications used by the Java language in enterprise development, which specifies a total of 13 major specifications

Tomcat: web server software

1. Download: http://tomcat.apache.org/
2. Installation: unzip the compressed package.
 * Note: it is recommended that the installation directory should not contain Chinese and spaces
3. Uninstall: just delete the directory

1. start-up
	1. bin/startup.bat ,Double click to run the file
	2. Access: Browser input: http://localhost:8080 enter to access yourself

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
  1. close:
		1. Normal shutdown:
			* bin/shutdown.bat
			* ctrl+c
		2. Forced shutdown:
			* Click on the start window×
  1. to configure:

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
		* take Tomcat Integrated into IDEA And create JavaEE Project, deployment project.

Hot deployment

Servlet: server applet

Concept:

Applet 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
      

    Take the mac as an example. If the import prompts that the Servlet package does not exist, copy the servlet-api.jar under the tomcat lib directory to the Lib file directory under the idea project and add it.

  3. Implementing abstract methods in interfaces

  4. Configure the Servlet in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>demo1</servlet-name>
        <servlet-class>TomcatServletDemo.TestServletDemo1</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>demo1</servlet-name>
        <url-pattern>/demo1</url-pattern>
    </servlet-mapping>
</web-app>

Execution principle:

	1. When the server receives the request from the client browser, it will parse the request URL Path to get access Servlet Resource path for
	2. lookup web.xml Is there a corresponding file<url-pattern>Label body content.
	3. If so, find the corresponding<servlet-class>Full class name
	4. tomcat The bytecode file is loaded into memory and its object is created
	5. Call its method

Lifecycle methods in servlets:

  1. Created: execute the init method only once

    1. Servlet When was it created?
      	1. By default, when accessed for the first time, Servlet Created
      	2. Execution can be configured Servlet When to create.
           - stay\<servlet>Configuration under label
              - When first accessed, create\<load-on-startup>The value of is negative
              - When the server starts, create\<load-on-startup>The value of is 0 or a positive integer
      	3. 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. Provide service: execute the service method multiple times

    1. Every visit Servlet When, Service Methods are called once.
      
  3. Destroyed: execute the destroy method only once

    1. 		* 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
      

Servlet3.0

Benefits:

Annotation configuration is supported. You don't need web.xml anymore.

Steps:

1. establish JavaEE Projects, selecting Servlet Version 3 of.0 Above, you can not create web.xml
2. Define a class to implement Servlet Interface
3. Replication method
4. Use on class@WebServlet Annotation, configuration
@WebServlet("Resource path")
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {
    String name() default "";//Equivalent to < servlet name >

    String[] value() default {};//Represents the urlPatterns() property configuration

    String[] urlPatterns() default {};//Equivalent to < URL pattern >

    int loadOnStartup() default -1;//Equivalent to < load on startup >

    WebInitParam[] initParams() default {};

    boolean asyncSupported() default false;

    String smallIcon() default "";

    String largeIcon() default "";

    String description() default "";

    String displayName() default "";
}

Practice @ WebServlet annotation

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/demo1")
public class Test implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }


    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("hello");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

Keywords: Java Tomcat

Added by britey on Sun, 12 Sep 2021 21:11:03 +0300