栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用web.xml配置嵌入式码头吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用web.xml配置嵌入式码头吗?

用一个

org.eclipse.jetty.webapp.WebAppContext

例:

package jetty;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.webapp.WebAppContext;public class OnWebApp{    public static void main(String[] args) throws Exception    {        // Create a basic jetty server object that will listen on port 8080.        // Note that if you set this to port 0 then a randomly available port        // will be assigned that you can either look in the logs for the port,        // or programmatically obtain it for use in test cases.        Server server = new Server(8080);        // The WebAppContext is the entity that controls the environment in        // which a web application lives and breathes. In this example the        // context path is being set to "/" so it is suitable for serving        // root context requests and then we see it setting the location of        // the war. A whole host of other configurations are available,        // ranging from configuring to support annotation scanning in the        // webapp (through PlusConfiguration) to choosing where the webapp        // will unpack itself.        WebAppContext webapp = new WebAppContext();        webapp.setContextPath("/");        webapp.setWar("path/to/my/test.war");        // A WebAppContext is a ContextHandler as well so it needs to be set to        // the server so it is aware of where to send the appropriate requests.        server.setHandler(webapp);        // Start things up! By using the server.join() the server thread will        // join with the current thread.        // See http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()        // for more details.        server.start();        server.join();    }}

请注意,您将构建一个普通的WAR文件,并将其与Jetty一起使用。

如果您有特殊要求,例如注释扫描或JNDI,则需要进入配置规范。

// Enable parsing of jndi-related parts of web.xml and jetty-env.xmlorg.eclipse.jetty.webapp.Configuration.ClassList classlist =   org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);// Enable JNDIclasslist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",   "org.eclipse.jetty.plus.webapp.EnvConfiguration",   "org.eclipse.jetty.plus.webapp.PlusConfiguration");// Enable Annotation Scanningclasslist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",  "org.eclipse.jetty.annotations.AnnotationConfiguration");

有关WebAppContext中更长的示例,请参阅ServerWithAnnotations示例。

还要注意,您还将使用此技术将所有webapp类加载器规则都放在适当的位置。这意味着您将为webapp提供一个类加载器,为服务器提供一个类加载器。了解这一点很重要。

您可以对类加载器的WebAppContext进行一些调整,但是您不能消除它们,而只能控制它们的行为。

WebAppContext webapp = new WebAppContext();// ... various setup of the webapp ...// Flip the classloader priority from servlet spec where webapp is first to// Standard java behavior of parent (aka Server classloader) is first.webapp.setParentLoaderPriority(true);

也可以看看:

  • WebAppContext.setClassLoader(ClassLoader classloader)
  • WebAppContext.addServerClass(String classOrPackage)
  • WebAppContext.addSystemClass(String classOrPackage)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595609.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号