Minimum package required:
Commons-logging.jar
Javax. servlet. Jar
Org. mortbay. Jetty. Jar
Org. mortbay. JMX. Jar
// Code: Start jetty in Embedded Mode
Import org. mortbay. http. httpcontext;
Import org. mortbay. http. httpserver;
Import org. mortbay. http. socketlistener;
Import org. mortbay. http. handler. resourcehandler;
Public class jettysample {
Public static void main (string [] ARGs) throws exception
{
// Create a jetty httpserver object
Httpserver Server = new httpserver ();
// Bind a listener to the httpserver object on port 8080 so that it can receive HTTP requests
Socketlistener listener = new socketlistener ();
Listener. setport (8080 );
Server. addlistener (listener );
// Create an httpcontext to process HTTP requests.
Httpcontext context = new httpcontext ();
// Use setcontextpath to map context to the (/Web) URL.
Context. setcontextpath ("/Web ");
// Setresourcebase method sets the document directory to provide resources
Context. setresourcebase ("C: // j2sdk1.4.1 _ 05 ");
// Add a resource processor to httpcontext to provide files in the file system
Context. addhandler (New resourcehandler ());
Server. addcontext (context );
// Start the server
Server. Start ();
}
}
Jetty runs quickly and is lightweight. It can be controlled from test case in Java. Therefore, automated testing can be smoothly implemented without relying on external environments.