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:
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 }
<!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:
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 25 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 26 context.setContextPath("/jetty"); 27 context.addServlet(new ServletHolder(new HelloWebSocket()), "/hello"); 28 29 context.addServlet(new ServletHolder(new HelloServlet()), "/helloworld"); 30 31 HandlerCollection handlerCollection = new HandlerCollection(); 32 handlerCollection.setHandlers(new Handler[]{context}); 33 34 server.setHandler(handlerCollection); 35 36 37 try { 38 server.start(); 39 server.join(); 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 44 } 45 }
项目地址:https://github.com/windwant/windwant-demo/tree/master/jetty-service