嵌入 Tomcat 并向已部署的应用程序发出请求
好吧,我完全被困住了。我想以嵌入式模式运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 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.
我想第二为此,Jetty 优于 Tomcat。要运行 Jetty,我需要的是:
I'd like to second Jetty over Tomcat for this.. To run Jetty all I need is:
如果 Jetty 运行良好,那就太棒了,但如果您想更深入地了解 Tomcat,您可以在发出请求之前浏览一下您的应用程序,看看它们是否可用。
<代码>
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.
A third option is to look into a much simpiler container than Tomcat or Jetty, such as Winstone: http://winstone.sourceforge.net/