Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

HttpServer example java

public class BasicHttpServerExample {

  public static void main(String[] args) throws IOException {
      HttpServer server = HttpServer.create(new InetSocketAddress(8500), 0);
      HttpContext context = server.createContext("/");
      context.setHandler(BasicHttpServerExample::handleRequest);
      server.start();
  }

  private static void handleRequest(HttpExchange exchange) throws IOException {
      String response = "Hi there!";
      exchange.sendResponseHeaders(200, response.getBytes().length);//response code and length
      OutputStream os = exchange.getResponseBody();
      os.write(response.getBytes());
      os.close();
  }
}
Comment

HttpServer example java


package com.stackoverflow.q3732109;

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

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

public class Test {

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String response = "This is the response";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }

}

Comment

PREVIOUS NEXT
Code Example
Java :: spring h2 database example 
Java :: java get year from date 
Java :: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565) 
Java :: check instance of java 
Java :: check last character of a string java 
Java :: getting the last value of an integer in java 
Java :: string remove last character 
Java :: how to minimize jframe in java 
Java :: java const 
Java :: how to use scanners in java 
Java :: close a popup selenium python 
Java :: javafx tableview add data 
Java :: write input stream to file java 
Java :: action bar spigot 
Java :: java read keyboard input 
Java :: upload converted base64 to image object to oss without saving in java 
Java :: java print 2d char array 
Java :: java download file from url 
Java :: java how to make a number 
Java :: java jtable change column color 
Java :: java get all directories in path 
Java :: java download 64-bit 
Java :: Index through 2d array 
Java :: java collapse string array 
Java :: how to get data from radio group in android 
Java :: JAVA Display Sum of n Natural Numbers 
Java :: Duration class java 
Java :: min height bst leetcode 
Java :: how to convert int to string in java? 
Java :: findviewbyid in kotlin Just using id name . 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =