区块链基础:基于Jetty实现小型HTTP服务

1、创建项目

区块链基础:基于Jetty实现小型HTTP服务_第1张图片

pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>cn.hadrongroupId>
  <artifactId>httpartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>httpname>
  <url>http://maven.apache.orgurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>

  <dependencies>

        <dependency>
            <groupId>org.eclipse.jettygroupId>
            <artifactId>jetty-serverartifactId>
            <version>9.3.20.v20170531version>
        dependency>
        <dependency>
            <groupId>org.eclipse.jettygroupId>
            <artifactId>jetty-servletartifactId>
            <version>9.3.20.v20170531version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency> 
  dependencies>
project>

2、HTTPService

package cn.hadron.http;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

/**
 * 对外http服务
 *
 */
public class HTTPService {

    public HTTPService() {}
    /**
     * 初始化HTTP服务
     */
    public void initServer(int port) {
        try {
            Server server = new Server(port);
            System.out.println("监听http端口" + port);
            //Servlet上下文
            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/");
            server.setHandler(context);
            // 查询
            context.addServlet(new ServletHolder(new HelloServlet()), "/hello");
            // 提交
            context.addServlet(new ServletHolder(new PostServlet()), "/post");

            server.start();
            server.join();
        } catch (Exception e) {
            System.out.println("init http server is error:" + e.getMessage());
        }
    }

    private class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().print("Hello,World!");
        }
    }   

    private class PostServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setCharacterEncoding("UTF-8");
            String name = req.getParameter("name");
            System.out.println("接收到的参数:"+name);
            resp.getWriter().print("接收到的参数:"+name);
        }
    }
}

3、启动程序

import cn.hadron.http.HTTPService;

/**
 * 启动HTTP服务
 *
 */
public class Main {
    public static void main(String[] args) {
        try {
            HTTPService httpService = new HTTPService();
            int httpPort = Integer.valueOf(8001);
            httpService.initServer(httpPort);
        } catch (Exception e) {
            System.out.println("启动错误:" + e.getMessage());
        }
    }
}

4、测试

(1)运行启动程序
这里写图片描述

(2)get方法测试
http://localhost:8001/hello
区块链基础:基于Jetty实现小型HTTP服务_第2张图片

(3)POST方法测试
下面通过postman工具进行测试

这里写图片描述

这里写图片描述

这里写图片描述
这时查看控制台,输出信息正确
这里写图片描述

你可能感兴趣的:(Java区块链)