【问题标题】:Servlet 3.0 support in embedded Jetty 8.0嵌入式 Jetty 8.0 中的 Servlet 3.0 支持
【发布时间】:2010-08-11 22:11:48
【问题描述】:

对于我的单元测试,我使用基于 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?

【问题讨论】:

    标签: servlets embedded-jetty


    【解决方案1】:

    添加到您的代码中

    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 声称默认情况下将它们打开。我猜这是配置文件的差异。

    【讨论】:

    • 我意识到这篇文章很古老,但我发现即使在这篇文章发布一年后,我的嵌入式服务器也遇到了同样的问题。
    • 我发现 AsyncContext 在 Jetty 中不能正常工作。我暂时放弃了它,转而使用 Embedded Tomcat 7。目前它似乎运行良好。
    【解决方案2】:

    一年后再回答。

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

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

    从码头主目录调用。

    【讨论】:

      【解决方案3】:

      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 插件的权威资源。但是,它确实以类似的方式工作。

      我希望这至少会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-24
        • 2015-10-16
        • 2015-02-25
        • 2012-04-10
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        相关资源
        最近更新 更多