Hi everyone.
Welcome back.
In this lecture we'll learn about the simple web server that's part of Java.
This was released as part of Java 18.
And it's part of the Java distribution that we installed in our machine.
This web server serves the files and folders from your machine.
That's all it does.
Nothing more, nothing less.
Probably you might have this question running in your head, why is this being released in Java in the
first place?
So the primary goal of this simple web server is that can be used for prototyping, testing and debugging.
So how do we launch this simple web server in your machine?
We can launch the simple web server by running the J web server in the terminal.
So when you run this, this is going to automatically launch the web server that's going to be serving
up the files and folders from your machine.
And some of the things that you need to be aware of when it comes to J web server is that it has the
support for just the get and head requests only.
The next thing is Https is not supported, and it has just the support for HTTP 1.1, which means http
t.
2.0 is not supported, so that's all you need to know from a theoretical aspect of it.
Now let's go ahead and explore this in action.
So in this case I'm going to go back to the terminal or to my IntelliJ.
So in the IntelliJ if you open your Explore Java Features project and then if you go to the resources
directory.
So in the resources directory you will notice two files.
Number one is movies JSON.
So this movies JSON returns you an array of different movies actually.
And if you take a look at it, it has ten movies in this particular JSON file.
And then you have another file named movie ID by JSON.
So this is a one single file.
So this is a single JSON element.
That's the reason why the name is movie ID by JSON.
So what we are going to do here is that using the J-web server we are going to be serving up these two
Jsons.
Okay.
And then we are going to look at it in the browser.
So the very first thing is.
So I'm in the root path of this specific project.
In this case, you can see the modern Java, right?
That's where I'm on right now.
And then what I'm going to do, I'm going to run a J web server.
If you run this command, what this is going to give you is that this is going to be starting up the
web server in the port 8080.
As you can see, it is running in the port 8080.
And what it is telling you here is that serving the content under modern Java and the subdirectories
on port 127.0.0.1 and port 8080.
Okay, let's copy this and then let's go to the browser.
So in the browser I'm going to hit this endpoint.
So if I hit this endpoint this is exactly what we have in our IntelliJ actually.
So if you go to the IntelliJ you have this check out service Explore Java features.
So you have the check out service here you have the Explore Java features here.
And then you have the modules folder here.
And then the Readme Gradle.
You see right the Gradle over here we have the readme and then we have the settings.gradle dot CSS.
All those files are hosted in this web address.
And now what we are going to do is that we are going to be hosting the movie ID by Jason and the movie
by our movie star, Jason.
So if you go to the actual resources directory in the resources directory, I have all these commands
readily available.
So if you go to the commands dot, txt so you can actually copy this and then hit this endpoint from
the browser.
So if you hit this one.
So there you go.
It gives you all the movies that's been present in this specific directory okay.
And then in this case if we hit movie ID by Jason it's going to just serve up that specific file.
And let's say if you just provide the resources over here, it's going to serve up all the files in
this specific folder.
So the J-web server is useful if you have any kind of testing going on where you are building a UI.
And the UI requires a simple JSON.
So in those kind of scenarios, basically you can use J-web server to host those files, and you can
prototype your UI based on that.
Or it could be a back end service, require some kind of a response from another service, but the other
service is not ready yet.
You can simply use your server to give you the response whatever you want through an endpoint call like
this.
So we have this option available.
So any time you are looking for a real network call that can serve up a JSON or any kind of content
over the network, then you can use the simple web server by running the J-web server command like this.
And then it can serve up the files and folders.
In this case, any call that's been made, it will be logged over here.
So in this case, if I hit this one more time and you'll be noticing a call over there.
There you go.
So all these calls get logged to.
So that's all about simple web server.
In the next section we are going to be exploring the new HTTP client that got released as part of modern
Java.
And we will be using the web server as a back end for that HTTP client, which is exactly the purpose
of the J web server.
This marks the end of this lecture.
Thank you for watching.