It has been a long time since the beginning of the article. During this period, the previous versions have been improved and can be fully run now. Basically, all the functions are available. In addition, the original test program is changed to a sample project, you only need to implement controller and view for the new project according to the sample project structure. For details, see easy-httpserver and demo-httpsrever.
I. Preparations
4.0.0 learn-1 learn-1 0.0.1-SNAPSHOT jar learn-1 http://maven.apache.org UTF-8 commons-logging 1.1.3 junit 4.10 test easy-httpserver $ {basedir}/src/main/viewPom. xml
Log log = LogFactory. getLog (EHServer. startServer () "Starting EHServer ...... "Constants. CLASS_PATH =. getClass (). getResource ("/" = "page" = "static" port = 8899 HttpServerProvider provider = provider. createHttpServer (InetSocketAddress (port), 100 usd/", "EHServer is started, listening at 8899. "main (String [] args)View Code
3. ImplementationEHHttpserver, controller, and viewHandler
Handle (HttpExchange httpExchange) = "Receive a request, Request path:" + String suffix = ". "[] bytes = +" static "+ 200 ResultInfo resultInfo = (resultInfo = | 200, "
/*** Contoller * @ author guojing */public class IndexController implements Controller {public ResultInfo process (Map <String, Object> map) corresponding to the Home Page) {ResultInfo result = new ResultInfo (); result. setView ("index"); result. setResultMap (map); return result ;}}
String path = "" = "" = ""(! === Content. replaceAll ("\\\ \{" + key + "\\}" =+ (Constants. VIEW_BASE_PATH ==? "/": Constants. VIEW_BASE_PATH + "/" + viewPath + ". page"View Code
200: {[] bytes = 302: {Headers headers = "Location" 0 404: {[] bytes = "" = [] bytes) =View Code
In this example, index.pageis a dynamic template page, test.js is a JS file, and tx.jpg is an image. The Code is as follows:
Hello,${name}
The source code (github) address is attached: source code