Java implements embedded Web server and servlet container using jetty

Source: Internet
Author: User

First introduce the next Jetty, according to the wiki:

Jetty is a purely Java-based web server and Java Servlet container. Although Web servers are often used to render documents for people, Jetty is typically used in larger software frameworks for communication between computers and machines.

as part of the Eclipse Foundation, Jetty is a free and open source project . The Web server is used in Apache ActiveMQ, Alfresco, Apache Geronimo, Apache Maven, Google App Engine, Eclipse, FUSE and more.

Jetty is also a server for open source projects such as Lift, Eucalyptus, Red5, Hadoop, and I2P. Jetty supports the latest Java Servlet API (with JSP support) and supports the SPDY and WebSocket protocols.

In 2016, Jetty's code master repository was migrated to Github, but it was still under the Eclipse IP Process policy.

Jetty provides WEB services in embedded Java applications , which are already part of the Eclipse IDE. It supports AJP, JASPI, JMX, JNDI, OSGi, WebSocket, and other Java technologies.

Apache Hadoop is a typical example of the Jetty application in the framework. Hadoop uses jetty as a WEB server in several modules

To summarize:

Jetty is a Java-implemented open-source servlet container that can be used as a complete Web server and Servlet container, as well as Tomcat, and can be embedded in a Java application and called in a Java program Jett Y

Because it's "lightweight", it's a good choice for small projects that aren't very complex, and it's very fast to start (load).

The following is the main view of the application of Jetty in embedded Java applications

Loading static pages

The import dependency does not say that the Jetty itself is distributed through a jar package, or can be built using Maven:

<dependency>  <groupId>org.eclipse.jetty</groupId>  <artifactId>jetty-servlet</artifactId>  <version>9.2.1.v20140609</version></dependency>

Of course, the Servlet-related dependencies Don't forget to join, then the Java code entry:

publicstaticvoidmainthrows Exception {  newServer(8080);  newResourceHandler();  resourceHandler.setResourceBase("D:/test");  // 可显示目录结构,类似 FTP  resourceHandler.setDirectoriesListed(true);   server.setHandler(resourceHandler);  server.start

Run the Java program, the Jetty server will be launched, in the browser can be accessed, but this way can only access static pages, does not support servlet/jsp

Implementing a servlet container (external)

Java Code Main entry:

publicstaticvoidmainthrows Exception {    newServer(8080);    newWebAppContext();  webapp.setResourceBase("E:/apache-tomcat-7.0.47/webapps/test");  // 也可以通过设置 war 包的方式  // webapp.setWar("C:/TVPlay.war");    server.setHandler(webapp);  server.start();}

is to set up a directory for a Java WEB application, which is the way to use an external file (address)

Running a servlet written internally

Many times we need to write a few servlets, not to build a Web project, this time with Jetty to embed a server is most suitable, the main entrance:

 Public Static void Main(string[] args)throwsException {Server Server =New Server(8080); Servletcontexthandler context =New Servletcontexthandler(Servletcontexthandler.SESSIONS);//or Servletcontexthandler.no_sessionsContext.Setcontextpath("/"); Server.SetHandler(context);//Http://localhost:8080/helloContext.Addservlet(New Servletholder(New HelloServlet()),"/hello");//Http://localhost:8080/hello/KerronexContext.Addservlet(New Servletholder(New HelloServlet("Hello kerronex!")),"/hello/kerronex"); Server.Start(); Server.Join();}

The specific Servlet I will not post, very simple doget test can be ~ ~

After packing, just use the command java -jar xxx.jar to allow it.

About Join

If the server is not up, the join () function has the effect of blocking the thread , where the join () function essentially calls the jetty thread pool (similar to the Join function in thread)

If there is no join function, the jetty server can start or run normally, because the jetty is small and the boot speed is very fast .

However, if your application is heavier, calling the join function will ensure that your server is really up (that is, before jetty start, the join method is blocked and the JVM exits)

Other

TODO: Build a Web project using Jetty

Need to use plug-ins and related dependencies:

<dependency>  <groupId>Org.eclipse.jetty</groupId>  <artifactId>Jetty-websocket</artifactId>  <version>8.1.11.v20130520</version></dependency><dependency>  <groupId>Org.eclipse.jetty</groupId>  <artifactId>Jetty-webapp</artifactId>  <version>8.1.11.v20130520</version></dependency><!--jetty --<dependency>  <groupId>Org.eclipse.jetty</groupId>  <artifactId>Jetty-server</artifactId>  <version>8.1.11.v20130520</version></dependency><build>  <plugins>    <plugin>      <groupId>Org.eclipse.jetty</groupId>      <artifactId>Jetty-maven-plugin</artifactId>      <version>9.3.7.v20160115</version>      <configuration>        <webApp>          <contextPath>/</contextPath>        </webApp>        <scanIntervalSeconds>3</scanIntervalSeconds>        <scanTargetPatterns>          <scanTargetPattern>            <directory>Src/main/webapp</directory>             <includes>              <include>**/*.xml</include>              <include>**/*.properties</include>            </includes>          </scanTargetPattern>        </scanTargetPatterns>        <webAppConfig>          <defaultsDescriptor>Src/main/resource/webdefault222.xml</defaultsDescriptor>        </webAppConfig>        <connectors>          <connectorimplementation="Org.eclipse.jetty.server.nio.SelectChannelConnector">            <port>8010</port>            <maxIdleTime>400000</maxIdleTime>          </connector>        </connectors>      </configuration>    </plugin>    </plugins></build>

Related commands:

mvn jetty:run

mvn -Djetty.http.port=9999 jetty:run

See:

https://www.zhihu.com/question/52433013

Http://www.blogjava.net/fancydeepin/archive/2015/06/23/maven-jetty-plugin.html

http://blog.csdn.net/tomato__/article/details/37927813

Individually downloaded Jetty jar packages can be run separately and also using the Java-jar command

Java implements embedded Web server and servlet container using jetty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.