Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Jetty中部署简单web应用程序时遇到问题_Java_Jetty - Fatal编程技术网

Java 在Jetty中部署简单web应用程序时遇到问题

Java 在Jetty中部署简单web应用程序时遇到问题,java,jetty,Java,Jetty,我正在使用Jetty编写一个简单的服务器 SimpleServer.java: public class SimpleServer { public static void main(String[] args) throws Exception { Server server = new Server(8080); server.setHandler(new ServerHandler()); server.start();

我正在使用Jetty编写一个简单的服务器

SimpleServer.java:

public class SimpleServer {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        server.setHandler(new ServerHandler());
        server.start();
        server.join();
    }   

}
ServerHandler.java:

public class ServerHandler extends AbstractHandler {

    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        response.setContentType("text/html; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }
}

在控制台中,我得到一个
java.lang.reflect.InvocationTargetException
。此项目在IntelliJ中工作和运行。只有当我把它放在jetty/webapps文件夹中时,才会出现错误

您的示例代码库是使用Jetty API编写的

WAR文件需要的是,而不是Jetty处理程序API

如果您想继续使用Jetty
处理程序
API,那么您将继续使用
嵌入式Jetty
,在没有WAR文件的情况下完成所有工作

如果您想要WAR文件,那么您就不必使用Jetty
处理程序
API

将代码迁移到使用Servlet API,并在Web描述符中添加对它的引用(
Web-INF/Web.xml
)。这将生成一个可以部署的正确WAR文件

公共类ServerServlet扩展了HttpServlet{
public void doGet(HttpServletRequest请求、HttpServletResponse响应)
{
setContentType(“text/html;charset=utf-8”);
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(“Hello World”);
}
}

注意:您可以选择使用而不是
WEB-INF/WEB.xml
,但要知道您需要将
${jetty.base}
实例目录配置为启用
注释
模块。

发布您的WEB.xml好的,这很有意义。在阅读eclipse文档时,我没有注意到这一点。
HTTP ERROR 503

Problem accessing /. Reason:

    Service Unavailable