gpt4 book ai didi

java - 使用 web.xml 以编程方式启动 jetty

转载 作者:搜寻专家 更新时间:2023-10-30 20:02:09 25 4
gpt4 key购买 nike

我创建了一个 eclipse maven 项目并添加了 jetty 依赖项。接下来我制作了一个简单的 servlet 和一个启动 jetty 服务器的类。这是我到目前为止得到的:

package com.example.jetty;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class App {
public static void main(String[] args) throws Exception {
Server server = new Server(80);
ServletContextHandler servletContext = new ServletContextHandler(server, "/");
servletContext.addServlet(MyServlet.class, "/");
server.start();
}
}

我的问题是我看到的大多数教程都有一个 web.xml 来配置 servlet 等。我找不到执行其中一些操作的编程方法。我可以创建一个 web.xml 并仍然以编程方式启动我的 jetty 并以某种方式使用该 web.xml 进行配置吗?

更具体地说,我需要在 web.xml 中写入 true。我没有找到任何编程方式。

最佳答案

我将从一个您可能感兴趣的示例开始。如果您想以编程方式使用 web.xml Jetty 服务器,那么您可以执行以下操作:

WebAppContext context = new WebAppContext();
context.setContextPath("/myWebApp");
context.setExtractWAR(false);
context.setDescriptor("/file/system/path/to/your/wab/app/WEB-INF/web.xml");
context.setResourceBase("/file/system/path/to/your/wab/app");
context.setConfigurationDiscovered(false);

HandlerList handlerList=new HandlerList();
handlerList.addHandler(webAppContext);

Server server = new Server(threadPool);
server.setHandler(handlerList);
server.start();

关于以编程方式配置,您可以尝试使用 Servlet 3.x API,它受 Jetty 8.x 支持(当前 Jetty 版本 9.x),并且可以通过编程方式进行完全配置。

关于java - 使用 web.xml 以编程方式启动 jetty ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40045500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com