Create a simple HTTP server with Java?

后端 未结 13 2612
时光取名叫无心
时光取名叫无心 2020-11-28 04:16

What\'s the easiest way to create a simple HTTP server with Java? Are there any libraries in commons to facilitate this? I only need to respond to GET/POST, and

13条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 04:37

    This is how I would go about this:

    1. Start a ServerSocket listening (probably on port 80).
    2. Once you get a connection request, accept and pass to another thread/process (this leaves your ServerSocket available to keep listening and accept other connections).
    3. Parse the request text (specifically, the headers where you will see if it is a GET or POST, and the parameters passed.
    4. Answer with your own headers (Content-Type, etc.) and the HTML.

    I find it useful to use Firebug (in Firefox) to see examples of headers. This is what you want to emulate.

    Try this link: - Multithreaded Server in Java

提交回复
热议问题