[英]Having trouble deploying simple web app in Jetty
I am using Jetty to write a simple server. 我正在使用Jetty编写一个简单的服务器。
SimpleServer.java: 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: 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>");
}
}
I use maven to package the files up into a war file and then throw them in the jetty/webapps directory. 我使用maven将文件打包成war文件,然后将它们扔到jetty / webapps目录中。 I have the war file named root.war, so that means the context is at localhost:8080/.
我有一个名为root.war的war文件,因此这意味着上下文位于localhost:8080 /。 But when I access it, I get the following error in the browser:
但是,当我访问它时,在浏览器中出现以下错误:
HTTP ERROR 503
Problem accessing /. Reason:
Service Unavailable
In the console, I get a java.lang.reflect.InvocationTargetException
. 在控制台中,我得到了一个
java.lang.reflect.InvocationTargetException
。 This project works and runs in IntelliJ. 该项目在IntelliJ中运行。 It's only when I put it in the jetty/webapps folder that I get errors.
仅当我将其放在jetty / webapps文件夹中时,我才会出错。
Your example codebase is written using the Jetty Handler
API. 您的示例代码库是使用Jetty
Handler
API编写的。
A WAR file expects the Servlet API , not the Jetty Handler API. WAR文件需要Servlet API ,而不是Jetty Handler API。
If you want to continue using the Jetty Handler
API, then you'll be staying with embedded-jetty
and doing everything without a WAR file. 如果您想继续使用Jetty
Handler
API,那么您将继续使用embedded-jetty
并在没有WAR文件的情况下进行所有操作。
If you want a WAR file, then you'll have to not use the Jetty Handler
API. 如果您需要WAR文件,则不必使用Jetty
Handler
API。
Migrate your code to using the Servlet API, and add a reference to it in your Web Descriptor ( WEB-INF/web.xml
). 将代码迁移为使用Servlet API,并在Web描述符(
WEB-INF/web.xml
)中添加对它的引用。 That will produce a proper WAR file that can be deployed. 这将产生可以部署的适当的WAR文件。
public class ServerServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/html; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello World</h1>");
}
}
Note: You can optionally, you can use the Servlet API Annotations instead of the WEB-INF/web.xml
, but know that you'll want your ${jetty.base}
instance directory configured to have the annotations
module enabled. 注意:您可以选择使用Servlet API注释,而不是
WEB-INF/web.xml
,但是知道您希望将${jetty.base}
实例目录配置为启用annotations
模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.