使用Http Server构建web应用

一、输出信息到页面中
package http;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class Http {


    public static void main(String[] arg) throws Exception{
        HttpServer server = HttpServer.create(new InetSocketAddress(8001),0);
        server.createContext("/index",new TestHandler());
        server.start();
    }

    static class TestHandler implements HttpHandler{

        @Override
        public void handle(HttpExchange exchange) throws IOException {
            String response = "this is server";
            exchange.sendResponseHeaders(200, 0);
            OutputStream os = exchange.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}

直接在浏览器上输入地址访问
http://127.0.0.1:8001/index

显示如下图


使用Http Server构建web应用_第1张图片
image.png
二、加载某个页面

加载某个页面,需要用到pom.xml
项目结构如下


使用Http Server构建web应用_第2张图片
image.png

pom.xml



    4.0.0

    netty_tcp_test
    netty_tcp_test
    1.0-SNAPSHOT

    
        4.1.25.Final
        linux-x86_64
        1.7.25
        1.8
    


        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.1
                
                    ${java.version}
                    ${java.version}
                    ${java.version}
                
            

        
    


Http.java

package http;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class Http {


    public static void main(String[] arg) throws Exception{
        HttpServer server = HttpServer.create(new InetSocketAddress(8002),0);
        server.createContext("/index.html",new TestHandler());
        server.start();
        System.out.println("启动成功");
    }

    static class TestHandler implements HttpHandler{

        private byte[] data;

        public TestHandler(){
            try(InputStream in = this.getClass().getResourceAsStream("/index.html")){
                this.data = new byte[in.available()];
                in.read(data);
            }catch (Exception e){
                throw  new RuntimeException(e);
            }
        }
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            String response = "this is server";
            exchange.getResponseHeaders().set("Content-Type", "text/html");
            exchange.sendResponseHeaders(200, 0);

            OutputStream os = exchange.getResponseBody();
            os.write(data);
            os.close();
        }
    }
}

index.html




    
    teste






启动后如下访问,即可访问到对应的index.html页面


使用Http Server构建web应用_第3张图片
image.png
3、并发处理

对于TestHandler,可以在其中为每个新的请求新开一个线程进行处理,如下

  static class TestHandler implements HttpHandler{

        private byte[] data;

        public TestHandler(){
            try(InputStream in = this.getClass().getResourceAsStream("/index.html")){
                this.data = new byte[in.available()];
                in.read(data);
            }catch (Exception e){
                throw  new RuntimeException(e);
            }
        }
        @Override
        public void handle(HttpExchange exchange) throws IOException {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        exchange.getResponseHeaders().set("Content-Type", "text/html");
                        exchange.sendResponseHeaders(200, 0);

                        OutputStream os = exchange.getResponseBody();
                        os.write(data);
                        os.close();
                    }catch (IOException ie){
                        ie.printStackTrace();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();

        }
    }

你可能感兴趣的:(使用Http Server构建web应用)