simple HTTP server in Java using only Java SE API

前端 未结 17 1711
无人共我
无人共我 2020-11-22 13:28

Is there a way to create a very basic HTTP server (supporting only GET/POST) in Java using just the Java SE API, without writing code to manually parse HTTP requests and man

相关标签:
17条回答
  • 2020-11-22 14:12

    I like this question because this is an area where there's continuous innovation and there's always a need to have a light server especially when talking about embedded servers in small(er) devices. I think answers fall into two broad groups.

    1. Thin-server: server-up static content with minimal processing, context or session processing.
    2. Small-server: ostensibly a has many httpD-like server qualities with as small a footprint as you can get away with.

    While I might consider HTTP libraries like: Jetty, Apache Http Components, Netty and others to be more like a raw HTTP processing facilities. The labelling is very subjective, and depends on the kinds of thing you've been call-on to deliver for small-sites. I make this distinction in the spirit of the question, particularly the remark about...

    • "...without writing code to manually parse HTTP requests and manually format HTTP responses..."

    These raw tools let you do that (as described in other answers). They don't really lend themselves to a ready-set-go style of making a light, embedded or mini-server. A mini-server is something that can give you similar functionality to a full-function web server (like say, Tomcat) without bells and whistles, low volume, good performance 99% of the time. A thin-server seems closer to the original phrasing just a bit more than raw perhaps with a limited subset functionality, enough to make you look good 90% of the time. My idea of raw would be makes me look good 75% - 89% of the time without extra design and coding. I think if/when you reach the level of WAR files, we've left the "small" for bonsi servers that looks like everything a big server does smaller.

    Thin-server options

    • Grizzly
    • UniRest (multiple-languages)
    • NanoHTTPD (just one file)

    Mini-server options:

    • Spark Java ... Good things are possible with lots of helper constructs like Filters, Templates, etc.
    • MadVoc ... aims to be bonsai and could well be such ;-)

    Among the other things to consider, I'd include authentication, validation, internationalisation, using something like FreeMaker or other template tool to render page output. Otherwise managing HTML editing and parameterisation is likely to make working with HTTP look like noughts-n-crosses. Naturally it all depends on how flexible you need to be. If it's a menu-driven FAX machine it can be very simple. The more interactions, the 'thicker' your framework needs to be. Good question, good luck!

    0 讨论(0)
  • 2020-11-22 14:12

    All the above answers details about Single main threaded Request Handler.

    setting:

     server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
    

    Allows multiple request serving via multiple threads using executor service.

    So the end code will be something like below:

    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 App {
        public static void main(String[] args) throws Exception {
            HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
            server.createContext("/test", new MyHandler());
            //Thread control is given to executor service.
            server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
            server.start();
        }
        static class MyHandler implements HttpHandler {
            @Override
            public void handle(HttpExchange t) throws IOException {
                String response = "This is the response";
                long threadId = Thread.currentThread().getId();
                System.out.println("I am thread " + threadId );
                response = response + "Thread Id = "+threadId;
                t.sendResponseHeaders(200, response.length());
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:14

    This code is better than ours, you only need to add 2 libs: javax.servelet.jar and org.mortbay.jetty.jar.

    Class Jetty:

    package jetty;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.mortbay.http.SocketListener;
    import org.mortbay.jetty.Server;
    import org.mortbay.jetty.servlet.ServletHttpContext;
    
    public class Jetty {
    
        public static void main(String[] args) {
            try {
                Server server = new Server();
                SocketListener listener = new SocketListener();      
    
                System.out.println("Max Thread :" + listener.getMaxThreads() + " Min Thread :" + listener.getMinThreads());
    
                listener.setHost("localhost");
                listener.setPort(8070);
                listener.setMinThreads(5);
                listener.setMaxThreads(250);
                server.addListener(listener);            
    
                ServletHttpContext context = (ServletHttpContext) server.getContext("/");
                context.addServlet("/MO", "jetty.HelloWorldServlet");
    
                server.start();
                server.join();
    
            /*//We will create our server running at http://localhost:8070
            Server server = new Server();
            server.addListener(":8070");
    
            //We will deploy our servlet to the server at the path '/'
            //it will be available at http://localhost:8070
            ServletHttpContext context = (ServletHttpContext) server.getContext("/");
            context.addServlet("/MO", "jetty.HelloWorldServlet");
    
            server.start();
            */
    
            } catch (Exception ex) {
                Logger.getLogger(Jetty.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        }
    } 
    

    Servlet class:

    package jetty;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class HelloWorldServlet extends HttpServlet
    {
        @Override
        protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException
        {
            String appid = httpServletRequest.getParameter("appid");
            String conta = httpServletRequest.getParameter("conta");
    
            System.out.println("Appid : "+appid);
            System.out.println("Conta : "+conta);
    
            httpServletResponse.setContentType("text/plain");
            PrintWriter out = httpServletResponse.getWriter();
            out.println("Hello World!");
            out.close();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:15

    You can write a pretty simple embedded Jetty Java server.

    Embedded Jetty means that the server (Jetty) shipped together with the application as opposed of deploying the application on external Jetty server.

    So if in non-embedded approach your webapp built into WAR file which deployed to some external server (Tomcat / Jetty / etc), in embedded Jetty, you write the webapp and instantiate the jetty server in the same code base.

    An example for embedded Jetty Java server you can git clone and use: https://github.com/stas-slu/embedded-jetty-java-server-example

    0 讨论(0)
  • 2020-11-22 14:18

    How about Apache Commons HttpCore project?

    From the web site:... HttpCore Goals

    • Implementation of the most fundamental HTTP transport aspects
    • Balance between good performance and the clarity & expressiveness of API
    • Small (predictable) memory footprint
    • Self contained library (no external dependencies beyond JRE)
    0 讨论(0)
  • 2020-11-22 14:19

    Once upon a time I was looking for something similar - a lightweight yet fully functional HTTP server that I could easily embed and customize. I found two types of potential solutions:

    • Full servers that are not all that lightweight or simple (for an extreme definition of lightweight.)
    • Truly lightweight servers that aren't quite HTTP servers, but glorified ServerSocket examples that are not even remotely RFC-compliant and don't support commonly needed basic functionality.

    So... I set out to write JLHTTP - The Java Lightweight HTTP Server.

    You can embed it in any project as a single (if rather long) source file, or as a ~50K jar (~35K stripped) with no dependencies. It strives to be RFC-compliant and includes extensive documentation and many useful features while keeping bloat to a minimum.

    Features include: virtual hosts, file serving from disk, mime type mappings via standard mime.types file, directory index generation, welcome files, support for all HTTP methods, conditional ETags and If-* header support, chunked transfer encoding, gzip/deflate compression, basic HTTPS (as provided by the JVM), partial content (download continuation), multipart/form-data handling for file uploads, multiple context handlers via API or annotations, parameter parsing (query string or x-www-form-urlencoded body), etc.

    I hope others find it useful :-)

    0 讨论(0)
提交回复
热议问题