Use jetty as an Embedded Server

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.