Sunday, March 19, 2006

Very Simple Jetty Configuration

I like Jetty as a web server. It is pretty small, fast and easy to configure. Here is my very simple web application configuration on jetty 4.2.2 (you can try it on any java ide):

1. Put the following files into the classpath: jasper-compiler.jar, jasper-runtime.jar, javax.servlet.jar, org.apache.jasper.jar, org.mortbay.jetty.jar (these files are included in jetty 4.2.x distributions)

2. Create a web page welcome.html and put it into {project.dir}/resources folder.

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
<html xmlns=\"\" xml:lang=\"tr\">
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<script type=\"text/javascript\">

3. Compile and run the following java class (the working folder should be {project.dir}):

public class Start {
private static Logger log = Logger.getLogger("start");
public static void main(String[] args) {
Server jettyServer = null;
jettyServer = new Server();
SocketListener socketListener = new SocketListener();
jettyServer.addWebApplication("/test", "resources");
catch (Exception e) {"Could not start the Jetty server: " + e);
if (jettyServer != null) {
try { jettyServer.stop();
} catch (InterruptedException e1) {"Unable to stop the jetty server: " + e1); }

4. Go to http://localhost:8080/test/welcome.html

Here you can download the code as an IntelliJ Idea project, excluding the jetty-4.2.x distribution.

No comments: