All Downloads are FREE. Search and download functionalities are using the official Maven repository.

li.rudin.mavenjs.server.HTTPServer Maven / Gradle / Ivy

The newest version!
package li.rudin.mavenjs.server;

import javax.servlet.http.HttpServlet;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class HTTPServer
{
	/**
	 * Creates a new http server on given port
	 * @param port
	 */
	public HTTPServer(int port)
	{
		handler = new ServletContextHandler();
		server = new Server(port);
		server.setHandler(handler);
		
		this.port = port;
	}
	
	private final int port;
	
	/**
	 * Maps a servlet to given path
	 * @param servlet
	 * @param path
	 */
	public void map(Class servlet, String path)
	{
		handler.addServlet(servlet, path);
	}
	
	/**
	 * Maps a servlet holder to give path
	 * @param holder
	 * @param path
	 */
	public void map(ServletHolder holder, String path)
	{
		handler.addServlet(holder, path);
	}
	
	private final Server server;
	private final ServletContextHandler handler;
	
	/**
	 * Starts the server
	 * @throws Exception
	 */
	public void start() throws Exception
	{
		server.start();
	}
	
	/**
	 * Stops the server
	 * @throws Exception
	 */
	public void stop() throws Exception
	{
		server.stop();
	}

	public int getPort()
	{
		return port;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy