HTTP request object

Is there an object in standard Java SE that can accept an HTTP request from a socket? I found how to create and send one, however I did not find a way to get the HTTP object from the socket. I can create my own self, but I would rather rely on a highly tested object.

This is similar to what would be easily accessible given the structure of the JSP.

+3
source share
3 answers

There is a small HTTP server in the Java 6 SDK (not sure if it will be in a JRE or in a non-Sun JVM).

From http://www.java2s.com/Code/Java/JDK-6/LightweightHTTPServer.htm :

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 HttpServerDemo {
  public static void main(String[] args) throws IOException {
    InetSocketAddress addr = new InetSocketAddress(8080);
    HttpServer server = HttpServer.create(addr, 0);

    server.createContext("/", new MyHandler());
    server.setExecutor(Executors.newCachedThreadPool());
    server.start();
    System.out.println("Server is listening on port 8080" );
  }
}

class MyHandler implements HttpHandler {
  public void handle(HttpExchange exchange) throws IOException {
    String requestMethod = exchange.getRequestMethod();
    if (requestMethod.equalsIgnoreCase("GET")) {
      Headers responseHeaders = exchange.getResponseHeaders();
      responseHeaders.set("Content-Type", "text/plain");
      exchange.sendResponseHeaders(200, 0);

      OutputStream responseBody = exchange.getResponseBody();
      Headers requestHeaders = exchange.getRequestHeaders();
      Set<String> keySet = requestHeaders.keySet();
      Iterator<String> iter = keySet.iterator();
      while (iter.hasNext()) {
        String key = iter.next();
        List values = requestHeaders.get(key);
        String s = key + " = " + values.toString() + "\n";
        responseBody.write(s.getBytes());
      }
      responseBody.close();
    }
  }
}
+4
source

, HTTP- , . , , , , , HTTP-.

import java.io.*;
import java.net.*;
import java.util.*;

public final class WebServer {
    public static void main(String args[]) throws Exception {
        int PORT = 8080;
        ServerSocket listenSocket = new ServerSocket(PORT);
        while(true) {
            HttpRequest request = new HttpRequest(listenSocket.accept());
            Thread thread = new Thread(request);
            thread.start();
        }
    }
}

: http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=396
, .

+2

It looks like you are looking for a servlet. A servlet is an API that allows you to receive and respond to an HTTP request.

Your servlet is deployed in a container, which is basically the actual web server that will take care of all the protocol complexities. (The most popular are Tomcat and Jetty)

+1
source

Source: https://habr.com/ru/post/1722247/


All Articles