博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Jetty 嵌入式开发(实例)

Posted on 2016-03-15 17:26  和风细雨汪汪  阅读(901)  评论(0编辑  收藏  举报

我尝试了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