Embedding jetty in Eclipse projects

Source: Internet
Author: User

  Jetty is an open source project that provides HHTP servers, HTTP clients, and Javax.servlet containers, and Jetty is now a more favored Servlet engine, with a simpler architecture and a scalable and flexible application server. It has a basic data model, the data model is Handler, all the components that can be expanded as a Handler, added to the Server, Jetty is to help you manage these Handler.

Recently work needs to integrate jetty in the project, because has never used jeety, so I spent more than half a day to learn, the basic implementation of jetty embedded integration, I built a simple SPRINGMVC framework, Simple implementation of the controller request to jump JSP page small function, where the creation of SPRINGMVC is not described here. No, you can go to the Internet to find information. The project structure is as follows:

  

First, import the corresponding jar package in the project as follows:

Jetty-all-8.1.17.v20150415.jar
Servlet-api.jar
Ant-1.6.5.jar
Core-3.1.1.jar
Jsp-2.1.jar
Jsp-api-2.1.jar
Ant-xmltask.jar
Jetty-util-6.1.9.jar

Next, create a portal method to start the jetty with the following code:

/*** Start Web App in built-in jetty mode * Tip: Please start with Debug as Java application*/ Public classAOS {/*** Start the built-in server * *@paramargs *@throwsException*/     Public Static voidMain (string[] args)throwsException {aosserver aosserver=NewAOSServer (); Aosserver.setwebcontext ("/testjetty"); The context name for the project launch Aosserver.setport (10010); The port number of the service Aosserver.start (); Start Service}}

Finally write the specific jetty startup method, the code is as follows:

/*** <b> embedded servlet container based on jetty </b> * *@authorATOM.WU * @date 2008-06-06 *@since1.0*/ Public classAOSServer {Private StaticLogger log = Loggerfactory.getlogger (aosserver.class); /*** Listening port, default is*/    Private intPort = 80; /*** Application Context, default =/(no context)*/    PrivateString Webcontext = "/";  PublicAOSServer () {}/*** Construct Server instance * *@paramPwebcontext *@paramPport*/     PublicAOSServer (String Pwebcontext,intpport)        {Setwebcontext (Pwebcontext);    Setport (Pport); }     Public intGetport () {returnPort; }    /*** Listening port, default is * *@paramPort*/     Public voidSetport (intPort) {         This. Port =Port; }     PublicString Getwebcontext () {returnWebcontext; }    /*** Application Context, default =/(no context) * *@paramWebcontext*/     Public voidSetwebcontext (String webcontext) { This. Webcontext =Webcontext; }        /*** Start Jetty container*/     Public voidStart ()throwsException {LongStart =System.currenttimemillis (); FinalString webRoot = System.getproperty ("User.dir") + "/webcontent";//engineering Path Server server=NewServer (); //sets the hook to close jetty when the JVM exits. Server.setstopatshutdown (true); Selectchannelconnector connector=NewSelectchannelconnector (); //disable NIO cache to unlock the CSS and JS file when runningConnector.setusedirectbuffers (false); //resolves an issue in which a Windows recurring startup jetty unexpectedly does not report port conflicts.Connector.setreuseaddress (false);        Connector.setport (port); Server.setconnectors (Newconnector[]{connector}); Webappcontext Context=NewWebappcontext (); Context.setresourcebase ("WebContent");        Context.setcontextpath (Webcontext); //Set form submission size (default: 200000)Context.setmaxformcontentsize (10000000); Context.setparentloaderpriority (true); //scan TLD files for special settings for jetty using Jstl. Specifies which jars may contain TLDs. Context.setattribute ("Org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ". */.*jsp-api-[^/]*\\.jar$|. */.*jsp-[^/]*\\.jar$|. */.*taglibs[^/]*\\.jar$|. */.*jstl[^/]*\\.jar$ ");                        Server.sethandler (context); BooleanIssuccess =true; Try{Server.start (); } Catch(bindexception e) {issuccess=false;}Catch(Exception e) {issuccess=false;}finally{String msg = "Sa-web started successfully"; String supportmsg= " "; if( !issuccess) {msg= "Sa-web failed to start";                Log.error (msg); Msg= msg +supportmsg;                SYSTEM.OUT.PRINTLN (msg); System.exit (0); }Else{Toolkit Toolkit=Toolkit.getdefaulttoolkit (); Clipboard Clipboard=Toolkit.getsystemclipboard (); Webcontext= Webcontext.equals ("/")? "": Webcontext; String WEBURL= "http://localhost"; if(Port = = 80) {WebUrl= WebUrl +Webcontext; }Else{WEBURL= WebUrl + ":" + port +Webcontext; } stringselection Stringsel=Newstringselection (WEBURL); Clipboard.setcontents (Stringsel,NULL); LongAlltime = System.currenttimemillis ()-start; Msg= msg + "[" + Alltime + "msec]" + ">>" + WebUrl +supportmsg;                SYSTEM.OUT.PRINTLN (msg); Server.join (); //Thread Blocking            }        }    }

Start the jetty service.

Browser Open Address:

  

Embedding jetty in Eclipse projects

Related Article

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.