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

Jetty Configuration Guide

(1) Publish the project to Jetty

Anatomy of a Web Application

A web application can put all files into a directory tree and bundle it into a War distribution.

/WEB-INF/: used to store resources that users do not want to access directly through the browser

/WEB-INF/web.xml: where publishers define various web application behaviors and parameters

/WEB-INF/classes/: Compiled java class files

/WEB-INF/lib/:jar package storage directory

WebAppClassloader will first load files from /WEB-INF/classes and then from /WEB-INF/lib/.

Automate Web Applications

Most techniques for deploying web applications revolve around placing a war or war's unpacked files in the ${jetty.base}/webapps/ directory so that Jetty's deployment scanner will detect them and publish them to the Context path with the same name.

Configure a specific web application to publish the project

The jetty publish description file **.xml publishes Jetty by creating a ContextHandler instance

By default, Jetty looks for xml files in the $JETTY_HOME/webapps directory.

This xml file will configure the WebAppContext class, which has two nodes, war and contextPath.

Both SystemProperty and Property elements are fine. for example:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

 

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <Set name="contextPath">/wiki</Set>

  <Set name="war">/opt/myapp/myapp.war</Set>

</Configure>

Use SystemProperty to specify myapp.home=/opt/myapp, then the above file becomes the following:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <Set name="contextPath">/wiki</Set>

 <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set>

</Configure>

 

See the benefits of writing this way? That is, every time the root directory changes, just re-specify the value of myapp.home, and the change cost is minimized.

Note: To ensure that your web.xml file is detected, you need to set the validateXml property to true. as follows:

<Call name="setAttribute">

<Arg>org.eclipse.jetty.webapp.validateXml</Arg>

<Arg type="Boolean">true</Arg>

</Call>

Other configuration items:

extractWAR: not allowed to unpack war

Since web.xml is loaded after the release description file, its properties will overwrite those in the release description file.

The configuration file below retrieves the servlet content and sets an initial parameter.

Here's an overlay description file using web.xml:

This feature makes it possible to add parameters or servlet mappings without breaking the war file.

The following is to configure a database connection pool while configuring webContext. If web.xml does not contain a reference to this data source, including it can overwrite the previous one.

Note: The javadoc for WebAppContext is a good source of information.

Publish WebAppContext process:

The org.eclipse.jetty.webapp.Configuration interface has many implementation classes, each of which corresponds to a specific function.

Default configuration class:

Anatomy of a configuration class:

A configuration class will go through 5 calls in the life cycle of webAppContext:

preConfigure-resource,configure-class,postConfigure-clear resource,deconfigure-undo resource,destroy-delete war

Extend container support by creating additional configurations:

Here are two examples:

a  JNDI configuration

JNDI inspects the connection resource status of the container and the environment in the web application, using two additional configurations:

These two configurations must be inserted in order before org.eclipse.jetty.webapp.JettyWebXmlConfiguration.

b  Annotation Configuration

Here only one additional configuration class is needed to provide inspection of servlet annotations:

Set configuration list:

a is configured directly in the webAppContext, as follows:

b by deployer:

If deployer is used, this list needs to be set in WebAppProvider:

To configure the publishing process for static content:

Need to use ResourceHandler

Hot deployment:

If a web application or a context descriptor is added to the directory, Jetty's Deployment (DM) publishes the new context. If the context descriptor is updated or created, the DM will go through the process of stopping, reconfiguring, and republishing. If the context is removed, the DM will stop it and remove it from the server.

The following behaviors can be controlled when configuring the WebAppProvider property:

monitoredDirName: The directory where the web application is published

scanInterval: The interval time (seconds) to check monitoredDirName. 0 means that hot deployment is not supported, and the web application can only be published at startup.

The default address is ${jetty.home}/etc/jetty-deploy.xml.

First configure to open the deploy module, and then set. See the next lecture on publishing architecture for details.

Guess you like

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