tags:

views:

142

answers:

3

Is there an object within the standard Java SE that can accept a HTTP request from a socket? I have found how to create and send one, however I have not found a way to retrieve a HTTP object from a socket. I can create one my self, but I would rather rely on a heavily tested object.

This seems like something that would be readily available given the structure of JSP.

+1  A: 

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

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

flybywire
This isn't what I was looking for. I am writting an HTTP server, and I would like to see if there was existing classes that would handle HTTP request parsing.
monksy
+3  A: 

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

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();
    }
  }
}
Thorbjørn Ravn Andersen
This looks very close to what I've been looking for.
monksy
If you don't have very good control on where your software will be running, I would strongly advice against using proprietary features in the Sun VM like this, at least when there are easily available alternatives. You could instead use Jetty as an embeddable servlet container, which would provide you a portable solution for your question.
jarnbjo
@jarnbjo, true, but this is not an internal Sun class. See http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/index.html?com/sun/net/httpserver/HttpServer.html
Thorbjørn Ravn Andersen
+1  A: 

Yeah, you make a new HTTP Request object from what you accept on the socket. What you do after that is up to you, but it should probably involve an HTTP Response.

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();
        }
    }
}

From: http://www.devhood.com/tutorials/tutorial%5Fdetails.aspx?tutorial%5Fid=396
There's some more work to be done in the tutorial, but it does look nice.

dlamblin