Embedding Jetty 创建简单的内嵌服务器
来源:互联网 发布:windows vista iso 编辑:程序博客网 时间:2024/06/06 01:38
一. 创建内嵌的Jetty服务器主要有以下几个步骤:
二. 创建一个最简单的服务器
packageorg.eclipse.jetty.embedded; importorg.eclipse.jetty.server.Server; /** The simplest possible Jetty server. */publicclass SimplestServer { publicstatic void main(String[] args) throwsException { Server server = newServer(8080); server.start(); server.join(); }}
三. 使用Handler
1. 检查或者修改HTTP request2. 产生完整的HTTP response3. 调用其它的Handler处理程序4. 选择一个或多个Handlers去调用
packageorg.eclipse.jetty.embedded; importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.eclipse.jetty.server.Request;importorg.eclipse.jetty.server.handler.AbstractHandler; publicclass HelloHandler extendsAbstractHandler { finalString _greeting; finalString _body; publicHelloHandler() { _greeting = "Hello World"; _body = null; } publicHelloHandler(String greeting) { _greeting = greeting; _body = null; } publicHelloHandler(String greeting, String body) { _greeting = greeting; _body = body; } publicvoid handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)throwsIOException, ServletException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println("<h1>"+ _greeting + "</h1>"); if(_body != null) response.getWriter().println(_body); }}
target–the target of the request, which is either a URI or a name from a named dispatcher.
baseRequest–the Jetty mutable request object, which is always unwrapped.
request–the immutable request object, which may have been wrapped by a filter or servlet.
response–the response, which may have been wrapped by a filter or servlet.
四. 运行HelloWordHandler
publicstatic void main(String[] args) throwsException { Server server = newServer(8080); server.setHandler(newHelloHandler()); server.start(); server.join();}
五. 资源处理程序 Resource Handler
packageorg.eclipse.jetty.embedded; importorg.eclipse.jetty.server.Handler;importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.server.handler.DefaultHandler;importorg.eclipse.jetty.server.handler.HandlerList;importorg.eclipse.jetty.server.handler.ResourceHandler; /** Simple Jetty FileServer. * This is a simple example of Jetty configured as a FileServer. */publicclass FileServer { publicstatic void main(String[] args) throwsException { Server server = newServer(8080); ResourceHandler resource_handler = newResourceHandler(); resource_handler.setDirectoriesListed(true); resource_handler.setWelcomeFiles(newString[] { "index.html"}); resource_handler.setResourceBase("."); HandlerList handlers = newHandlerList(); handlers.setHandlers(newHandler[] { resource_handler, newDefaultHandler() }); server.setHandler(handlers); server.start(); server.join(); }}
六. 内嵌的连接器 Embedding Connectors
packageorg.eclipse.jetty.embedded; importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.server.ServerConnector; /** * A Jetty server with one connectors. */publicclass OneConnector { publicstatic void main(String[] args) throwsException { Server server = newServer(); ServerConnector http = newServerConnector(server); http.setHost("localhost"); http.setPort(8080); http.setIdleTimeout(30000); server.addConnector(http); server.setHandler(newHelloHandler()); server.start(); server.join(); }}
七. 多个连接器
packageorg.eclipse.jetty.embedded; importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.servlet.ServletHandler; publicclass MinimalServlets { publicstatic void main(String[] args) throwsException { Server server = newServer(8080); ServletHandler handler = newServletHandler(); server.setHandler(handler); handler.addServletWithMapping(HelloServlet.class,"/*"); server.start(); server.join(); } @SuppressWarnings("serial") publicstatic class HelloServlet extendsHttpServlet { @Override protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Hello SimpleServlet</h1>"); } }}
八. 内嵌的上下文 Embedding Contexts
A Classloader that is set as the Thread context classloader while request handling is in scope.
A set of attributes that is available via the ServletContext API.
A set of init parameters that is available via the ServletContext API.
A base Resource which is used as the document root for static resource requests via the ServletContext API.
A set of virtual host names.
packageorg.eclipse.jetty.embedded; importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.server.handler.ContextHandler; publicclass OneContext { publicstatic void main(String[] args) throwsException { Server server = newServer(8080); ContextHandler context = newContextHandler(); context.setContextPath("/"); context.setResourceBase("."); context.setClassLoader(Thread.currentThread().getContextClassLoader()); context.setHandler(newHelloHandler()); server.setHandler(context); server.start(); server.join(); }}
packageorg.eclipse.jetty.embedded; importorg.eclipse.jetty.server.Handler;importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.server.handler.ContextHandler;importorg.eclipse.jetty.server.handler.ContextHandlerCollection; publicclass ManyContexts { publicstatic void main(String[] args) throwsException { Server server = newServer(8080); ContextHandler context = newContextHandler("/"); context.setContextPath("/"); context.setHandler(newHelloHandler("Root Hello")); ContextHandler contextFR = newContextHandler("/fr"); contextFR.setHandler(newHelloHandler("Bonjoir")); ContextHandler contextIT = newContextHandler("/it"); contextIT.setHandler(newHelloHandler("Bongiorno")); ContextHandler contextV = newContextHandler("/"); contextV.setVirtualHosts(newString[] { "127.0.0.2"}); contextV.setHandler(newHelloHandler("Virtual Hello")); ContextHandlerCollection contexts = newContextHandlerCollection(); contexts.setHandlers(newHandler[] { context, contextFR, contextIT, contextV }); server.setHandler(contexts); server.start(); server.join(); }}
九. 内嵌多个Servlet上下文 Embedding ServletContexts
packageorg.eclipse.jetty.embedded; importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.servlet.ServletContextHandler;importorg.eclipse.jetty.servlet.ServletHolder; publicclass OneServletContext { publicstatic void main(String[] args) throwsException { Server server = newServer(8080); ServletContextHandler context = newServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); server.setHandler(context); context.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class,"/"); context.addServlet(newServletHolder(newDumpServlet()), "/dump/*"); server.start(); server.join(); }}
附录:
import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/*** Created by superman on 14-1-11.*/public class DumpServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>DumpServlet</h1><pre>"); response.getWriter().println("requestURI=" + request.getRequestURI()); response.getWriter().println("contextPath=" + request.getContextPath()); response.getWriter().println("servletPath=" + request.getServletPath()); response.getWriter().println("pathInfo=" + request.getPathInfo()); response.getWriter().println("session=" + request.getSession(true).getId()); String r=request.getParameter("resource"); if (r!=null) response.getWriter().println("resource("+r+")=" + getServletContext().getResource(r)); response.getWriter().println("</pre>"); }}
package jetty;import org.eclipse.jetty.server.*;import org.eclipse.jetty.util.ssl.SslContextFactory;/*** Created by superman on 14-1-11.*/public class ManyConnectors {public static void main(String[] args) throws Exception { String jetty_home = System.getProperty("jetty.home", "D:\\jetty-distribution-9.1.0.v20131115"); System.setProperty("jetty.home", jetty_home); Server server = new Server();//configure some common parameters HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.setSecureScheme("https"); httpConfig.setSecurePort(8443); httpConfig.setOutputBufferSize(32768);// HTTP connector// The first server connector we create is the one for http, passing in the http configuration we configured// above so it can get things like the output buffer size, etc. We also set the port (8080) and configure an// idle timeout. ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig)); httpConnector.setPort(8080); httpConnector.setIdleTimeout(30000);// SSL Context Factory for HTTPS and SPDY// SSL requires a certificate so we configure a factory for ssl contents with information pointing to what// keystore the ssl connection needs to know about. Much more configuration is available the ssl context,// including things like choosing the particular certificate out of a keystore to be used.69 SslContextFactory sslContextFactory = new SslContextFactory(); sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore"); sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");// HTTPS Configuration// A new HttpConfiguration object is needed for the next connector and you can pass the old one as an// argument to effectively clone the contents. On this HttpConfiguration object we add a// SecureRequestCustomizer which is how a new connector is able to resolve the https connection before// handing control over to the Jetty Server. HttpConfiguration https_config = new HttpConfiguration(httpConfig); https_config.addCustomizer(new SecureRequestCustomizer());// HTTPS connector// We create a second ServerConnector, passing in the http configuration we just made along with the// previously created ssl context factory. Next we set the port and a longer idle timeout. ServerConnector https = new ServerConnector(server,new SslConnectionFactory(sslContextFactory, "http/1.1"),new HttpConnectionFactory(https_config)); https.setPort(8443); https.setIdleTimeout(500000); server.setConnectors(new Connector[]{httpConnector, https}); server.setHandler(new HelloWorldHandler("hello", "welcome")); server.start(); server.join(); }}
- Embedding Jetty 创建简单的内嵌服务器
- Jetty embedding Server 基本概念
- 使用jetty作为内嵌服务器启动项目
- java使用jetty作为内嵌服务器启动项目
- JavaWeb之Jetty和Tomcat内嵌服务器实现
- 内嵌jetty
- 创建一个简单的Server(嵌入Jetty学习一)
- Jetty服务器的简介
- Jetty配置JAAS(独立的Jetty服务器)
- Maven webapp 内嵌 Jetty
- Jetty 1 内嵌启动
- Jetty 内嵌启动2
- jetty内嵌以后out.write引起的问题
- 内嵌的Jetty启动后访问503
- 创建简单的samba服务器
- 出色的Web服务器Jetty
- 出色的Web服务器Jetty
- 基于Jetty的内嵌式服务器
- 最长公共子序列问题(动态规划算法)
- Rectangles
- jquery实现的监听文本框输入事件
- 28-猴子吃桃
- 测试人员必备工作技能
- Embedding Jetty 创建简单的内嵌服务器
- Windows CE下驱动程序开发基础
- GameFolder - 简约、轻松管理电脑游戏
- 子元素浮动,父元素高度为0解决方法
- WinCE6.0中应用程序如何直接访问物理空间
- 改变 CListCtrl、CHeaderCtrl 高度、字体、颜色和背景
- “Cannot load php5apache2_4.dll into server”问题的解决方法
- 设计模式从何而来?
- 黑马程序员_面向对象—>多态