Eclipse – How to embed jetty to a dynamic web project using jsp in an eclipse IDE

eclipseembedded-jettyjarjsp

I am using an embedded jetty for my Wicket projects with no problem since wicket does not use JSP. Until I run across a task to create a basic Dynamic Web Application using an embedded jetty application server.

I know that by default jetty does not support JSPs. I'm also aware that there are several other Servlet containers to choose from. But for this task, I'd like to show how powerful and simple web development is using jetty since I have been using it with great success for larger projects.

I'm using Eclipse Helios as my IDE and added the following jars to my classpath

  • junit-4.8.1.jar
  • jetty-6.1.25.jar
  • jetty-management-6.1.25.jar
  • jetty-util-6.1.25.jar
  • log4j-1.2.14.jar
  • servlet-api-2.5-6.1.4.jar
  • jsp-2.1-6.1.5.jar
  • javax.servlet.jar

here is the code i use to start my embeded jetty

import org.mortbay.jetty.Connector;

import org.mortbay.jetty.Server;

import org.mortbay.jetty.bio.SocketConnector;

import org.mortbay.jetty.webapp.WebAppContext;

public class Start {

    public static void main(String[] args) throws Exception {
        Server server = new Server();
        SocketConnector connector = new SocketConnector();

        // Set some timeout options to make debugging easier.
        connector.setMaxIdleTime(1000 * 60 * 60);
        connector.setSoLingerTime(-1);
        connector.setPort(8080);
        server.setConnectors(new Connector[] { connector });

        WebAppContext bb = new WebAppContext();
        bb.setServer(server);
        bb.setContextPath("/");
        bb.setWar("WebContent");

        server.addHandler(bb);

        try {
            System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
            server.start();
            System.in.read();
            System.out.println(">>> STOPPING EMBEDDED JETTY SERVER"); 
            // while (System.in.available() == 0) {
            //   Thread.sleep(5000);
            // }
            server.stop();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(100);
        }
    }
}

And below is the error message

2011-02-11 09:18:12.953:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP
2011-02-11 09:18:13.000:INFO::jetty-6.1.25
2011-02-11 09:18:13.156:WARN::failed jsp: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext
2011-02-11 09:18:13.171:WARN::failed org.mortbay.jetty.webapp.WebAppContext@76cbf7{/,WebContent}: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext
2011-02-11 09:18:13.171:WARN::Error starting handlers
java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext
at org.apache.jasper.compiler.JspRuntimeContext.(JspRuntimeContext.java:103)
at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:134)
at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:263)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1272)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:489)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:224)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.pldt.embededserver.Start.main(Start.java:36)
2011-02-11 09:18:13.203:INFO::Started SocketConnector@0.0.0.0:8080

I hope you guys can help me with this. I feel so disappointed (with jetty's documentation with regards to this) that a very simple task like this turns out to be very confusing and time consuming due to poor documentation that even the simplest of all task (how to run a web application with JSP) would be so hard specially for people who wants to study java and considering jetty as their Servlet container.

Thanks in advance.

Best Answer

It looks like you're simply missing a couple of jar file.

Le me say though, that I think your complaints about documentation are out of place. Jetty works fine with JSP, and both standalone Jetty and the Jetty-Maven-plugin are designed to automatically include JSP support.

What you've chosen to do, is build you own server by embedding Jetty, and then you've failed to include the necessary jar files to make that work.
Embedding Jetty is an advanced use-case, and the documentation assumes that you know what you're doing.

Now, as to the problem you've run into...

Inside the Jetty distribution there is a lib/ directory.
Inside that lib/ directory there is a jsp-2.1 directory.
You need to include all the jar files from that directory on your classpath.
You seem to be missing

  • jsp-2.1-glassfish-2.1.v20091210.jar
  • jsp-api-2.1-glassfish-2.1.v20091210.jar
  • ant-1.6.5.jar
  • core-3.1.1.jar

(They might be slightly different versions in Jetty 6.1.25 - I pulled these from 6.1.26)