Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2476304
  • 博文数量: 293
  • 博客积分: 2660
  • 博客等级: 少校
  • 技术积分: 3632
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-03 17:50
文章分类

全部博文(293)

文章存档

2015年(13)

2014年(58)

2013年(73)

2012年(25)

2011年(30)

2010年(86)

2009年(8)

分类: Web开发

2013-10-26 18:14:34

http://blog.csdn.net/kongxx/article/details/7230080
Jetty实战之 嵌入式Jetty运行Servlet
1. 首先创建一个ServletContextServer类,用来初始化web应用程序的Context,并且指定Servlet和Servlet匹配的url。这里指定了两个Servlet,分别是HelloServlet和GoodbyeServlet,并分别对应/hello/*和/goodbye/*。

package com.google.code.garbagecan.jettystudy.sample5;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class ServletContextServer {
 public static void main(String[] args) throws Exception {
  Server server = new Server(8080);

  ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  context.setContextPath("/");
  server.setHandler(context);

  //
  context.addServlet(new ServletHolder(new HelloServlet()), "/hello");
  //
  context.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/hello/kongxx");

  //
  context.addServlet(new ServletHolder(new GoodbyeServlet()), "/goodbye");
  //
  context.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/goodbye/kongxx");
  
  server.start();
  server.join();
 }
}
2. 两个简单的Servlet:HelloServlet和GoodbyeServlet:

package com.google.code.garbagecan.jettystudy.sample5;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
 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("

" + msg + "

");
  response.getWriter().println("session=" + request.getSession(true).getId());
 }
}

package com.google.code.garbagecan.jettystudy.sample5;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GoodbyeServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 private String msg = "Goodbye!";

 public GoodbyeServlet() {
 }

 public GoodbyeServlet(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("

" + msg + "

");
  response.getWriter().println("session=" + request.getSession(true).getId());
 }
}
3. 运行ServletContextServer类,然后分别访问以下四个url


 
  /kongxx
 
  /kongxx

4. 除了上面的方式外,也可以创建两个个Context,分别绑定到"/hello"和"/goodbye",如下:

package com.google.code.garbagecan.jettystudy.sample5;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class MultiContextServer {
 public static void main(String[] args) throws Exception {
  Server server = new Server(8080);

  //
  ServletContextHandler context1 = new ServletContextHandler(ServletContextHandler.SESSIONS);
  context1.setContextPath("/hello");
  context1.setResourceBase(".");
  context1.setClassLoader(Thread.currentThread().getContextClassLoader());
  context1.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/kongxx");

  //
  ServletContextHandler context2 = new ServletContextHandler(ServletContextHandler.SESSIONS);
  context2.setContextPath("/goodbye");
  context2.setResourceBase(".");
  context2.setClassLoader(Thread.currentThread().getContextClassLoader());
  context2.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/kongxx");

  ContextHandlerCollection contexts = new ContextHandlerCollection();
  contexts.setHandlers(new Handler[] { context1, context2 });

  server.setHandler(contexts);

  server.start();
  server.join();
 }
}

阅读(6765) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~