嵌入式Jetty 8.0中的Servlet 3.0支持

时间:2010-08-11 22:11:49

标签: servlets embedded-jetty

对于我的单元测试,我使用一个基于Jetty的简单测试服务器:

package eu.kostia.textanalysis.webservices.jetty;

import java.awt.Desktop;
import java.net.URI;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class TestServer {
 static private final String CONTEXT_PATH = "/webservice";
 static private final String PROJECT_HOME = System.getenv("MY_WORKSPACE_HOME") + "/WebServices";
 static public final int PORT = 8080;

 private Server server;
 private Exception startException;

 private static class SingletonHolder {
  private static final TestServer INSTANCE = new TestServer();
 }

 /**
  * Returns the singleton instance of the test server.
  * 
  * @return the singleton instance of the test server.
  */
 public static TestServer getInstance() {
  return SingletonHolder.INSTANCE;
 }

 private TestServer() {
  server = new Server(PORT);

  WebAppContext context = new WebAppContext();

  context.setDescriptor(PROJECT_HOME + "/web/WEB-INF/web.xml");
  context.setResourceBase(PROJECT_HOME + "/web");
  context.setContextPath(CONTEXT_PATH);
  context.setParentLoaderPriority(true);


  server.setHandler(context); 
 }

 /**
  * Start the test server. This method returns only when the server is
  * complete started. There is no effect when you invoke this method and the
  * server is already running.
  */
 public void start() {  
  if (!server.isRunning()) {
   startException = null;
   new Thread("TestServer") {
    public void run() {
     try {
      server.start();
      server.join();
     } catch (Exception exc) {
      startException = exc;
     }
    }
   }.start();

   while (true) {
    if (startException != null) {
     throw new Error(startException);
    }

    // Block this method call until the server is started
    if (server.isStarted()) {
     return;
    }
   }
  }
 }

 /**
  * Stop the test server.
  */
 public void stop() {
  try {
   if (server.isRunning()) {
    server.stop();
   }
  } catch (Exception e) {
   throw new Error(e);
  }
 }

 /**
  * Returns {@code true} is the server is running.
  * 
  * @return {@code true} is the server is running.
  */
 public boolean isRunning() {
  return server.isRunning();
 }

 public static void main(String[] args) throws Exception {
  TestServer.getInstance().start();  
  Desktop.getDesktop().browse(new URI("http://localhost:8080/webservice/"));
 }

}

它适用于在web.xml中配置的servlet,但我现在想使用Servlet Specification 3.0引入的新注释语法,例如:

@WebServlet(urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                PrintWriter writer = response.getWriter();
                writer.print("<h1>HttpServlet using Servlet 3.0</h1>");
        }
}

我如何在TestServer类中配置Jetty以处理基于注释的servlet?

3 个答案:

答案 0 :(得分:9)

添加到您的代码

context.setConfigurations(new Configuration[] {
                new AnnotationConfiguration(), new WebXmlConfiguration(),
                new WebInfConfiguration(), new TagLibConfiguration(),
                new PlusConfiguration(), new MetaInfConfiguration(),
                new FragmentConfiguration(), new EnvConfiguration() });

您只需设置AnnotationConfiguration即可自动发现带注释的类。其余配置是您可以启用容器的其他方面。据说你应该可以从命令行执行此操作,使用OPTIONS = annotations,jsp,(etc ...),但我从来没有这样做。至少在这种情况下,它应该在嵌入式环境中正确地选取带注释的类。

另外作为旁注,看来Eclipse jetty项目默认关闭注释,而riptide声称默认启用它们。我猜这是配置文件的不同之处。

答案 1 :(得分:1)

再过一年后回答。

在当前版本的Jetty(8.1)中,您可以使用命令行完成您想要的任务:

java -jar start.jar OPTIONS=annotations,plus etc/jetty-plus.xml

从码头主目录调用。

答案 2 :(得分:0)

Jetty 8正在实现servlet 3.0规范,但它仍然是实验性的。

您还可以使用嵌入式glassfish 3插件来运行测试。有关以下信息,请参阅以下链接: http://wikis.sun.com/display/GlassFish/3.1EmbeddedOnePager http://ocpsoft.com/java/using-embedded-glassfish-with-maven/ http://embedded-glassfish.java.net/

我在写这篇文章时意识到,在Jetty经常使用的方式中没有使用Glassfish插件的权威资源。但它确实以类似的方式工作。

我希望这至少有一点帮助。