Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Spark is a Micro-Framework that’s sole purpose is to get you up and running and rapidly developing web applications. After we discuss the project for the course, let’s go get it installed. It’s a breeze!
Project Directory Fix
The 'Create directories for empty content roots automatically' option has moved. Once you've finished creating the project, you can set that property by doing the following:
- Use CTRL+SHIFT+A to bring up the Action Search menu and type 'gradle'.
- Choose the option that says 'Gradle' on the left and 'Settings' on the right.
- Check the box for 'Create directories for empty content roots automatically' and hit OK.
Links:
-
0:00
I'm super excited to introduce you to the web
-
0:03
application framework that we're gonna be using for the rest of this course, Spark.
-
0:07
Now Spark is unfortunately in a bit of name space collision in the Java world.
-
0:12
The Spark framework is not to be confused with the database called Apache Spark.
-
0:18
It's unfortunate that they share the same name and
-
0:20
it makes searching around a little confusing at times.
-
0:23
So, keep this in mind as you start navigating these parts.
-
0:27
The idea behind a microframework is to keep things small, simple, and
-
0:31
clear and Spark does just that.
-
0:33
It is built for productivity and design to get you up and running and
-
0:37
ready for rapid application development in no time.
-
0:42
Part of the complaint of Web frameworks in general,
-
0:44
is that the more sophisticated they get, the more they bloat and swell.
-
0:48
There's usually tons of configuration, and
-
0:50
you have to learn a whole new environment before you can even write a simple page.
-
0:54
Well microframeworks are not like that.
-
0:56
In the Ruby programming world,
-
0:58
a super tiny and easy to use framework was created.
-
1:00
It's called Sinatra.
-
1:02
Now Spark and many other microframeworks got it's inspiration from Sinatra so
-
1:06
you might hear it being called Sinatra based.
-
1:09
So get ready to start spreading the news because this is awesome.
-
1:14
Okay, so before we get this framework installed,
-
1:17
let's talk a bit about the project for this course.
-
1:20
So the actual idea for
-
1:21
the creation of this course came from one of your fellow classmates.
-
1:24
As you probably know, we always ask you for feedback and ideas for future courses.
-
1:29
I usually get a direct message on Twitter, something over email, and
-
1:32
we put these suggestions out for you to vote on.
-
1:35
So one of your classmates shot me an email saying, I'd love to learn Spark so
-
1:38
I can get a site up and running quickly.
-
1:40
And I thought that sounded like a good idea.
-
1:42
And I turned that into a request, and into something you could vote on.
-
1:45
So, check the teacher's notes for more on the current voting system.
-
1:49
And you all voted it up and
-
1:50
we agreed that it made great sense to have in our library, so here it is.
-
1:54
Well that got me thinking, why don't we remove me, as the middle man and
-
1:59
just have a place where you students can submit ideas yourself.
-
2:02
Why don't we just build an app?
-
2:03
And I thought, that's a pretty simple web app.
-
2:05
And then I was like, why not just use one of those microframeworks.
-
2:09
And then I was like, I'm writing a course on Spark and I need a project and then.
-
2:15
I was like, wow sparkception.
-
2:18
So the project for this course, let's make a site that takes ideas from students and
-
2:23
then allows voting.
-
2:24
Sound good?
-
2:25
Let's go get Spark installed.
-
2:27
Alright, so
-
2:28
let's create a new project in IntelliJ IDEA and we'll make a great old project.
-
2:33
So do create new project And we'll choose Gradle over here on the left, and
-
2:38
if Gradle's new to you, check the teacher's notes for a quick intro to it.
-
2:41
Now we're going to use it because we're going to use Spark as a dependency.
-
2:46
So if we were going to publish this, we'd want something like this for the Groupid.
-
2:52
We'd want com Team Treehouse the reverse, and we'll just call this Courses.
-
3:00
And for the artifact ID we want something like Course Ideas.
-
3:05
Remember, this is what the published jar would be named.
-
3:08
Let's go ahead and choose Next.
-
3:11
And let's choose create directories for
-
3:13
the empty content roots automatically that way it gonna be there.
-
3:18
Awesome.
-
3:23
Okay.
-
3:24
So now the file, directories all there, let's open up the source.
-
3:28
Main, Java And let's add a new class here.
-
3:33
New java class, and the class name is,
-
3:37
let's do come dot team treehouse .courses and for
-
3:41
simplicity's sake let's just call it me.
-
3:47
Okay.
-
3:48
And in this file we'll make our standard main call.
-
3:53
I want you to see how simple Spark actually is
-
3:56
to get the prototype up and running.
-
3:57
So we'll do the public static void main and press tab, it builds it out for us.
-
4:02
Okay, so now we're ready.
-
4:04
So first let's add our dependency.
-
4:06
So the site's called sparkjava.com, I have it open over here.
-
4:10
So sparkjava.com, and up here there's this Download tab.
-
4:14
So over here it tells us how to get it.
-
4:16
So the group ID is com.sparkjava, and then it's spark-core and 2.3.
-
4:22
So let's go to build.gradle And in the dependencies section here,
-
4:27
we'll say compile.
-
4:28
And we'll use the shorthand, we'll say,
-
4:32
com.sparkjava, and then spark-core, and then 2.3.
-
4:43
Let's go ahead and click apply the suggestion there that way when we ship
-
4:48
this project, if we ever do, Gradle will be shipped with it.
-
4:53
Okay, so let's flip over to the Gradle tool window, and
-
4:56
in here we'll press this refresh all Gradle projects.
-
5:04
And bam, just like that, we have Spark and all of its dependencies.
-
5:09
So here's Spark and here's Jetty.
-
5:12
And Jetty is the embedded web server that's used.
-
5:15
So, what do you say we flip back to the documentation and
-
5:18
take a quick look at the demo under the documentation?
-
5:20
So let's switch back.
-
5:22
Let's go over here to documentation.
-
5:25
And it says right here this is what we wanna do.
-
5:27
Let's just grab this line because basically we have that same thing in our
-
5:30
file, right.
-
5:31
So I'm just gonna grab this and I'm gonna flip back over and
-
5:35
in our main Java we'll add that line.
-
5:37
And let's see what happens.
-
5:40
Let's close this Gradle project, get it out of the way.
-
5:46
Okay.
-
5:50
So what does it say here?
-
5:52
It says, we need to do the static import of Spark.get.
-
5:56
See it saw that.
-
5:58
So again it's statically importing,
-
5:59
there is a get method here and it's off of the spark class.
-
6:05
Cool, if it doesn't like your lambda syntax, by default,
-
6:10
some of the ides are set to 6 by default.
-
6:13
But go ahead and set it to 8.
-
6:15
Spark really shines when we're using the lambda syntax.
-
6:18
And if lambda syntax is new to you, check the teacher's notes.
-
6:21
But basically what it's saying here is
-
6:26
called the static method get with the path name /hello.
-
6:29
Anything that's matching /hello, and then it takes a lambda.
-
6:34
In the lambda here, there's two parameters that are taken.
-
6:37
It's the Rec which is request and res which is response.
-
6:42
And then it returns,
-
6:44
remember the [INAUDIBLE] the first thing here returns hello world.
-
6:49
So, this is a function that will return hello world and
-
6:51
its passed a request and a response.
-
6:54
This is, what this lambda here is know as a route.
-
6:57
It's actually a functional interface for the route object.
-
7:01
So what this means is that when an http get request is made to our web server
-
7:07
that is looking for the uri or the uniform resource identifier of slash hello.
-
7:14
Our function will be called and hello world will be returned.
-
7:17
Now we'll touch a bit more on these requests and
-
7:20
response variables in upcoming videos, but for now let's just return that text.
-
7:24
Could that really be all it took to get things up and running?
-
7:28
Well, let's give it a go, what do you say?
-
7:30
So, let's choose run and we'll choose Run for the menu here, we will choose Main.
-
7:40
Okay, so it says that Spark has ignited and
-
7:43
it says that it's listening on port 0.0.0:4567,
-
7:48
so 0.0.0 is a code name for all IP addresses on your current machine.
-
7:55
So you can access that URL in the URL bar of your browser,
-
8:00
or you could just use another nickname, which is localhost.
-
8:02
So let's do that.
-
8:03
So let's switch over to a new window here, and we'll say localhost.
-
8:09
Well, actually, first let's say that we're gonna talk HTTP to localhost
-
8:16
and I'm going to do a colon and that's for the port which was port 4567.
-
8:22
Now by default it listens on a different port but we're specifically saying this.
-
8:25
By default, you don't need to specify the port.
-
8:28
The port is 80 but we're specifying here 4567 and that's how you specify the port.
-
8:33
Okay. So then, let's look for /hello.
-
8:37
Sound good, right?
-
8:40
Whoa, look at that.
-
8:41
A website.
-
8:43
So, wait a second.
-
8:45
All I need to do if I want another page is just add a new route, right?
-
8:49
So I know that we want a page when someone just comes to our site.
-
8:51
So that path is just /, right?
-
8:54
So let's do that.
-
8:55
Let's add Over here we'll just come in and say get.
-
8:58
If anybody comes to just slash then we add,
-
9:04
again, we'll add a request and a response.
-
9:10
And let's make it say welcome students.
-
9:17
Cool, awesome.
-
9:18
And I save it, and if I come back over here and I just do the slash, that should.
-
9:25
What?
-
9:27
You know what?
-
9:29
I forgot to restart the server.
-
9:33
The server is running.
-
9:34
So there's this button here.
-
9:35
This restarts.
-
9:35
This will stop and re-run main.
-
9:37
So notice that main is still running.
-
9:39
So let's stop it and re-run it and now let's go see if the slash is there.
-
9:47
Welcome students.
-
9:48
There it is.
-
9:48
Woah.
-
9:50
Bam.
-
9:50
You just built a web application.
-
9:52
It's for sure gonna get more challenging, but
-
9:54
you just tackled that in like five minutes or something.
-
9:57
That's pretty amazing right?
-
9:58
Micro-frameworks for the win.
-
10:00
Also, did you see how clear that syntax was?
-
10:03
Doesn't get much more clearer than that, does it?
-
10:06
But forgetting to restart the server is going to bite you, I promise it will.
-
10:10
You'll make a change and you won't see it and you change it some more and
-
10:13
you'll still not see it and then do a facepalm like this.
-
10:16
And you'll realize that it's because you forgot to reset the server.
-
10:19
It's actually a good thing to remember that the server's constantly running,
-
10:23
just waiting for any number of requests to come in from wherever.
-
10:26
Even though our request response loop for that one request was finished,
-
10:29
the server's still just sitting there waiting to respond to the next request.
-
10:33
Well, let's see if we can make this app a little more dynamic.
-
10:36
Why don't we make a site that's more personal.
-
10:39
Welcome, students isn't exactly as personable as we can make it.
-
10:43
Why don't we prompt for the user's name?
-
10:45
Actually I think we have a little work on making things look a little bit prettier.
You need to sign up for Treehouse in order to download course files.
Sign up