A new round of learning - Jetty official documentation and source code reading (1)

一 java servlet

1 Wikipedia defines it as follows: A Java Servlet is a Java program that contains server functions. Although servlets can respond to any type of request, most of them are usually implemented as applications built on a web server. Just such web servlets are java implementations of dynamic web content like PHP and ASP.NET.

2 What are the defects of traditional CGI scripts:

(1) Each HTTP request consumes a process, and the CPU usage is too high

(2) For concurrent CGI requests, each request must load the CGI script into the memory, and the memory usage is too large

(3) Only one singleton responds to concurrent requests. This reduces memory usage and makes it difficult to manage persistent data

3 servlet life cycle

init(): Initializes the servlet instance, implements the javax.servlet.ServletConfig interface, and allows the servlet to get the name-value initialization parameters from the web application configuration.

service(): Called on every request. Determine the request type and dispatch it to an appropriate method to handle.

destroy(): Called when the service ends, included in the servlet life cycle.

Two Jetty

The latest version: jetty 9.4.8v20171121

Official documentation:

1 Getting started with Jetty

a. Introduction to jetty

(1) maven information:

pom:

<dependency>

  <groupId>org.eclipse.jetty</groupId>

  <artifactId>jetty-project</artifactId>

  <version>${project.version}</version>

</dependency>

(2) First popularize the knowledge of the next version code:

主要有Alpha、Beta、Milestones(M)、Release Candidates(RC)、General Availability(GA)。

Introduce RC and GA, that is, the candidate version and available version before release. Generally, RC cannot be used in production environment, and it may contain some security vulnerabilities or incomplete feature sets.

GA is the available online version, and is also divided into Release and Stable, that is, the first version and the stable version are released. After the user test is passed after the release, there will be some issues raised during the test that need to be fixed. The stable version is the stable version released after the first version is used and tested for a certain period of time, and the bugs are fixed. It is generally recommended to use this version (if it exists).

The following table is the package path and purpose on the official website, please refer to it, no explanation is given here:

The demo-base path in the above table is the root directory for jetty service startup and resource storage

The startup command is:

>cd $JETTY_HOME/demo-base/

>java –jar $JETTY_HOME/start.jar

(JETTY_HOME=/path/to/$(jetty_home) has been set before)

The official website recommends not to start in the $JETTY_HOME directory. Visit http://localhost:8080 in your browser to view the demo server.

Create a new jetty base to start the service:

>JETTY_BASE=/tmp/mybase

>mkdir $JETTY_BASE

>cd $JETTY_BASE

>java –jar $JETTY_HOME/start.jar --create-startd

>java –jar $JETTY_HOME/start.jar --add-to-start=http,deploy

>cp $JETTY_HOME/demo-base/webapps/async-rest.war webapps/ROOT.war

>java –jar $JETTY_HOME/start.jar

(3) Introduction to configuration

Several ways: POJO, startup configuration file (it is divided into ini, mod, xml, no detailed explanation here, please refer to the official website), other configuration files, Jetty IoC XML configuration

The relationship between ini, mod, and xml is as follows:

Other configuration files include context xml, web.xml, and Properties files.

b. Configuration Introduction

Configure Server

Core server configuration: etc/jetty.xml

包括ThreadPool,Handlers,Server Attributes,Server fields,Connectors,Services

ThreadPool: The default Executor Service used by other Jetty server components. The basic configuration is the maximum size and minimum size in start.ini and start.d/server.ini

Handlers: used to process requests, it will be configured as a tree in xml, and each node represents a function point.

Server Attributes: Contains an inherited attribute map. If the value object implements the LifeCycle interface, they will be started and stopped together with the server.

Server fields: pass attributes in the http response

Connectors: Connectors for some protocols (such as HTTP) supported by jetty. In the server, you can specify a set of connectors and add/delete connectors.

Services: Typically LoginService and DataSources

Configure Connectors

包括Port,Host,Idle Timeout,HTTP Configuration,SSL Context Factory

Port: set by jetty.http.port or jetty.ssl.port, the default http is 8080, https and http2 are 8443

Host: If not set, the default is 0.0.0.0. xml is configured as the jetty.host property.

Idle Timeout: idle time

HTTP Configuration: An instance of HttpConfiguration that contains a generic HTTP configuration independent of specific wireless protocols. In the standard version, an HttpConfiguration is created in jetty.xml

SSL Context Factory: locate keystore and truststore for authentication.

ServerConnector: NIO-based and uses a connection factory to handle requests for one or more protocols.

Configure Contexts

Roughly equivalent to the servletContext API, used to assemble handlers and load resource files in a context path. Contains standard jetty processors and custom application processors.

Common include contextPath, virtualHost, classPath, attributes, resourceBase

Focus on classPath, a context can only contain one classpath, so any thread that executes processing contains a classLoader of thread context. Standard web applications are initialized through the WEB-INF/lib and WEB-INF/classes directories, and some rules are also required to proxy the loading of the parent classloader.

Release of Web Application

Multiple formats can be published: war, extended war, static content, xml containing ContextHandler instances.

When there are multiple publishable formats in the same directory, the priority is: XML>WAR>Dir

(Special reminder: ContextDeployer is no longer supported in Jetty9, functionality has been merged into the new WebAppProvider to avoid double-publish semantics.

Guess you like

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