Reputation: 710
I am using Jetty to write a simple server.
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>");
}
}
I use maven to package the files up into a war file and then throw them in the jetty/webapps directory. I have the war file named root.war, so that means the context is at 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
. This project works and runs in IntelliJ. It's only when I put it in the jetty/webapps folder that I get errors.
Upvotes: 0
Views: 312
Reputation: 49452
Your example codebase is written using the Jetty Handler
API.
A WAR file expects the Servlet API, not the 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.
If you want a WAR file, then you'll have to not use the 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
). That will produce a proper WAR file that can be deployed.
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.
Upvotes: 1