Jetty 嵌入式开发(实例)

Posted 和风细雨汪汪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jetty 嵌入式开发(实例)相关的知识,希望对你有一定的参考价值。

我尝试了jetty几个版本,类的使用有些差异,在此记录下jettyVersion = 9.0.2.v20130417 的部分实例

maven 依赖及配置:

<properties>
        <!-- Adapt this to a version found on http://central.maven.org/maven2/org/eclipse/jetty/jetty-maven-plugin/ -->
        <jettyVersion>9.0.2.v20130417</jettyVersion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        
        <!-- jetty 依赖 -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jettyVersion}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>org.example.HelloWorld</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

以下是示例代码:

package com.bocom.testjetty.httpserver;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;

public class httpserver {

    public static void main(String[] arg0) throws Exception{
        server1();
        server2();
        
    }
    
    /**
     * 多个连接器实例
     * @throws Exception
     */
    public static void server1() throws Exception{
        Server server = new Server();
        ServerConnector connector1 = new ServerConnector(server);
        connector1.setPort(8082);
        ServerConnector connector2 = new ServerConnector(server);
        connector2.setPort(9001);
        
         server.setConnectors(new Connector[] { connector1, connector2 });
        server.setHandler(new HelloHandler());
        
        server.start();
    }
    
    /**
     * 多个handler实例
     * @throws Exception
     */
    public static void server2() throws Exception {
        
        Server server = new Server(9080);
        ContextHandler context1 = new ContextHandler();
        context1.setContextPath("/hello");
        context1.setHandler(new HelloHandler());
        ContextHandler context2 = new ContextHandler();
        context2.setContextPath("/content");
        context2.setHandler(new HelloHandler());
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[]{context1,context2});
        server.setHandler(handlers);
        server.start();
        server.join();
    }
    
    
}
package com.bocom.testjetty.httpserver;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloHandler extends AbstractHandler{

    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);//响应状态码
    baseRequest.setHandled(true);
    response.getWriter().println("<a>你好!</a>");
    
    }

}

参考资料:

http://www.eclipse.org/jetty/documentation/current/index.html

 

http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

 

http://blog.sina.com.cn/s/articlelist_2664952023_14_1.html

以上是关于Jetty 嵌入式开发(实例)的主要内容,如果未能解决你的问题,请参考以下文章

Jetty实战之 嵌入式运行Jetty 配置Https

Jetty简介

jersery+jetty嵌入式restful的框架开发

启动嵌入式 Jetty 服务器的最短代码

在嵌入式 Jetty 上使用 javax.websocket 进行 JUnit 测试抛出 RejectedExecutionException: NonBlockingThread

Undertow和jetty做嵌入式,哪个更好