Giter Site home page Giter Site logo

tiny-tomcat's Introduction

tiny_tomcat

Netty based Tomcat web application server

自己动手造轮子系列(二)

基于Netty实现tomcat

20年的时候写过一个基于Netty的简易版Tomcat,当时是为了学习Netty的使用,实现了一个简单的Servlet容器。原文地址:https://blog.csdn.net/qq_38258310/article/details/112305744

其他系列

. 自己动手造轮子系列(一)- Golang实现Redis cli

环境依赖

  • JDK 1.8
  • Maven 3.x
  • Netty 4.1.51.Final
  • SnakeYaml 1.25
  • Gson 2.8.0

使用方法

启动

加载完maven依赖后进入src/main/java/example目录下执行example.TomcatApp的main方法即可启动

配置

  • 配置文件位于src/main/resources目录下的tomcat.yml文件,可以配置端口号、静态资源目录、Servlet包名等
  • 静态资源放置于src/main/resources/static目录下

自定义Servlet

使用@WebServlet注解即可将Servlet注册到容器中,使用url参数指定路由,例如:

@WebServlet(url = "/hello")
public class HelloServlet extends HttpServlet
{}

实现Servlet的doGet和doPost方法完成业务处理,例如:

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    doPost(request, response);
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    response.getWrite().write("you response");
}

访问

http://localhost:8080/hello?id=1

servlet.png

http://localhost:8080/index.html

static.png

tiny-tomcat's People

Contributors

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