Jetty 发布web服务

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 |
|---|---|
|
|
轻量级webserver servlet容器,包括HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS等支持集成...
项目实例:
Servlet:
package org.windwant.jetty.servlet; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* HelloServlet
*/
public class HelloServlet extends HttpServlet {
public HelloServlet() {
super();
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
resp.setContentType("text/html");
resp.setStatus(HttpServletResponse.SC_OK);
resp.getWriter().println("<h1>" + (name == null?"random:":name) + ":" + Math.random()*1000 + "</h1>");
resp.getWriter().println("session=" + req.getSession(true).getId());
}
}
WebsocketSevlet:
package org.windwant.jetty.websocket; import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketListener; import java.io.IOException; /**
* MyEchoSocket
*/
public class MyEchoSocket implements WebSocketListener { private Session session;
public void onWebSocketBinary(byte[] bytes, int i, int i1) { } public void onWebSocketText(String s) {
if (session == null){
return;
} try{
System.out.println("server received msg: " + s);
session.getRemote().sendString("server response msg: " + s);
}catch (IOException e) {
e.printStackTrace();
}
} public void onWebSocketClose(int i, String s) {
session = null;
} public void onWebSocketConnect(Session session) {
this.session = session;
} public void onWebSocketError(Throwable throwable) {
throwable.printStackTrace();
}
}
package org.windwant.jetty.websocket; import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; /**
* HelloWebSocket
*/
public class HelloWebSocket extends WebSocketServlet {
@Override
public void configure(WebSocketServletFactory webSocketServletFactory) {
webSocketServletFactory.register(MyEchoSocket.class);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<script type="text/javascript">
if(!window.WebSocket){
window.WebSocket = window.MozWebsocket;
} if(window.WebSocket){
socket = new WebSocket("ws://localhost:8080/jetty/hello");
socket.onmessage = function(event){
document.getElementById("rtext").value = event.data;
} socket.onopen = function(event){
document.getElementById("rtext").value = "socket open";
} socket.onclose = function(event){
document.getElementById("rtext").value = "socket close";
}
}else{
alert("browser not supported");
} function sendMessage(){
if(!window.WebSocket){
return;
} if(socket.readyState == WebSocket.OPEN){
socket.send(document.getElementById('message').value);
}else{
alert('waiting connecting');
}
} </script>
<body>
<input type="text" id="message" name="message" value="best prictice"> <input type="button" onclick="sendMessage()" value="SEND"> </br>
</br>
</br>
<textarea id="rtext" style="width: 30%; height: 200px;"/>
</body>
</html>
Jetty Server:
package org.windwant.jetty; 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.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.windwant.jetty.servlet.HelloServlet;
import org.windwant.jetty.websocket.HelloWebSocket; /**
* ServletContextServer
*/
public class ServletContextServer {
public static void main(String[] args) {
Server server = new Server(); ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.setConnectors(new Connector[]{connector}); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/jetty");
context.addServlet(new ServletHolder(new HelloWebSocket()), "/hello"); context.addServlet(new ServletHolder(new HelloServlet()), "/helloworld"); HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.setHandlers(new Handler[]{context}); server.setHandler(handlerCollection); try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
} }
}
项目地址:https://github.com/windwant/windwant-demo/tree/master/jetty-service
Jetty 发布web服务的更多相关文章
- (七)CXF之与spring整合发布web服务
一.需求分析 用spring发布服务 二.案例 2.1 引入maven依赖 <dependencies> <!-- 添加Spring支持 --> <dependency& ...
- svn + nginx unit + python3自动化发布web服务方法
本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...
- dubbo发布web服务实例
dubbo角色与调用执行过程 dubbo节点角色说明:provider: 暴露服务的服务提供方consumer: 调用远程服务的服务消费方registry: 服务注册于发现的注册中心monitor: ...
- 【maven】之使用jetty发布web项目
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin ...
- IIS7.0发布Web服务-0001
配置错误 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (overrideModeDefault="Deny"),或者是通过包含 ove ...
- 在Eclipse中,用XFire发布web服务
配置及相关说明见http://www.cnblogs.com/xshy3412/archive/2007/09/29/910848.html 仅确定发布xfire需要的最基本的jar activati ...
- VS2015发布web服务
一.IIS中 ①添加网站 二.VS2015 ①右键解决方案→发布: ②自定义,设置配置文件名称: ③ ④发布 三.IIS中浏览(图片的ip地址是自己,上面的ip是截图别人的,所以不一样)
- Java和Tomcat的关系 Java如何发布web服务
https://blog.csdn.net/qq_31301961/article/details/80732669 除了Tomcat还有WebLogic 大型分布式的 如何部署映射 Tomcat使用 ...
- Azure机器学习入门(四)模型发布为Web服务
接Azure机器学习(三)创建Azure机器学习实验,下一步便是真正地将Azure机器学习的预测模型发布为Web服务.要启用Web服务发布任务,首先点击底端导航栏的运行即"Run" ...
随机推荐
- 使用List把一个长字符串分解成若干个短字符串
把一个长字符串分解成若干个固定长度的短字符串,由于事先不知道长字符串的长度,以及短字符串的数量,只能使用List. public static void get_list_sbody(String s ...
- python flask detect browser language
python flask detect browser language No problem. We won't show you that ad again. Why didn't you l ...
- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
场景:eclipse中编写java中用到数组 问题: 程序不报错但是运行过程中 终止,显示字样 “ Exception in thread "main" java.lang.Arr ...
- 第 19 章 CSS 其他样式
学习要点: 1.颜色和透明度 2.盒子阴影和轮廓 3.光标样式 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS 其他剩下几个常用的样式,包括颜色.透明度.盒子的阴影轮廓以及光标的样式. 一.颜 ...
- Java final类
如果说整个类都是final(在它的定义前冠以final关键字),就表明自己不希望从这个类继承,或者不允许其他任何人采取这种操作.换言之,出于这样或那样的原因,我们的类肯定不需要进行任何改变:或者出于安 ...
- ionic + cordova 使用 cordova-gallery-api 获取本地相册所有图片
cordova-gallery-api 插件定义一个全局galleryapi对象,提供查询图库相册的方法 安装 cordova-gallery-api: cordova plugin add http ...
- EXCEL经纬度转化
=LEFT(A1,FIND("°",A1)-1)+MID(A1,FIND("°",A1)+1,FIND(CHAR(39),A1)-1-FIND("°& ...
- Sharepoint 文档库根据文件夹层级展示
类似于资源管理器,效果如下 步骤 打开Sharepoint Desinger,编辑Allitems.aspx页面 在PlaceHolderMain里面插入代码,黄色部分需要替换 <table s ...
- android listview多视图嵌套多视图
笔记,listview视图总结 public class HomeEduMoreAdapter extends BaseAdapter { private final String TAG = &qu ...
- XMPP学习——3、XMPP协议学习补充
流基础 两个基本概念,使得XMPP实体之间的小的结构化信息有效载荷能快速地进行异步交换:XML流和XML节.这些术语的定义如下. XML流的定义: XML流是一个容器,用于任何两个实体通过网络进行XM ...