0

I write a jettyserver.java as start service,here is the code,

public class jettyserver {
public static void main(String[] args) throws Exception {
    Server server = new Server(8080); 
    ServletHolder servletHolder = new ServletHolder(ServletContainer.class);
    Map<String, String> parameterMap = new HashMap<String, String>();
    parameterMap.put("jersey.config.server.provider.packages", "com.heu.cs.mavenproject3");
    servletHolder.setInitParameters(parameterMap); 
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);  
    context.setContextPath("/jettyproject/*");
    context.addServlet(servletHolder, "/*");
    server.setHandler(context);
    server.start();
    server.join();
    }
}

And I write a demo.java as a test demo,

@Path("/demo")
public class demo {
@GET
@Produces({"text/html"})
public String index(){
    return "OK";
    }
}

and then i run jettyserver.java,I got OK like:

this

we can see OK,that's right but wen I enter localhost:8080/jettyproject/index.html or localhost:8080/index.html in the browser, I got a 404 error like this:

404 Error

what should I do ? What'wrong with the code????

Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
Meng.Qi
  • 13
  • 1
  • 5
  • Try changing `addServlet(servletHolder, "/*");` to `addServlet(servletHolder, "/api/*");`. If you don't want to change the path base path for Jersey, then you need to see [these answers](http://stackoverflow.com/q/12422660/2587435) – Paul Samsotha May 14 '17 at 11:30
  • Thank u , I deal with this,but another problem occur. When I run it in IDEA,it worked well, static file and dyanmic servlet worked well. I package a war file ,and put it in jetty webapps directory ,and I start jetty,but only the static file worked well , what happend and what should I do? Could help me please,Thank u very much. U can see the quetion in http://stackoverflow.com/questions/44017121/deploy-a-embedded-jettyjersey-war-to-jetty9-only-can-see-static-file – Meng.Qi May 17 '17 at 06:27

0 Answers0