是否可以使用以下命令完全启动webapp应用程序:
1)无war文件:
http://stephenh.github.io/2009/01/10/war-less-dev-with-jetty.html http://www.jamesward.com/2011/08/23/war-less-java-web-apps
2)无web.xml (即Servlet-3.0)
3)从嵌入式web容器(例如,Tomcat或Jetty...)
发布于 2013-04-24 12:02:55
我是如何做到的(嵌入式SpringMVC + Jetty,没有web.xml,没有war文件):
使用Spring的@WebAppConfiguration通过MockServletContext引导您的WebApplicationContext,
然后,只需通过Jetty的ServletContextHandler/ServletHolder机制注册您的new DispatcherServlet(WebApplicationContext)。简单!
发布于 2013-04-09 21:16:08
示例项目:https://github.com/jetty-project/embedded-servlet-3.0
你仍然需要一个WEB-INF/web.xml,但是it can be empty。这样就可以知道servlet支持级别和元数据完成标志。
示例:空Servlet 3.0 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
</web-app>然后,您可以按照EmbedMe.java中的示例进行设置。
public class EmbedMe {
public static void main(String[] args) throws Exception {
int port = 8080;
Server server = new Server(port);
String wardir = "target/sample-webapp-1-SNAPSHOT";
WebAppContext context = new WebAppContext();
context.setResourceBase(wardir);
context.setDescriptor(wardir + "WEB-INF/web.xml");
context.setConfigurations(new Configuration[] {
new AnnotationConfiguration(), new WebXmlConfiguration(),
new WebInfConfiguration(), new TagLibConfiguration(),
new PlusConfiguration(), new MetaInfConfiguration(),
new FragmentConfiguration(), new EnvConfiguration() });
context.setContextPath("/");
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();
server.join();
}
}发布于 2017-04-01 23:07:02
可以使用Jetty实现不需要WAR或任何XML的嵌入式服务器。您只需指定带注释的类的位置,添加额外的类路径即可。
此方法应从您可以命名为Server.java的main中调用
private static void startServer() throws Exception {
final org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(7070);
final WebAppContext context = new WebAppContext("/", "/");
context.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebInfConfiguration() });
context.setExtraClasspath("build/classes/main/com/example/servlet");
server.setHandler(context);
server.start();
server.join();
}我的src结构是:
-main
-java
-com.example
-json
-servlet
-filter
-util
Server.java我希望看到Tomcat也有类似的解决方案。
https://stackoverflow.com/questions/15893982
复制相似问题