Create a simple HTTP server with Java?

后端 未结 13 2472
时光取名叫无心
时光取名叫无心 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

    0 讨论(0)
  • 2020-11-28 04:37

    Embedding Tomcat is relatively painless as such things go. Here's a good StackOverflow reference about it.

    0 讨论(0)
  • 2020-11-28 04:37

    I have implemented one link

    N.B. for processing json, i used jackson. You can remove that also, if you need

    0 讨论(0)
  • 2020-11-28 04:42

    I just added a public repo with a ready to run out of the box server using Jetty and JDBC to get your project started.

    Pull from github here: https://github.com/waf04/WAF-Simple-JAVA-HTTP-MYSQL-Server.git

    0 讨论(0)
  • 2020-11-28 04:44

    Jetty is a great way to easily embed an HTTP server. It supports it's own simple way to attach handlers and is a full J2EE app server if you need more functionality.

    0 讨论(0)
  • 2020-11-28 04:45

    If you are using the Sun JDK you can use this built in library
    Look at this site on how to use.

    If n ot there are several Open Source HTTP Servers here which you can embed into your software.

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