jetty服务器是十分优秀的java web应用服务器,支持的java的 jsp和 servlet标准,跟tomcat一样很受欢迎,,另外jetty更轻量一些,允许将jetty嵌入到程序中执行,在某些场景下十分有用。
[size=large][color=green]jetty嵌入式环境的使用[/color][/size]
内置jetty服务器
分发器:DispatchHandler
业务处理
[size=large][color=green]jetty嵌入式环境的使用[/color][/size]
内置jetty服务器
package com.gbcom.frame.jetty.demo;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
/**
* 创建服务器:
* 启动服务,通过浏览器访问: http://localhost:8888/index 和 http://localhost:8888/hello
*
*
* mvn 参考:2.0. 添加依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>7.6.9.v20130131</version>
</dependency>
如果在应用中使用到jsp的话需要添加jetty-jsp依赖,在jetty-all中并为聚合jetty-jsp模块。
* @author SYZ
* @date 2016-2-23 上午09:54:01
* @version 1.0.0
* @see com.gbcom.frame.jetty.demo.EmbedJettyServer
*/
public class EmbedJettyServer {
public static void main(String[] args) throws Exception {
// 创建Server
Server server = new Server();
// 添加ThreadPool
QueuedThreadPool queuedThreadPool = new QueuedThreadPool();
queuedThreadPool.setName("queuedTreadPool");
queuedThreadPool.setMinThreads(10);
queuedThreadPool.setMaxThreads(200);
server.setThreadPool(queuedThreadPool);
// 添加Connector
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8888);
connector.setAcceptors(4);
connector.setMaxBuffers(2048);
connector.setMaxIdleTime(10000);
server.addConnector(connector);
// 添加Handler
ContextHandlerCollection context = new ContextHandlerCollection();
ContextHandler contextHandler = context.addContext("/", "/");
contextHandler.setHandler(new DispatchHandler());
Handler defaults = new DefaultHandler();
HandlerCollection collection = new HandlerCollection();
collection.setHandlers(new Handler[] { context, defaults });
server.setHandler(collection);
// 启动服务
server.start();
while (server.isStarted()) {
System.out.println("server starting...");
break;
}
System.out.println("server stared...");
System.out.println("ContextHandlerCollectio.getServer() = "
+ context.getServer().hashCode());
System.out.println("Server:" + server.hashCode());
server.join();
}
}
分发器:DispatchHandler
package com.gbcom.frame.jetty.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
/**
* 编写分发处理类DispatchHandler
*
* @author SYZ
* @date 2016-2-23 上午09:54:34
* @version 1.0.0
* @see com.gbcom.frame.jetty.demo.DispatchHandler
*/
public class DispatchHandler extends AbstractHandler {
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
/**
* 这里将是根据不同的请求来分发给不同的Handler来处理
*/
if (target.equals("/index")) {
new IndexHandler().handle(target, baseRequest, request, response);
} else {
new HelloHandler().handle(target, baseRequest, request, response);
}
}
}
业务处理
package com.gbcom.frame.jetty.demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
/**
* 编写相应的Handler(IndexHandler和HelloHandler)
*
* @author SYZ
* @date 2016-2-23 上午09:54:45
* @version 1.0.0
* @see com.gbcom.frame.jetty.demo.IndexHandler
*/
public class IndexHandler extends AbstractHandler {
@Override
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("<h3>Hello World Jetty!</h3>");
out.write(Calendar.getInstance().getTime().toString());
out.flush();
out.close();
System.out.println("process http success!!!!");
}
}
package com.gbcom.frame.jetty.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
/**
* 编写相应的Handler(IndexHandler和HelloHandler)
* @author SYZ
* @date 2016-2-23 上午09:54:55
* @version 1.0.0
* @see com.gbcom.frame.jetty.demo.HelloHandler
*/
public class HelloHandler extends AbstractHandler {
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("<h3>This is a HelloHandler and contextPath is '/hello'.</h3>");
out.flush();
out.close();
}
}