此测试失败,出现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文件夹的根目录中。
拜托,请告诉我我有多愚蠢。感谢。
答案 0 :(得分:1)
我和Tomcat有类似的经历。我最终使用了Jetty - 从代码的角度来管理起来要简单得多。
答案 1 :(得分:0)
我希望second Jetty over Tomcat for this ..要运行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);
}
}
}
答案 2 :(得分:0)
如果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() });
...
第三种选择是查看比Tomcat或Jetty更简单的simpiler容器,例如Winstone:http://winstone.sourceforge.net/