嵌入 Tomcat 并向已部署的应用程序发出请求

发布于 2024-08-18 00:29:38 字数 2264 浏览 3 评论 0原文

好吧,我完全被困住了。我想以嵌入式模式运行 Tomcat,这样我就可以测试应用程序,而无需在单独的进程中运行服务器。我错过了一些简单、愚蠢但重要的东西,我需要你的帮助才能看到它。

此测试失败并出现 HTTP 错误 400,错误请求。我尝试过 MemoryProtocolHandler、context.invoke(),...我不知道该怎么做。也许你看到一些简单的东西。

package ca.jbrains.jsfunit.learning.test;

import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.connector.Request;
import org.apache.catalina.realm.MemoryRealm;
import org.apache.catalina.startup.Embedded;
import org.junit.After;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.WebClient;

public class LearnEmbeddedTomcatTest {
    private Embedded embedded;
    private Container host;
    private Engine engine;

    @Test
    public void deploySampleApplicationFromFileSystem() throws Exception {
        String tomcatPath = "/Users/jbrains/ThirdParty/apache-tomcat-5.5.28-embed";

        // Create an embedded server
        embedded = new Embedded();
        embedded.setCatalinaHome(tomcatPath);
        embedded.setRealm(new MemoryRealm());

        // Create an engine
        engine = embedded.createEngine();
        engine.setDefaultHost("localhost");

        // Create a default virtual host
        host = embedded.createHost("localhost", tomcatPath + "/webapps");
        engine.addChild(host);

        // Create an application context
        Context context = embedded.createContext("TddJsfWeb", tomcatPath
                + "/webapps/TddJsfWeb");
        host.addChild(context);

        // Install the assembled container hierarchy
        embedded.addEngine(engine);

        // Assemble and install a default HTTP connector
        Connector connector = embedded.createConnector("localhost", 8080,
                "http");
        embedded.addConnector(connector);

        // Start the embedded server
        embedded.setAwait(true);
        embedded.start();

        WebClient webClient = new WebClient();
        webClient.getPage("http://localhost:8080/TddJsfWeb/static.xhtml");
    }
}

解压后的 .war 肯定位于 /Users/jbrains/ThirdParty/apache-tomcat-5.5.28-embed/webapps/TddJsfWeb/...static.xhtml 是在解压后的 .war 文件夹的根目录中。

拜托,拜托,让我看看我是多么愚蠢。谢谢。

OK, I'm completely stuck. I want to run Tomcat in embedded mode so I can test an application without running the server in a separate process. I'm missing something simple, stupid, and important, and I need your help to see it.

This test fails with an HTTP error 400, Bad Request. I've tried MemoryProtocolHandler, context.invoke(), ... I don't know what to do. Maybe you see something simple.

package ca.jbrains.jsfunit.learning.test;

import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.connector.Request;
import org.apache.catalina.realm.MemoryRealm;
import org.apache.catalina.startup.Embedded;
import org.junit.After;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.WebClient;

public class LearnEmbeddedTomcatTest {
    private Embedded embedded;
    private Container host;
    private Engine engine;

    @Test
    public void deploySampleApplicationFromFileSystem() throws Exception {
        String tomcatPath = "/Users/jbrains/ThirdParty/apache-tomcat-5.5.28-embed";

        // Create an embedded server
        embedded = new Embedded();
        embedded.setCatalinaHome(tomcatPath);
        embedded.setRealm(new MemoryRealm());

        // Create an engine
        engine = embedded.createEngine();
        engine.setDefaultHost("localhost");

        // Create a default virtual host
        host = embedded.createHost("localhost", tomcatPath + "/webapps");
        engine.addChild(host);

        // Create an application context
        Context context = embedded.createContext("TddJsfWeb", tomcatPath
                + "/webapps/TddJsfWeb");
        host.addChild(context);

        // Install the assembled container hierarchy
        embedded.addEngine(engine);

        // Assemble and install a default HTTP connector
        Connector connector = embedded.createConnector("localhost", 8080,
                "http");
        embedded.addConnector(connector);

        // Start the embedded server
        embedded.setAwait(true);
        embedded.start();

        WebClient webClient = new WebClient();
        webClient.getPage("http://localhost:8080/TddJsfWeb/static.xhtml");
    }
}

The unpacked .war is definitely at /Users/jbrains/ThirdParty/apache-tomcat-5.5.28-embed/webapps/TddJsfWeb/... and static.xhtml is in the root of the unpacked .war folder.

Please, please, show me how stupid I am. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作妖 2024-08-25 00:29:38

我在 Tomcat 上也有过类似的经历。我最终使用了 Jetty——从代码的角度来看管理起来要简单得多。

I had a similar experience with Tomcat. I ended up using Jetty instead - was a lot simpler to manage from a code point of view.

情定在深秋 2024-08-25 00:29:38

我想第二为此,Jetty 优于 Tomcat。要运行 Jetty,我需要的是:

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class StartJetty {
       public static void main(String[] args) throws Exception {
               Server server = new Server();
               SocketConnector connector = new SocketConnector();
               // Set some timeout options to make debugging easier.
               connector.setMaxIdleTime(1000 * 60 * 60);
               connector.setSoLingerTime(-1);
               connector.setPort(8081);
               server.setConnectors(new Connector[] { connector });

               WebAppContext bb = new WebAppContext();
               bb.setServer(server);
               bb.setContextPath("/");
               bb.setWar("src/main/webapp");
               server.addHandler(bb);

               try {
                       System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY
KEY TO STOP");
                       server.start();

                       //Launch browser
                       if (Desktop.isDesktopSupported())
                               if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE))
                                       try {
                                               Desktop.getDesktop().browse(new URI("http://localhost:8081/"));
                                       }
                                       catch (IOException ioe) {
                                               ioe.printStackTrace();
                                       }
                                       catch (URISyntaxException use) {
                                               use.printStackTrace();
                                       }

                       System.in.read();
                       System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
                       server.stop();
                       server.join();
               }
               catch (Exception e) {
                       e.printStackTrace();
                       System.exit(100);
               }
       }
}

I'd like to second Jetty over Tomcat for this.. To run Jetty all I need is:

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class StartJetty {
       public static void main(String[] args) throws Exception {
               Server server = new Server();
               SocketConnector connector = new SocketConnector();
               // Set some timeout options to make debugging easier.
               connector.setMaxIdleTime(1000 * 60 * 60);
               connector.setSoLingerTime(-1);
               connector.setPort(8081);
               server.setConnectors(new Connector[] { connector });

               WebAppContext bb = new WebAppContext();
               bb.setServer(server);
               bb.setContextPath("/");
               bb.setWar("src/main/webapp");
               server.addHandler(bb);

               try {
                       System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY
KEY TO STOP");
                       server.start();

                       //Launch browser
                       if (Desktop.isDesktopSupported())
                               if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE))
                                       try {
                                               Desktop.getDesktop().browse(new URI("http://localhost:8081/"));
                                       }
                                       catch (IOException ioe) {
                                               ioe.printStackTrace();
                                       }
                                       catch (URISyntaxException use) {
                                               use.printStackTrace();
                                       }

                       System.in.read();
                       System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
                       server.stop();
                       server.join();
               }
               catch (Exception e) {
                       e.printStackTrace();
                       System.exit(100);
               }
       }
}
你对谁都笑 2024-08-25 00:29:38

如果 Jetty 运行良好,那就太棒了,但如果您想更深入地了解 Tomcat,您可以在发出请求之前浏览一下您的应用程序,看看它们是否可用。
<代码>

for (Container cont : host.findChildren()) {
    if (cont instanceof StandardContext) {
        StandardContext ctx = (StandardContext) cont;
        ServletContext servletContext = ctx.getServletContext();
        log.debug("Context initialized: {}", servletContext.getContextPath());
        String prefix = servletContext.getRealPath("/");
        try {
            if (ctx.resourcesStart()) {
                log.debug("Resources started");
            }
            log.debug("Context - available: {} privileged: {}, start time: {}, reloadable: {}", new Object[] { ctx.getAvailable(), ctx.getPrivileged(), ctx.getStartTime(), ctx.getReloadable() });
...

A third option is to look into a much simpiler container than Tomcat or Jetty, such as Winstone: http://winstone.sourceforge.net/

If Jetty is working well for you then that is awesome, but if you want to dig a little deeper into Tomcat you can walk through your apps before you issue your request and see if they are available.

for (Container cont : host.findChildren()) {
    if (cont instanceof StandardContext) {
        StandardContext ctx = (StandardContext) cont;
        ServletContext servletContext = ctx.getServletContext();
        log.debug("Context initialized: {}", servletContext.getContextPath());
        String prefix = servletContext.getRealPath("/");
        try {
            if (ctx.resourcesStart()) {
                log.debug("Resources started");
            }
            log.debug("Context - available: {} privileged: {}, start time: {}, reloadable: {}", new Object[] { ctx.getAvailable(), ctx.getPrivileged(), ctx.getStartTime(), ctx.getReloadable() });
...


A third option is to look into a much simpiler container than Tomcat or Jetty, such as Winstone: http://winstone.sourceforge.net/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文