核心类:org.mortbay.jetty.Server
核心接口:org.mortbay.component.LifeCycle
核心线程池封装:org.mortbay.thread.QueuedThreadPool
核心IO处理类:org.mortbay.jetty.nio.SelectChannelConnector
核心Servlet处理类:org.mortbay.jetty.servlet.ServletHandler
代码示例:
private static final long serialVersionUID = 1L;
private String msg = "Hello World!";
public HelloServlet() {
}
public HelloServlet(String msg) {
this.msg = msg;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>" + msg + "</h1>");
response.getWriter().println("session=" + request.getSession(true).getId());
}
}
public class GoodLuckServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private String msg = "祝你好运!";
public GoodLuckServlet() { public class JettyService {
public static void main(String[] args) throws Exception{
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
// http://localhost:8080/hello
context.addServlet(new ServletHolder(new HelloServlet()), "/hello");
// http://localhost:8080/hello/kongxx
context.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/hello/kongxx");
// http://localhost:8080/goodbye
context.addServlet(new ServletHolder(new GoodLuckServlet()), "/goodluck");
// http://localhost:8080/goodbye/kongxx
context.addServlet(new ServletHolder(new GoodLuckServlet("goodluck boy!")), "/goodluck/boy");
server.start();
server.join();
}
}
}
public GoodLuckServlet(String msg) {
this.msg = msg;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>" + msg + "</h1>");
response.getWriter().println("session=" + request.getSession(true).getId());
}
}