开发者

Getting started with embedded Jetty

I just got started with embedded jetty. I'm stuck at some error messages. It's simple and straightforward few lines code, which I found online and wanted to test out.

 import org.jaxen.Context;
 import org.mortbay.jetty.Server;
 import org.mortbay.jetty.servlet.ServletHolder;

 public class Main {

    public static void main(String[] args) throws Exception {

        ServletHolder sh = new ServletHolder(ServletContainer.class);
        sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
        sh.setInitParameter("com.sun.jersey.config.property.packages", "jerseyplusjetty");
        Server server = new Server(80);
        ServletContextHandler sch = new ServletContextHandler(server, "/");
        sch.addServlet(sh, "/*");
        server.start();
        server.join();

    }
}

I have all jetty jars in java build path. But I kept getting errors: The constructor ServletHolder(Class) is undefined, The constructor Server(int) is undefined, ServletContextHandler cannot be resolved to a type. If I remove the parameter inside ServletHolder and Server, it stops complaining. e.g. if I have: ServletHolder sh = new ServletHolder(); Server server = new Server(); But that's not right. I开发者_Python百科 read Jetty docs and ServletHolder class can take parameters. Am I missing something here?


Just FYI on embedded Jetty in general... I have created a github project that I humbly submit may cover most of the embedded jetty issues that keep cropping up.

I've got examples for AbstractHandlers, Servlets, Jersey Servlets, static files, webapps and what not. Still working on RoR and Sinatra, but will get there.

See https://github.com/ZenGirl/EmbeddedJettyRepository for details. Anyone want to contribute, just ask.


The version of ServletHolder I have takes a String or a servlet in the constructor. So instead of doing

new ServletHolder(ServletContainer.class) you should do new ServletHolder(ServletContainer.class.getCanonicalName()) or new ServletHolder(new ServletContainer()).

ServletContainer is a strange name for a servlet, make sure it is actually a servlet.

Also, be aware that there are number of different versions of Jetty (you're using an old one because in the new one all the classes are in org.eclipse.jetty package), and it's easy to pick up example code that refers to a different version to the one you've got. I would get jetty 7.2.2 from maven and use the example code here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜