Build a web server of your own-from simple start

Source: Internet
Author: User
Tags log log

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 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:


The source code (github) address is attached: source code

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: 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.