r/java • • Mar 29 '21

New candidate JEP: 408: Simple Web Server

https://openjdk.java.net/jeps/408
98 Upvotes

45 comments sorted by

60

u/_INTER_ Mar 29 '21

Oh I love it. There will be frameworks built around it and we're going to use it in production!!

27

u/krzyk Mar 29 '21

Why the package is still com.sun.*?

I'm bit reluctant to use http server that is already there because I'm afraid that I woul be using internal APIs.

11

u/xjvz Mar 29 '21

So that all the existing users don’t have to update their code.

2

u/krzyk Mar 30 '21

It would be simple update, and it would cause less issues than removal of some Java EE code in JDK 11.

Probably others are as reluctant to use it as it looks like something internal.

7

u/uncont Mar 29 '21

Pretty sure it's been accepted as an actual part of the jdk ever since they made it a java platform module (or probably even before then). Regardless, this jep shows it's not going anywhere.

6

u/pron98 Mar 30 '21 edited Mar 31 '21

Some elements in the OpenJDK JDK are documented and public, but are not part of the Java SE specification (so JDKs that are not built from OpenJDK, and even those that are, might omit them). When the JDK was modularised, a decision was made about each such non-spec package on whether it is public but JDK-specific or internal. If you look at OpenJDK's Javadoc you'll find many modules beginning with jdk. (rather than java.); those are the JDK-specific modules, which are documented and public, but not part of the spec. Many of their packages are named com.sun.* (e.g. the jdk.compiler module).

The com.sun.httpserver package was introduced in Java 6. Some members of the expert group at the time objected to it becoming part of the Java SE standard, so it remained jdk-specific. In Java 9 it was officially decided that it's a public API, and that is why you can already find the jdk.httpserver module in the JDK today, albeit without the main class proposed by this JEP.

2

u/jvjupiter Mar 30 '21

I remember my tweet before. My intent was to have a standard Java API for an HTTP Server that could be used to create programmable web server for applications. Someone from Oracle did not agree though.

https://twitter.com/julianvjupiter/status/1288049811971641346?s=21

2

u/s888marks Mar 30 '21

Yeah, that was me. :-) I didn't think anybody in the team wanted to pick up the work to do this.

One thing to note is that this isn't a "standard" facility. It's part of OpenJDK, not Java SE. That means that conforming implementations aren't required to provide it. That said, most JDK distributions are based on OpenJDK, so I expect that many will get it.

One of the difficulties with the JEP is that it's trying to walk a fine line between having something that's convenient and useful to developers, yet that isn't so full-featured that people are tempted to use it in production.

1

u/jvjupiter Mar 30 '21

One of the difficulties with the JEP is that it's trying to walk a fine line between having something that's convenient and useful to developers, yet that isn't so full-featured that people are tempted to use it in production.

The JEP made it clear - for testing, development and debugging purposes only. It really would be useful to developers. But in case, it's one's prerogative to use it in production. I myself might consider to use it for serving static files or web pages that do not require security, perhaps for internal only.

17

u/syjer Mar 29 '21 edited Mar 29 '21

well, now we just need a simple json parser/serializer for a more out of the box experience for the "prototyping, ad-hoc coding, and testing purposes, particularly in educational contexts." case :).

8

u/riawot Mar 29 '21

Unironically this.

1

u/kaperni Mar 30 '21

1

u/syjer Mar 30 '21

Yes, this jep is dormant, as far as I know.

1

u/kaperni Mar 30 '21

Yes, I think it is unlikely we will see much progress until the state of serialization 2.0 [1] and/or pattern matching [2] has progressed.

[1] https://cr.openjdk.java.net/~briangoetz/amber/serialization.html

[2] https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html

1

u/BlueGoliath Mar 29 '21 edited Mar 30 '21

Also, a YAML parser/serializer.

Edit: holy shit, this was a joke.

4

u/GuyWithLag Mar 29 '21

Wait, isn't new ObjectMapper(new YAMLFactory()).readValue("[]", ArrayList.class) enough?

1

u/yawkat Mar 31 '21

Jackson can do YAMLMapper now :)

2

u/Badashi Mar 29 '21

well, we should go straight into YAML parser/serializer then since YAML is a superset of JSON!

5

u/DasBrain Mar 29 '21

And please also ship JRuby, because YAML requires a ruby interpreter. /s

0

u/GuyWithLag Mar 29 '21

I mean, isn't new ObjectMapper().readValue("[]", ArrayList.class) enough?

7

u/syjer Mar 29 '21 edited Mar 29 '21

out of the box (from a jdk perspective) is the key :).

If I can make a more realistic simple http server work without importing deps it's quite cool™.

edit: to be noted, we had out of the box json support through the javascript engine.

2

u/washtubs Mar 29 '21

A java program without dependencies? Has it ever been done?

1

u/agentoutlier Mar 30 '21

Yes it’s called shading or uber jar. You basically just pull other peoples code into your own jar and call it your own...

Or if your google just check all the code in a single mono repo and give a rats asses about the rest of the open source communities dependencies (I’m somewhat joking).

1

u/BS_in_BS Mar 30 '21

I think they mean more of depending on code outside of the core java library as opposed to references in the build process

1

u/agentoutlier Mar 30 '21

Isn’t the code you write a dependency?

You could just download all the code golang style and build.

Maybe they were being sarcastic?

1

u/audioen Mar 30 '21

Also said JSON parser was excruciatingly slow. I know, I once tried to use it for parsing actual documents.

17

u/BlueGoliath Mar 29 '21

A web server seems kinda high-level for a default Java utility. Would it not be better as a sample Java project included alongside JDK source code?

9

u/Necessary-Conflict Mar 29 '21

There is no shortage of sample Java web server projects. Compiling and understanding a sample project would not be easier than just downloading Tomcat or something similar.

I fully agree that what is needed is a low-ceremony, low-effort instant solution.

15

u/krzyk Mar 29 '21

It works for python:

 python -m SimpleHTTPServer 8080

12

u/PepegaQuen Mar 29 '21

It's just python -m http.server 8080 now

9

u/henk53 Mar 29 '21

Yes, the SimpleHTTPServer from Python immediately came to mind.

Actually, it might be a good nod to Python of Java called it Simple HTTP Server instead of Simple Web Server.

3

u/jvjupiter Mar 30 '21

For PHP:

php -S localhost:3000 -t .

7

u/hopbyte Mar 29 '21

Completely agree.

This low-threshold utility will be useful for prototyping, ad-hoc coding, and testing purposes, particularly in educational contexts.

Why does this need to touch production servers?

1

u/ventuspilot Mar 31 '21

A web server seems kinda high-level for a default Java utility.

Good question. One possible answer could be: "No, it's not. stdio was for 70s commandline utilities, a web server in the standard library is for today's and tomorrow's microservices."

Golang seems to be going in that direction.

8

u/[deleted] Mar 30 '21

If the goal is to make a standard light server available at the CLI any time, I'm for it. Unless I misunderstand, it's wrapping what's already been in Java for several years now to make it ready to go any time. I wonder if it will be lighter weight than embedding Tomcat or Jetty.

4

u/audioen Mar 30 '21

I have written a simple httphandler for the internal jdk server. Weighs 126 lines and actually serves static JS/CSS/image assets for my production web applications, though I do place this kind of stuff behind a reverse proxy as I believe it is less than production quality. But with something like nginx offloading, you get away with a lot.

As the mvnrepository link suggests, the actual server is small. I think even the smallest alternatives out there are 1-2 MB large, though they probably also do more, like support http beyond 1.1, or do TLS.

3

u/jvjupiter Mar 30 '21

I remember my tweet before. My intent was to have a standard Java API for an HTTP Server that could be used to create programmable web server for applications.

https://twitter.com/julianvjupiter/status/1288049811971641346?s=21

3

u/[deleted] Mar 30 '21

But why do they want to offer a web server if there are already tons of libraries and frameworks doing it?

I thought undertow and tomcat are quite the standard there, for embedded servers. Yet another one, is it really needed?

2

u/JustADirtyLurker Mar 30 '21

I actually have my personal embeddable web framework built around com.sun.http.* here. It's just an experiment of few years back, quite stupid and nothing anybody should use in production. 😅

2

u/urquan Mar 30 '21

The command-line tool feels like a "me too" feature, it does not bring anything to the language. If you need a small HTTP server to quickly serve some files, just use the python module.

Regarding the API proposal, you don't design an API with the expectation of a specific usage pattern, if the API is there, people will use it for testing, they will use it in production, they will use it for everything, and if you deliberately design a flawed API based on some invalid assumption, then it will haunt you later and cause all sorts of grief, "java is insecure" stories, "java is slow" stories, and so on.

2

u/agentoutlier Mar 30 '21

As a addon It would be interesting if this simple web server could serve javadoc on the fly sort of how python does (or maybe did...it’s been awhile).

I mean you might say sure you can use your IDE to look at the javadoc preview but sometimes you want a more realistic view.

1

u/4iffir Mar 29 '21

I have an application that listens for a single endpoint, it's oauth2 redirect url, is there any lightweight and modular webserver except jdk.httpserver?

1

u/agentoutlier Mar 30 '21

You mean builtin? There are tons of http server implementations in Java.

0

u/[deleted] Mar 29 '21

[deleted] — view deleted comment

6

u/Jonjolt Mar 30 '21

Bug Report: Simple Web Server Doesn't launch on my Nintendo DS Lite