Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Créer un serveur HTTP simple en java


{filelink=873}

 
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
 
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
 
public class ServeurHTTP {
  public static void main(String[] args) throws IOException {
    InetSocketAddress addr = new InetSocketAddress(1985);
    HttpServer server = HttpServer.create(addr, 0);
 
    server.createContext("/", new Gestionnaire());
    server.setExecutor(Executors.newCachedThreadPool());
    server.start();
    System.out.println("Le serveur en ecoute sur le port: "+addr.getPort());
  }
}
 
class Gestionnaire implements HttpHandler {
 
  public void handle(HttpExchange exchange) throws IOException {
    String methodeRequete = exchange.getRequestMethod();
    if (methodeRequete.equalsIgnoreCase("GET")) {
      Headers reponseEntete = exchange.getResponseHeaders();
      reponseEntete.set("Content-Type", "text/plain");
      exchange.sendResponseHeaders(200, 0);
 
      OutputStream reponse = exchange.getResponseBody();
      Headers requeteEntete = exchange.getRequestHeaders();
      Set<String> keySet = requeteEntete.keySet();
      Iterator<String> iter = keySet.iterator();
      while (iter.hasNext()) {
        String key = iter.next();
        List values = requeteEntete.get(key);
        String s = key + " = " + values.toString() + "";
        reponse.write(s.getBytes());
      }
      reponse.close();
    }
  }
}

A Voir sur le même Sujet:

  • java lecture fichier serveur http split
  • java http server
  • java et serveur http
  • creer serveur web java
  • créer serveur http en java
  • email httpeasy.com loc:FR
  • java simple web server
  • com.sun.net.httpserver.httphandler html post
  • develloper un serveur web avec java
  • http exchange image java