Created
June 10, 2011 11:25
-
-
Save monperrus/1018649 to your computer and use it in GitHub Desktop.
Running a standalone HTTP server with Jetty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import org.mortbay.jetty.Connector; | |
import org.mortbay.jetty.Server; | |
import org.mortbay.jetty.handler.HandlerList; | |
import org.mortbay.jetty.handler.ResourceHandler; | |
import org.mortbay.jetty.nio.SelectChannelConnector; | |
import org.mortbay.jetty.servlet.ServletHandler; | |
import org.mortbay.jetty.servlet.ServletHolder; | |
import org.mortbay.jetty.servlet.ServletMapping; | |
/** Starts a Jetty Server. | |
* Serves all servlets registered with registerServlet. | |
* Also Serves the static content in the current working directory. | |
* | |
* Usage: | |
* JettyServer.init(); | |
* JettyServer.registerServlet(foo.CommentingServlet.class, "/c/*"); | |
* JettyServer.start(); | |
*/ | |
public class JettyServer { | |
/** Tests the server */ | |
public static void main(String[] args) throws Exception { | |
JettyServer.init(); | |
// TestServlet contains (the class must be public) | |
// public class TestServlet extends HttpServlet { | |
// @Override | |
// protected void doGet(HttpServletRequest req, HttpServletResponse resp) | |
// throws ServletException, IOException { | |
// resp.getWriter().write(new Date().toString());} | |
// } | |
JettyServer.registerServlet(TestServlet.class, "/date"); | |
JettyServer.start(); | |
} | |
// all Jetty objects required to start | |
private static final SelectChannelConnector connector = new SelectChannelConnector(); | |
private static final Server server = new Server(); | |
private static final HandlerList mainHandler = new HandlerList(); | |
private static int port = 8080; | |
private static ServletHandler servletHandler = new ServletHandler(); | |
// local lists storing the servlets | |
private static final List<ServletHolder> holders = new ArrayList<ServletHolder>(); | |
private static final List<ServletMapping> mappings = new ArrayList<ServletMapping>(); | |
// protocol bits | |
private static boolean initAlreadyCalled = false; | |
/** Initializes the server */ | |
public static void init() throws Exception { | |
if (initAlreadyCalled) { | |
throw new RuntimeException("init() already called"); | |
} | |
// init config | |
connector.setPort(port); | |
Connector[] c = { connector }; | |
server.setConnectors(c); | |
// serving static files | |
// "./src/JettyServer.java" is accessible at "http://localhost:8080/src/JettyServer.java" | |
ResourceHandler resourceHandler = new ResourceHandler(); | |
resourceHandler.setResourceBase("./"); | |
// recall that Jetty takes into account | |
// the first handler which matches the requested URI | |
mainHandler.addHandler(resourceHandler); | |
mainHandler.addHandler(servletHandler); | |
server.setHandler(mainHandler); | |
initAlreadyCalled = true; | |
} | |
/** Starts the server */ | |
public static void start() throws Exception { | |
if (!initAlreadyCalled) { | |
throw new RuntimeException("init() must be called before start()"); | |
} | |
servletHandler.setServlets(holders.toArray(new ServletHolder[0])); | |
servletHandler.setServletMappings(mappings.toArray(new ServletMapping[0])); | |
server.start(); | |
} | |
/** Registers a servlet in the server on the given path. | |
* Example: registerServlet(foo.CommentingServlet.class, "/c/*"); | |
* */ | |
public static void registerServlet(Class servlet, String path) { | |
if (!initAlreadyCalled) { | |
throw new RuntimeException("init() must be called before registerServlet()"); | |
} | |
ServletHolder holder = new ServletHolder(); | |
holder.setName(servlet.getName()); | |
holder.setClassName(servlet.getName()); | |
holders.add(holder); | |
ServletMapping mapping = new ServletMapping(); | |
mapping.setPathSpec(path); | |
mapping.setServletName(servlet.getName()); | |
mappings.add(mapping); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.Date; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
public class TestServlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | |
resp.getWriter().write(new Date().toString());} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Translated to Scala...:
package org.dorest.server
package servlet
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Date;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.HandlerList;
import org.mortbay.jetty.handler.ResourceHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.ServletMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
After the start go to: "http://localhost:8080/date"
*/
class JettyServer
object JettyServer extends App {
// all Jetty objects required to start
val connector = new SelectChannelConnector();
val server = new Server();
val mainHandler = new HandlerList();
val port = 8080;
val servletHandler = new ServletHandler();
// local lists storing the servlets
val holders = new ArrayListServletHolder;
val mappings = new ArrayListServletMapping;
// protocol bits
var initAlreadyCalled = false;
/** Initializes the server */
def init() {
if (initAlreadyCalled) {
throw new RuntimeException("init() already called");
}
}
/** Starts the server */
def start() {
if (!initAlreadyCalled) {
throw new RuntimeException("init() must be called before start()");
}
servletHandler.setServlets(holders.toArray(new ArrayServletHolder));
servletHandler.setServletMappings(mappings.toArray(new ArrayServletMapping));
server.start();
}
def registerServlet(servlet: java.lang.Class[_], path: String) {
if (!initAlreadyCalled) {
throw new RuntimeException("init() must be called before registerServlet()");
}
val holder = new ServletHolder();
holder.setName(servlet.getName());
holder.setClassName(servlet.getName());
holders.add(holder);
val mapping = new ServletMapping();
mapping.setPathSpec(path);
mapping.setServletName(servlet.getName());
mappings.add(mapping);
}
init();
registerServlet(classOf[TestServlet], "/date");
start();
}
class TestServlet extends HttpServlet {
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
resp.getWriter().write(new Date().toString());
}
}