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" ...
随机推荐
- Tomcat8访问管理页面localhost出现:403 Access Denied
问题: Access Denied You are not authorized to view this page. If you have already configured the Manag ...
- XE7 iOS 取得系统字型名称
系统字型名称在每个平台的取得方式不尽相同,以下示范如何在 iOS 取得系统内所有字型的名称: uses // 加入下面二个 uses 文件 iOSapi.UIKit, iOSapi.Foundatio ...
- HTTP协议简解
1.什么是http协议 http协议: 浏览器客户端 与 服务器端 之间数据传输的规范 2.查看http协议的工具 1)使用火狐的firebug插件(右键->查看元素->网络) 2)使用 ...
- Elasticsearch5.0 BreakChange摘要
Lucene版本 6.2.1 常用API变化重点关注 Java api 1 client create方式变更: 2 count api removed; 3 suggest api removed; ...
- SDL制作拼图游戏
看完教程第三集后,好像自己能用这个来写一个简单的拼图游戏,第一次写出个带界面的游戏,好有成就感. 图片是自己慢慢截左上部分8个脸. #include <stdio.h> #include ...
- 【FFmpeg】Windows下64位ffmpeg编译
本文主要记录在64位Windows 7下,编译64位ffmpeg的过程. 1.资源准备 (1). MSYS http://sourceforge.net/projects/mingwbuilds/fi ...
- cnodejs社区论坛6--评论功能
- linux TCP: time wait bucket table overflow
早上一台rabbitmq和Java所在的服务器,客户端反馈超级卡,看io和cpu都不高.发现六七万消息挤压,临时性问题解决之后,看/var/log/messages,发现很多TCP: time wai ...
- Page Scroll Effects - 简单的页面滚动效果
Codyhouse 收集了一组页面滚动效果,就是目前大家很常见的用户在浏览网页的时候.一些效果虽然极端,但如果你的目标是创建一个身临其境的用户体验,他们是非常有用的.所有的动画都使用 Velocity ...
- Linux 常见命令说明
目前由于自己接触到的是 windows 的操作系统,所以会经常使用 git bash 来提交代码到 github上. git bash 是 windows 下模拟 Linux 的命令行工具. 在此总结 ...