Jetty provides a Web server and javax.servlet container, plus support for HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These components are open source and available for commercial use and distribution.

Jetty is used in a wide variety of projects and products, both in development and production. Jetty can be easily embedded in devices, tools, frameworks, application servers, and clusters. See the Jetty Powered page for more uses of Jetty.

The current recommended version for use is Jetty 9 which can be obtained here: Jetty Downloads. Also available are the latest maintenance releases of Jetty 8 and Jetty 7.

The Jetty project has been hosted at the Eclipse Foundation since 2009. Prior releases of Jetty have existed in part or completely under the Jetty project at the The Codehaus and Sourceforge before that. See the About page for more information about the history of Jetty.

Features Jetty Powered
  • Jetty Powered
  • Full-featured and standards-based
  • Open source and commercially usable
  • Flexible and extensible
  • Small footprint
  • Embeddable
  • Asynchronous
  • Enterprise scalable
  • Dual licensed under Apache and Eclipse
  • Large clusters, such as the Yahoo Hadoop Cluster
  • Cloud computing, such as the Google AppEngine
  • SaaS, such as Yahoo! Zimbra
  • Application Servers, such as Apache Geronimo
  • Frameworks, such as GWT
  • Tools, such as the Eclipse IDE
  • Devices, such as phones
  • More…​

轻量级webserver servlet容器,包括HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS等支持集成...

项目实例:

Servlet:

  1. package org.windwant.jetty.servlet;
  2.  
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8.  
  9. /**
  10. * HelloServlet
  11. */
  12. public class HelloServlet extends HttpServlet {
  13. public HelloServlet() {
  14. super();
  15. }
  16.  
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. String name = req.getParameter("name");
  20. resp.setContentType("text/html");
  21. resp.setStatus(HttpServletResponse.SC_OK);
  22. resp.getWriter().println("<h1>" + (name == null?"random:":name) + ":" + Math.random()*1000 + "</h1>");
  23. resp.getWriter().println("session=" + req.getSession(true).getId());
  24. }
  25. }

WebsocketSevlet:

  1. package org.windwant.jetty.websocket;
  2.  
  3. import org.eclipse.jetty.websocket.api.Session;
  4. import org.eclipse.jetty.websocket.api.WebSocketListener;
  5.  
  6. import java.io.IOException;
  7.  
  8. /**
  9. * MyEchoSocket
  10. */
  11. public class MyEchoSocket implements WebSocketListener {
  12.  
  13. private Session session;
  14. public void onWebSocketBinary(byte[] bytes, int i, int i1) {
  15.  
  16. }
  17.  
  18. public void onWebSocketText(String s) {
  19. if (session == null){
  20. return;
  21. }
  22.  
  23. try{
  24. System.out.println("server received msg: " + s);
  25. session.getRemote().sendString("server response msg: " + s);
  26. }catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. public void onWebSocketClose(int i, String s) {
  32. session = null;
  33. }
  34.  
  35. public void onWebSocketConnect(Session session) {
  36. this.session = session;
  37. }
  38.  
  39. public void onWebSocketError(Throwable throwable) {
  40. throwable.printStackTrace();
  41. }
  42. }
  1. package org.windwant.jetty.websocket;
  2.  
  3. import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
  4. import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
  5.  
  6. /**
  7. * HelloWebSocket
  8. */
  9. public class HelloWebSocket extends WebSocketServlet {
  10. @Override
  11. public void configure(WebSocketServletFactory webSocketServletFactory) {
  12. webSocketServletFactory.register(MyEchoSocket.class);
  13. }
  14. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <script type="text/javascript">
  8. if(!window.WebSocket){
  9. window.WebSocket = window.MozWebsocket;
  10. }
  11.  
  12. if(window.WebSocket){
  13. socket = new WebSocket("ws://localhost:8080/jetty/hello");
  14. socket.onmessage = function(event){
  15. document.getElementById("rtext").value = event.data;
  16. }
  17.  
  18. socket.onopen = function(event){
  19. document.getElementById("rtext").value = "socket open";
  20. }
  21.  
  22. socket.onclose = function(event){
  23. document.getElementById("rtext").value = "socket close";
  24. }
  25. }else{
  26. alert("browser not supported");
  27. }
  28.  
  29. function sendMessage(){
  30. if(!window.WebSocket){
  31. return;
  32. }
  33.  
  34. if(socket.readyState == WebSocket.OPEN){
  35. socket.send(document.getElementById('message').value);
  36. }else{
  37. alert('waiting connecting');
  38. }
  39. }
  40.  
  41. </script>
  42. <body>
  43. <input type="text" id="message" name="message" value="best prictice">
  44.  
  45. <input type="button" onclick="sendMessage()" value="SEND">
  46.  
  47. </br>
  48. </br>
  49. </br>
  50. <textarea id="rtext" style="width: 30%; height: 200px;"/>
  51. </body>
  52. </html>

Jetty Server:

  1. package org.windwant.jetty;
  2.  
  3. import org.eclipse.jetty.server.Connector;
  4. import org.eclipse.jetty.server.Handler;
  5. import org.eclipse.jetty.server.Server;
  6. import org.eclipse.jetty.server.ServerConnector;
  7. import org.eclipse.jetty.server.handler.HandlerCollection;
  8. import org.eclipse.jetty.servlet.ServletContextHandler;
  9. import org.eclipse.jetty.servlet.ServletHolder;
  10. import org.windwant.jetty.servlet.HelloServlet;
  11. import org.windwant.jetty.websocket.HelloWebSocket;
  12.  
  13. /**
  14. * ServletContextServer
  15. */
  16. public class ServletContextServer {
  17. public static void main(String[] args) {
  18. Server server = new Server();
  19.  
  20. ServerConnector connector = new ServerConnector(server);
  21. connector.setPort(8080);
  22. server.setConnectors(new Connector[]{connector});
  23.  
  24. ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  25. context.setContextPath("/jetty");
  26. context.addServlet(new ServletHolder(new HelloWebSocket()), "/hello");
  27.  
  28. context.addServlet(new ServletHolder(new HelloServlet()), "/helloworld");
  29.  
  30. HandlerCollection handlerCollection = new HandlerCollection();
  31. handlerCollection.setHandlers(new Handler[]{context});
  32.  
  33. server.setHandler(handlerCollection);
  34.  
  35. try {
  36. server.start();
  37. server.join();
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41.  
  42. }
  43. }

项目地址:https://github.com/windwant/windwant-demo/tree/master/jetty-service

Jetty 发布web服务的更多相关文章

  1. (七)CXF之与spring整合发布web服务

    一.需求分析 用spring发布服务 二.案例 2.1 引入maven依赖 <dependencies> <!-- 添加Spring支持 --> <dependency& ...

  2. svn + nginx unit + python3自动化发布web服务方法

    本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...

  3. dubbo发布web服务实例

    dubbo角色与调用执行过程 dubbo节点角色说明:provider: 暴露服务的服务提供方consumer: 调用远程服务的服务消费方registry: 服务注册于发现的注册中心monitor: ...

  4. 【maven】之使用jetty发布web项目

    <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin ...

  5. IIS7.0发布Web服务-0001

    配置错误 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (overrideModeDefault="Deny"),或者是通过包含 ove ...

  6. 在Eclipse中,用XFire发布web服务

    配置及相关说明见http://www.cnblogs.com/xshy3412/archive/2007/09/29/910848.html 仅确定发布xfire需要的最基本的jar activati ...

  7. VS2015发布web服务

    一.IIS中 ①添加网站 二.VS2015 ①右键解决方案→发布: ②自定义,设置配置文件名称: ③ ④发布     三.IIS中浏览(图片的ip地址是自己,上面的ip是截图别人的,所以不一样)

  8. Java和Tomcat的关系 Java如何发布web服务

    https://blog.csdn.net/qq_31301961/article/details/80732669 除了Tomcat还有WebLogic 大型分布式的 如何部署映射 Tomcat使用 ...

  9. Azure机器学习入门(四)模型发布为Web服务

    接Azure机器学习(三)创建Azure机器学习实验,下一步便是真正地将Azure机器学习的预测模型发布为Web服务.要启用Web服务发布任务,首先点击底端导航栏的运行即"Run" ...

随机推荐

  1. windbg学习进阶之——windbg环境变量配置

    接触性能调优以来一直想学下windbg分析dump,每次看老师几个命令就能找到很底层的问题原因那简直就是羡慕加崇拜啊~但是这接近一年了,愣是没啥进展呢,主要就是在今天整理的这部分卡住了...这理由找的 ...

  2. SQLServer中游标是如何处理数据的?

    游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力.我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许 ...

  3. TFS签入签出规范

    TFS签入签出规范1)开发平台的约定a)开发操作系统环境和最终用户使用环境 包含Service Pack版本号开发环境 Windows2008SP1 Windows7用户环境 Windows2008S ...

  4. JMS中的消息通信模型

    1. MQ简介: 消息队列(Message Queue,简称MQ),是应用程序与应用程序之间的一种通信方法.应用程序通过发送和检索出入列队的针对应用程序的数据 - 消息来通信,而无需专用连接来链接它们 ...

  5. DokuWiki整合Zentao的用户授权及分组体系

    老外们把精力都放在了怎样做通用性上面了. Doku后台有切换授权方式的选项,改成mysql. 注:如下修改mysql.conf.php后,要把分组和权限设置结合起来,还需要配置dokuwiki的分组, ...

  6. java servlet手机app访问接口(三)高德地图云存储及检索

    这篇关于高德地图的随笔内容会多一点, 一.业务说明     对应APP业务中的成员有两类,一是服务人员,二是被服务人员,  主要实现功能, 对APP中的服务人员位置进行时时定位, 然后通过被服务人员登 ...

  7. C#多线程学习笔记

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. PHP与MySQL的交互(mysqli)

    近期在学习PHP,这里总结一下PHP与MySQL的交互. 这里我们使用mysqli进行连接. mysqli扩展允许我们访问MySQL 4.1及以上版本提供的功能. 想深入了解mysqli的信息可以访问 ...

  9. No.010:Regular Expression Matching

    问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...

  10. Hibernate(一)__简介

    一. hibernate是什么 (一)hibernate 是一个orm框架,orm (object relation mapping) 对象关系映射框架 o object -> 业务层(只对对象 ...