-5

I need to build a simple HTTP server in Java using only the standard Java API. I found this question: simple HTTP server in Java using only Java SE API which has two answers that use only the standard Java API:

  • One answer is to use the com.sun.net.httpserver package, but this package is not an official part of Java.
  • Another answer is to use the javax.xml.ws package, but, according to the Java 9 documentation, this package is deprecated since Java 9.

So, what is the current official way to create a web-server?

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • 1
    Not sure what the first and third answer would be for you as they are sorted according to user preferences, but most of them look 7years old and one can expect there alternates to be in effect easily, so try and find ways and come back if you find them inefficient as compared to those answers. That would be better a discussion, right? Or else this could be primarily opinion based and too broad at the same time. – Naman Oct 13 '17 at 06:47
  • @nullpointer yes, the answers are 7 years old, that is why I ask! When I google for "java web server", the first link that I get is that 7 years old answer! – Erel Segal-Halevi Oct 13 '17 at 06:50
  • 2
    IMHO, when you say you googled and then you relied on just the first link to ask a question here. I don't believe you *googled* properly. There can be link 2 and 3 that might suggest some other way and then there can be 4 and 5 with other suggestion( hence primarily opinion based). And let's say I select 2 and 3 and start explaining all of it to the way to create a web server out of it, that would be too broad.....Anyway, there is an answer to the question, which makes me realize more that the question was poor in its research. – Naman Oct 13 '17 at 07:07

1 Answers1

2

The simple server, found in com.sun.net.httpserver is an official, but optional API in Java 9, located in the module jdk.httpserver. This implies that you can expect it to be maintained in OpenJDK, but not to be available in every JRE.

If you can live with these conditions, you can use it.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • Isn't there any server that is available in every JRE? – Erel Segal-Halevi Oct 13 '17 at 13:06
  • 1
    How many programming languages do you know which have a web server embedded in their standard library? How many percent of all Java developers do you think will need an embedded web server? – Holger Oct 13 '17 at 16:26
  • In Python: https://docs.python.org/3/library/http.server.html and Node.js: https://nodejs.org/api/synopsis.html it is quite easy to build a web-server with only the standard libraries. – Erel Segal-Halevi Oct 14 '17 at 16:25
  • 1
    Well, Python doesn’t have that separation between core libraries and optional libraries, I don’t know, whether there are alternative implementations you’d have to care about at all. And Node.js isn’t even a general purpose programming language in the first place. Anyway, you can bundle your application with the jdk and the required modules… – Holger Oct 16 '17 at 10:32
  • Well GO has a standard Http server in its core libraries, i believe we are in an age where embedded httpservers and logging capabilities have to be part of core of any language moving forward. – kimathie Jan 22 '22 at 15:07
  • 1
    @kimathie well, the http server of this answer is included in java. Whether every program should carry the baggage of a server even if it doesn’t need it, is questionable. Not everyone is looking forward to a future where literally every device can be hacked through the internet. And I don’t know why you are bringing in “logging capabilities”. No one discussed logging here. Java has built-in logging support since twenty years. – Holger Jan 24 '22 at 08:03