Jetty 嵌入式开发(实例)
2016-03-15 17:26
260 查看
我尝试了jetty几个版本,类的使用有些差异,在此记录下jettyVersion = 9.0.2.v20130417 的部分实例
maven 依赖及配置:
以下是示例代码:
参考资料:
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
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
相关文章推荐
- [New learn] 网络基础-网络操作
- Android Volley入门到精通:使用Volley加载网络图片
- 用 awk 统计文本文件中的某一列之和 (附 awk 命令简介)
- gif动态图片取静态帧
- debug开启后就这错误了,,求大神指点。。。。
- 修改xib里的控件位置大小
- Android获取控件大小的方法
- 杀京东价格监控软件项目开发日志
- Uva 272TEX Quotes (f符号转换)
- 关于JAVA中URL传递中文参数,取值是乱码的解决办法
- JAVA开发之Eclipse常用的快捷键
- rxjava源码分析
- android listview 下拉头部图片拉伸
- Leetcode:205. Isomorphic Strings(JAVA)
- Android实际开发中用到的线程池
- 快速排序 约瑟夫问题 顺序查找 二分查找
- Jenkins上分析前端项目
- 【版本发布】JEECG 3.6.2 移动能力版发布,快速开发平台
- java操作hdfs实例
- c++ 走向高级之日积月累