zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    线性回归和 逻辑回归 的思考(参考斯坦福 吴恩达的课程)
    数据结构算法基础-内部排序算法
    机器学习《test》
    day1.接口测试(概念、Postman、SoapUI、jmeter)
    SQL2000 3核6核 CUP 安装SP4
    SQL常用语句
    SQL SERVER 2000数据库置疑处理
    常用终端命令
    c++ 位操作
    计算机为什么用补码存储数据?
  • 原文地址:https://www.cnblogs.com/zzt-lovelinlin/p/5280345.html
Copyright © 2011-2022 走看看