Jetty

Eclipse Jetty is a Java HTTP (Web) server and Java Servlet container. While Web Servers are usually associated with serving documents to people, Jetty is now often used for machine to machine communications, usually within larger software frameworks. Jetty is developed as a free and open source project as part of the Eclipse Foundation. The web server is used in products such as Apache ActiveMQ,[2] Alfresco,[3] ScalatraApache Geronimo,[4] Apache Maven,  Apache SparkGoogle App Engine,[5] Eclipse,[6] FUSE,[7] iDempiere,[8] Twitter's Streaming API[9] and Zimbra.[10] Jetty is also the server in open source projects such as LiftEucalyptusOpenNMSRed5Hadoop and I2P.[11] Jetty supports the latest Java Servlet API (with JSP support) as well as protocols HTTP/2 and WebSocket.

官网链接直达下载:

https://www.eclipse.org/jetty/download.html

Start with Document:

https://www.eclipse.org/jetty/documentation/9.4.x/introduction.html#what-is-jetty

0.设置自己的JETTY_BASE、JETTY_HOME

> 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
> java -jar $JETTY_HOME/start.jar

打开http://localhost:8080 查看内容。

1.修改默认端口命令

默认端口号是8080,可以通过以下命令启动时修改默认端口

>java -jar $JETTY_HOME/start.jar jetty.http.port=8083

或者直接修改$JETTY_BASE/start.d/http.ini配置文件

2.Configuration Files

Configuring Web Applications

The servlet specification defines a web application, which when packaged as a zip is called WAR file (Web application ARchive). Jetty implements both WAR files and unpacked web applications as a specialized context that is configured by means of:

  • A standard layout which sets the location of the resourceBase (the root of the WAR) and initializes the classpath from jars found in WEB-INF/lib and classes found in WEB-INF/classes.
  • The standard WEB-INF/web.xml deployment descriptor which is parsed to define and configure init parameters, filters, servlets, listeners, security constraints, welcome files and resources to be injected.
  • A default web.xml format deployment descriptor provided either by Jetty or in configuration configures the JSP servlet and the default servlet for handling static content. The standard web.xml may override the default web.xml.
  • Annotations discovered on classes in Jars contained in WEB-INF/lib can declare additional filters, servlets and listeners.
  • Standard deployment descriptor fragments discovered in Jars contained in WEB-INF/lib can declare additional init parameters, filters, servlets, listeners, security constraints, welcome files and resources to be injected.
  • An optional WEB-INF/jetty-web.xml file may contain Jetty IoC configuration to configure the Jetty specific APIs of the context and handlers.

Web Application Deployment

Jetty is capable of deploying a variety of Web Application formats. This is accomplished via scans of the ${jetty.base}/webapps directory for contexts to deploy.

A Context can be any of the following:

  • A standard WAR file. (must in ".war").
  • A directory containing an expanded WAR file. (must contain {dir}/WEB-INF/web.xml file).
  • A directory containing static content.
  • A XML descriptor in Jetty XML Syntax that configures a ContextHandler instance (Such as a WebAppContext).

Quickstart Webapps

The auto discovery features of the Servlet specification can make deployments slow and uncertain. Auto discovery of Web Application configuration can be useful during the development of a webapp as it allows new features and frameworks to be enabled simply by dropping in a jar file. However, for deployment, the need to scan the contents of many jars can have a significant impact of the start time of a webapp.

With the release of Jetty 9.2, a quickstart module was included which allows a webapp to be pre-scanned and preconfigured. This means that all the scanning is done prior to deployment and all configuration is encoded into an effective web.xml, called WEB-INF/quickstart-web.xml, which can be inspected to understand what will be deployed before deploying. Not only does the quickstart-web.xml contain all the discovered Servlets, Filters and Constraints, but it also encodes as context parameters all discovered:

  • ServletContainerInitializers
  • HandlesTypes classes
  • Taglib Descriptors

With the quickstart mechanism, Jetty is able to entirely bypass all scanning and discovery modes and start a webapp in a predictable and fast way. Tests have shown that webapps that took many seconds to scan and deploy can now be deployed in a few hundred milliseconds.

In a standard Jetty distribution the quickstart module can be configured with the following command:

$ java -jar $JETTY_HOME/start.jar --add-to-start=quickstart

Pre-generating the quickstart-web.xml file

Rather than use the autoPreconfigure feature of the QuickStartWebApp - which lazily generates the quickstart-web.xml file - you can eagerly pre-generate it for an existing war by invoking as a main class org.eclipse.jetty.quickstart.PreconfigureQuickStartWar. Note that you will need to provide all necessary jetty jars on the command line classpath. This will unpack the war if necessary, and create the quickstart-web.xml before the first deployment:

$ java -cp [jetty classpath] org.eclipse.jetty.quickstart.PreconfigureQuickStartWar myapp.war

Starting Jetty Using start.jar

https://www.eclipse.org/jetty/documentation/9.4.x/start-jar.html

Debugg and Start Logging

--debug

Enables debugging output of the startup procedure.

Note: This does not set up debug logging for Jetty itself. For information on logging, please see the section on Configuring Jetty Logging.]

--start-log-file=<filename>

Sends all startup output to the filename specified. Filename is relative to ${jetty.base}. This is useful for capturing startup issues where the Jetty-specific logger has not yet kicked in due to a possible startup configuration error.

Startup / Shutdown Command Line

--stop

Sends a stop signal to the running Jetty instance.

Note: The server must have been started with various stop properties for this to work.

STOP.PORT=<number>

The port to use to stop the running Jetty server. This is an internal port, opened on localhost, used solely for stopping the running Jetty server. Choose a port that you do not use to serve web traffic.

Required for --stop to function.

STOP.KEY=<alphanumeric>

The passphrase defined to stop the server.

Required for --stop to function.

STOP.WAIT=<number>

The time (in seconds) to wait for confirmation that the running Jetty server has stopped. If not specified, the stopper waits indefinitely for the server to stop.

If the time specified elapses, without a confirmation of server stop, then the --stop command exits with a non-zero return code.

You can configure a port number for Jetty to listen on for a stop command, so you are able to stop it from a different terminal. This requires the use of a "secret" key, to prevent malicious or accidental termination. Use the STOP.PORT and STOP.KEY (or -DSTOP.PORT= and -DSTOP.KEY=, respectively, which will set these as system parameters) parameters as arguments to the start.jar:

> java -jar ${JETTY_HOME}/start.jar STOP.PORT=1234 STOP.KEY=secretpassword

Then, to stop Jetty from a different terminal, you need to supply this port and key information. You can either use a copy of the Jetty distribution, the jetty-maven-plugin, the jetty-ant plugin, or a custom class to accomplish this. Here’s how to use the Jetty distribution, leveraging start.jar, to perform a stop:

> java -jar start.jar STOP.PORT=8181 STOP.KEY=abc123 --stop

Startup a Unix Service using jetty.sh

https://www.eclipse.org/jetty/documentation/9.4.x/startup-unix-service.html

Jetty Runner

https://www.eclipse.org/jetty/documentation/9.4.x/runner.html

The idea of the jetty-runner is extremely simple – run a webapp directly from the command line using a single jar file and as much default configuration as possible.

> java -jar jetty-runner.jar --port 9090 simple.war
> java -jar jetty-runner.jar --host 192.168.22.19 my.war
> java -jar jetty-runner.jar simple
> java -jar jetty-runner.jar --help

Ant and Jetty

https://www.eclipse.org/jetty/documentation/9.4.x/ant-and-jetty.html#jetty-ant

参考链接:

https://en.wikipedia.org/wiki/Jetty_(web_server)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值