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\"
\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"tr\">
<head>
<title>Welcome</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<script type=\"text/javascript\">
</script>
</head>
<body>
Merhaba
</body>
</html>


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;
try
{
jettyServer = new Server();
SocketListener socketListener = new SocketListener();
socketListener.setPort(8080);
jettyServer.addListener(socketListener);
jettyServer.addWebApplication("/test", "resources");
jettyServer.start();
}
catch (Exception e) {
log.info("Could not start the Jetty server: " + e);
if (jettyServer != null) {
try { jettyServer.stop();
} catch (InterruptedException e1) { log.info("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: