GithubHelp home page GithubHelp logo

arctic-0 / easy-httpserver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from notbadpad/easy-httpserver

0.0 1.0 0.0 537 KB

一个java实现的简单的web服务器,主要目的是为了理解web服务器的工作原理。http请求的接收和返回使用JDK自带的HttpServer,请求处理和页面解析是自己实现的。目前已经能够支持web服务器一些基本的功能。

License: Apache License 2.0

Java 100.00%

easy-httpserver's Introduction

easy-httpserver

简介

一个java实现的简单的web服务器,主要目的是为了理解web服务器的工作原理。http请求的接收和返回使用JDK自带的HttpServer,请求处理和页面解析是自己实现的。目前已经能够支持web服务器一些基本的功能。

功能

目前项目已经能够完成一个动态网站的处理,并且采用类似jetty的嵌入启动方式,网站项目只需实现Controller和View页面并引入该项目jar包即可。 已完成主要功能如下:

  • 支持动态请求(后缀为do,请求路径restful风格)
  • 支持静态资源请求(后缀为对应资源类型后缀,可在配置文件中拓展)
  • 可通实现Controller接口处理动态请求,并支持注解方式进行映射
  • 动态请求可返回页面、json数据,并支持跳转
  • 整合Velocity,返回页面可通过Velocity语法渲染页面
  • 支持HTTP表单方式的文件上传(input:file)

如何使用

easy-httpserver项目内也有测试代码,可自行研究。这里介绍test-httpserver示例项目的使用,其中已经实现了一些例子,可参照这些例子实现。

  • 1、clone easy-httpserver到本地,导入eclipse(已安装maven,编码utf-8)
  • 2、clone test-httpserver(示例项目)到本地,导入eclipse
  • 3、mvn install easy-httpserver(将jar导入test-httpserver,若配置maven则自动导),以下修改均在test-httpserver中
  • 4、按照注释修改test-httpserver配置文件
    #包前缀
    PACKAGE_PREFIX=com.gj.web
    

    #view页面根路径(相对classes/view文件夹,项目中在src/main/view下) VIEW_BASE_PATH=page/

    #静态资源路劲(相对classes/view文件夹,项目中在src/main/view下) STATIC_RESOURCE_PATH=static/

    #端口 httpserver.port=8888

    #controller包路径,配置后可通过annocation直接配置 controller.package=com.gj.web.controller

    #url与controller类对应关系,与上边controller.package效果一致,二选一,格式:"url"+url路径+“=”+对应controller路径 #url/list=com.gj.web.controller.MyController

  • 5、编写Controller类,需实现Controller接口,并返回ResultInfo对象(详情见示例)。使用注解配置映射(如:http://127.0.0.1/test/path/show.do,配置controller为/test/path/,方法名为show
  • 5、在配置文件指定文件夹(page:velocity文件,static:静态页面和资源)下编写对应页面
  • 6、mvn exec:java -Dexec.mainClass="org.eh.core.http.EHServer"  启动服务
  • 7、浏览器访问http://127.0.0.1:8888/test/path/show.do,即可看到结果

项目结构

项目采用eclipse开发,并使用maven构建。

~src/main/java
  --org.eh.core
      --annotation
           AnnocationHandler.java   注解处理类
           Controller.java    Controller注解
       RequestMapping    请求方法注解,用来标注Controller中的处理方法
      --common
           Constants.java    常量类,包括系统常量和配置文件对应信息
           ReturnType.java    枚举,返回类型
      --http
           EHHttpHandler.java    httpserver请求核心处理类,完成请求的接收、处理、返回
           EHServer.java    项目启动类,完成服务器启动
       ApplicationContext.java   全局数据和会话相关数据,单例
       HttpSession.java   session会话
      --model
           FileInfo.java    上传文件信息封装
           ResultInfo.java    Controller返回结果
      --util
           FileUploadContentAnalysis.java    上传请求解析类,从中提取表单中的域值和文件信息
           FileUtil.java   文件工具类
           IOUtil.java    IO工具类
           PropertyUtil.java    配置文件工具类
           StringUtil.java    字符串工具类
           VelocityUtil.java    Velocity工具类
      --web
         --controller
              Controller.java    Controller接口,实现类必须继承Controller且标记Controller注解,实现方法必须标记RequestMapping注解
         --view
              ViewHandler.java    View处理类,完成页面文件渲染
~src/main/resources
     velocity.properties
     web.properties

easy-httpserver's People

Contributors

notbadpad avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.