Java Embedded Web Server (with source code)

Source: Internet
Author: User

Many Java developers use Tomcat as the main server. Tomcat undoubtedly has many advantages. Tomcat is also the latest jdk api with the best support and relatively high stability. However, Tomcat's weakness is also obvious. After all, Tomcat is not a pure Java Server. to interact with tomcat, Java must use the physical path or related interfaces. Tomcat is very troublesome to use.

I believe many people have also used the ser-U FTP server, which is a well-established but very stable server and is very enterprising. In later versions of this server, their console was upgraded from the original CS mode to the BS mode, which can be said to be a revolutionary evolution. The BS mode console is easier to upgrade and optimize than the CS mode. They can use BS as the console, but do not deploy related application servers, that is, they have embedded Java servers.

In this article, the application-type Java Server mainly uses the Java main method to start an embedded application server platform and listen to a port, to continuously provide application services to local users or local area network users. What services you deploy depends on what applications you deploy. ,

Core classes of embedded servers

Package COM. shine. framework. httpserver; </P> <p> Import Org. mortbay. jetty. connector; <br/> Import Org. mortbay. jetty. server; <br/> Import Org. mortbay. jetty. NIO. selectchannelconnector; <br/> Import Org. mortbay. jetty. webapp. webappcontext; <br/> Import Org. mortbay. thread. boundedthreadpool; </P> <p>/** <br/> * httpservermanager <br/> * related packages <br/> * <br/> * @ author viruscodecn@gmail.com <br/> * @ P Roject javaframework 1.0 <br/> */<br/> public class httpservermanager {<br/> private server; </P> <p>/** <br/> * initialize the war imported from the server, the default connection count is 100 <br/> * @ Param contextname <br/> * @ Param warpath <br/> * @ Param port <br/> */< br/> Public void initjettyhttpserverbywar (string contextname, string warpath, <br/> string port) {<br/> initjettyhttpserverbywar (contextname, warpath, port, 100); <BR/>}</P> <p>/** <br/> * initialize the war imported from the server <br/> * @ Param contextname <br/> * @ Param warpath <br/> * @ Param port <br/> * @ Param threadpoolnum <br/> */<br/> @ suppresswarnings ("deprecation ") <br/> Public void initjettyhttpserverbywar (string contextname, string warpath, <br/> string port, int threadpoolnum) {<br/> If (server! = NULL & server. isrunning () {<br/> system. err. println ("Disable the HTTP server and restart it"); <br/> return; <br/>}</P> <p> try {<br/> Server = new server (); <br/> boundedthreadpool threadpool = new boundedthreadpool (); <br/> // set the thread pool <br/> threadpool. setmaxthreads (threadpoolnum); <br/> server. setthreadpool (threadpool); <br/> // set the connection parameter <br/> connector conne= new selectchannelconne (); <br/> // set the listening port <br/> connector. setpo RT (integer. parseint (port); <br/> server. setconnectors (new connector [] {connector}); <br/> webappcontext context = new webappcontext (); <br/> // access the Project address <br/> context. setcontextpath (contextname); <br/> // The started war package <br/> context. setwar (warpath); <br/> server. addhandler (context); <br/> server. setstopatshutdown (true); <br/> server. setsendserverversion (true); </P> <p> server. start (); <br/> server. join (); <br/>} Ca Tch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p>/** <br/> * initialize the server through the project path, the default thread pool is 100 <br/> * @ Param contextname <br/> * @ Param webpath <br/> * @ Param port <br/> */ <br/> @ suppresswarnings ("deprecation ") <br/> Public void initjettyhttpserver (string contextname, string webpath, <br/> string port) {<br/> initjettyhttpserver (contextname, webpath, port, 100 ); <br/>}</P> <p>/** <br/> * Initialize the server through the project path <br/> * @ Param contextname <br/> * @ Param webpath <br/> * @ Param port <br/> * /<br/> @ suppresswarnings ("deprecation ") <br/> Public void initjettyhttpserver (string contextname, string webpath, <br/> string port, int threadpoolnum) {<br/> If (server! = NULL & server. isrunning () {<br/> system. err. println ("Disable the HTTP server and restart it"); <br/> return; <br/>}</P> <p> try {<br/> Server = new server (); <br/> boundedthreadpool threadpool = new boundedthreadpool (); <br/> // set the thread pool <br/> threadpool. setmaxthreads (threadpoolnum); <br/> server. setthreadpool (threadpool); <br/> // set the connection parameter <br/> connector conne= new selectchannelconne (); <br/> // set the listening port <br/> connector. setpo RT (integer. parseint (port); <br/> server. setconnectors (new connector [] {connector}); <br/> webappcontext context = new webappcontext (webpath, contextname); <br/> server. addhandler (context); <br/> server. setstopatshutdown (true); <br/> server. setsendserverversion (true); </P> <p> server. start (); <br/> server. join (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public void shutd Ownjettyhttpserver () {<br/> If (Server = NULL) {<br/> system. err. println ("HTTP is not initialized and then restarted"); <br/> return; <br/>}</P> <p> try {<br/> If (server. isrunning () <br/> server. stop (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public void restartjettyhttpserver () {<br/> If (Server = NULL) {<br/> system. err. println ("HTTP is not initialized and then restarted"); <br/> return; <br/>}</P> <p> try {<br/> If (! Server. isrunning () <br/> server. start (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p >}< br/>

 

Call class

Package COM. shine. framework. httpserver; </P> <p> public class example {<br/> Public static void main (string ARGs []) {<br/> httpservermanager manager = new httpservermanager (); <br/> // manager. initjettyhttpserverbywar ("/managesystemflex", <br/> // "C: // users // 123 // desktop // managesystemflex. war "," 8080 "); </P> <p> manager. initjettyhttpserver ("/managesystemflex", <br/> "E: // workspace // managesystemflex // webcontent", "8080 "); <br/>}< br/>

 

Initjettyhttpserverbywar is a Web application that calls a war package.Program;

The initjettyhttpserver method calls a webcontent web application;

Further Study on the specific usage

 

RelatedSource codeLocation: http://code.google.com/p/ken-javaframeword/source/browse/trunk/JavaFramework2.0/src/com/shine/framework/HtmlManager/HtmlManager.java

If you have other ideas, leave me a message.

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.