Java Web Development 2nd ed course extract – Introduction
Articles,  Blog

Java Web Development 2nd ed course extract – Introduction

– [Male Narrator] Hello, and welcome to this course on Java web development. My name is Matt Greencroft, and I’m going to be your
trainer on this course. I’m assuming that you’re
taking this course because you want to be able
to build websites in Java. Well, you will be able to do
this by the end of the course. Although I think it’s
important to understand right from the start that no one, or at least I would assume no one, would today attempt to build a new website using just the core Java libraries alone. Whilst this is possible,
it’s what we’re going to do on this course; it’s hard work, and most projects will therefore
use a framework to help. By a framework, I mean a set
of pre-written Java libraries that will help with the
task of building websites. Using a framework makes the
whole process much easier. Probably the most popular
framework for Java is Spring, and Spring provides a great web development framework called Spring MVC. And if you use Spring Boot, then you can get started
with Spring MVC very quickly. We have a number of courses on the Spring framework here at
Virtual Pair Programmers, and we cover both Spring MVC and Spring Boot in quite a lot of detail. If you’re using Java EE, using an application service
such as WildFly, for example, then you’ll probably want to
use the Java EE web framework, which is called Java Server Faces, or JSF. And there are other frameworks you can use on top of JSF; the most
popular is called PrimeFaces. I’m planning to do a short course covering JSF and Prime Faces immediately after this course is finished,
so there should be a course available on this very soon. By the time you take this course, if you’re interested in Java EE, you’ll hopefully find that
that is also available, and will be a good follow
on from this course. And, of course, there are
other frameworks you could use. You might have heard of
Struts, Wicket, Play, and there are many, many more. So, to be a Java web developer, you don’t need to do this course. You could go and learn the framework that you’re going to use. Indeed, we have many customers who have done our Spring courses and are happily creating websites today. But there is a really good reason why we have created this course. Web development has been possible in Java since the very early days
of Java back in 1997. It all started with a
technology called the servlet. Although the servlet API has been updated and improved over the years,
all the frameworks use and extend this piece of technology. So understanding what a
servlet is and how they work is a really important piece of knowledge that all good Java web
developers should have. It gives us a critically
important understanding of what is happening under the hood, even though the frameworks might hide some of that complexity. So understanding servlets
will help you understand what you might need to do when things don’t quite work as expected. Also, there are some
other fundamental concepts of web development that you really need to understand to build effective websites. And these are concepts that
the frameworks can’t hide. Things like forms, the
difference between get and post, what is meant by a session,
how cookies work, and so on. Some of this course is
designed to cover all of this. The different topics that
Java website builders really need to know and understand, irrespective of which
framework you’re using. So it doesn’t really matter
then whether you start learning about building
websites with this course and then go on to study about frameworks or if you’ve started with frameworks, and then you’re using this
course to fill in the gaps. But on this course I’m
trying to assume that you don’t have lots of understanding of how websites work, at least from the point of view of a coder. So that’s the introduction.
Let’s get started. And in the remainder of this chapter, we’ll talk about what
you need to get started. If you’ve come to this
course never having done any web development, but you have at least written some code in Java,
maybe you’ve taken our Java Fundamentals course,
then you’ll know that, with Java, you can package up the code that you’ve written into a JAR file. The JAR file, or Java
Archive, makes it easy to distribute your project,
and if you have a class with a public static void main method, well, you can make this the entry point to your JAR, or make a runnable JAR file. So JAR files allow us to
distribute console applications, server applications and
even desktop applications with a graphical user interface. All of these can be distributed
easily with a JAR file. But when you build a website, we need something slightly different. The code for our website will need to sit on a server somewhere, and the users will be
accessing the website that is calling the code
sitting on the server, either over the Internet, or maybe a local network,
but using a browser. So there’s a really
important piece of software that needs to run on the server, which we call the webserver. This might sound a bit confusing. The physical hardware, the
machine with the processor, memory and hard drives, is a server. But it can’t interact with the browser on the client computer
unless it has some software on it that does this, and this software is called the webserver software. So webserver software can do a few things, but primarily, it will sit there listening for requests from the clients, and then when it receives
one of those requests, it can do the processing and provide a response back to the client. There are lots of different
software providers of webservers, and depending
on what kind of processing you want to be able to do,
that will partly determine which piece of software you choose. We obviously want some webserver software that can process Java code, so we’ll need a webserver
written to work with Java. Now there are a few of these available. You can do a search on
the Internet to find some, but we’re going to use in
this course Apache Tomcat. This is an open-source webserver, and it is absolutely production standard. It’s suitable for running real production-quality websites on. In a production environment then, we’ll have a physical server
which runs the Tomcat software. We then need to do two things: we need to export the Java
code that we have written into a format that allows us to place it into this webserver software, the equivalent to building a JAR file, and we need to configure the webserver to work with our application. I’m going to look at the first of these in a little bit of detail. But the good news is that,
while we are developing, there is a much easier
way to work with Tomcat. We don’t need a separate server. We can run the software on
our own development computer. We need a single machine that can act as both the server and the client. We’re going to see that in practise in the next chapter, but
before we leave this one, I’d like to just talk a bit about this process of deploying a website that you’ve built to a Tomcat server. I’m covering it now
because it might be useful for you to have this as
a bit of an overview, but because it’s about deployment rather than coding, I
don’t want this to get in the way of the main part of the learning process in this course, which is going to start
in the next chapter. So sit back for a few minutes and I’m just going to
talk through the process to deploy a website to
the Tomcat software. If this is something you already
are very comfortable with or you’re not concerned
about deployment just yet, then you’re fine to leave this chapter now and go straight on to chapter two. There’s not going to be anything else on the video of this chapter other than the overview
of the deployment process. So I’ve got in Eclipse here a project open which contains code for a working website. Actually, it’s the code that
we’re going to be building as we progress through this course. It doesn’t matter for
now what this code is, but you’ll notice that
it is a complete website. It consists of various Java classes at the top here, and also,
there’s some other files that are useful for websites. There’s some HTML files, there’s an image, and CSS file. So everything that we need for our website is contained within this project. So the process to put all of this onto a server is quite straightforward. We create what is called a WAR file. A WAR file is similar to a JAR file. It stands for Web Archive, and it’s really a ZIP file containing all of the files for your
entire web application. I’m talking here about
all of the HTML files, all of the Java code,
the CSS and the images. Everything that you need
for the entire application is going to be exported
into a single file. In a few moments we’ll
look at a real WAR file, but before that, I have
a little caption here that will allow us to understand the contents of a WAR file. In a regular JAR file, the
structure is fairly simple. You just have your Java classes inside. But a WAR file has a
very distinct structure. If you were to look inside a WAR file, you’d find that, well,
in the root of the file is a collection of the HTML files. Actually, there’s a little more than that. All the other static
files of your application. So any images, CSS files, they would all be in the root of your WAR file. There’s no Java code
here, just the standard static files for your web pages. Oh, and by the way, you
could have a directory structure here, so if
you had a folder with some of these files within a folder, that would reflect the URLs, but the idea here is
that all the static stuff sits at the root of the WAR file. Also in the WAR file, there’ll
be a folder called WEB-INF. I believe that stands for web information. Quite importantly, it’s all in capitals with a hyphen in the middle. So what’s in the WEB-INF folder? Well, inside here is all the hidden files for your web application. A Java webserver such as
Tomcat guarantees that, under no circumstances, could a user ever see what’s inside this folder. So what is in here? Well, we’re going to be writing Java code. We want the Java code
to run on our server, and we’ll find the classes in here in a folder called Classes. There’s also going to
be a folder called Lib in which we’ll find any JAR files that our application needs. So if, for example, you were
using the Spring framework for your ware application,
the Spring JAR file would be in the Lib folder. Something else that sits in
here is a file called web.xml, and that describes the
configuration of your application. We’re going to be looking
at this web.xml file in quite a bit of detail as we
progress through the course. There’s one other folder in a WAR file, and that’s called the META-INF folder. If you’re familiar with JAR files, well, you’ll know about META-INF. The idea with this
folder is that supposedly any information about the
WAR file goes in here. Principally, there’s a
file called, and this is supposed to describe how many files are in the WAR file, the version of the WAR file, and so on. For some JAR files, that can be important, but for a WAR file, generally it’s boring. We never look at it, and
as we’re going to see in a moment, it’s
automatically generated anyway. So that’s the theory of a WAR file. Let’s go and build a WAR
file for our project, and we can then have a
look at what’s in it. So back to the project, and
how we build the WAR file is pretty similar to the
way we build a JAR file. We’re going to export the project. Depending on the version
of Eclipse that you have, you might be able to just do this. If we right click on the
project name and choose export, Eclipse gives us different
ways to export the project, and under Java we’ve got JAR file. Actually, the version
of Eclipse I’m running doesn’t give me the option for a WAR file. That’s simply because I’m
using the version of Eclipse that’s designed for Java SE developers, and the ability to build WAR files isn’t part of Java SE,
so we’ll need to add in this functionality to Eclipse. And in case this is something
that you will experience, you’ll need to build WAR files. I’d like to just show you how to do this. So it’s quite straightforward, actually. We’ll click on help and
instal new software. I’m just going to check
all available sites from the top option here, and I’m just waiting for this section to be filled with various options. And then we’ll scroll down this list. It’ll be near the bottom.
We’re looking for web. Here it is. And I want to
choose a few things in here. We’re looking for Eclipse
Java EE developer tools. Here it is. There, and the next one, Eclipse
Java Web Developer Tools. And the other one we want is the JST Server Adapters extensions. I’m just scrolling down. Here it is there. This Extensions one here. Okay, we’ll click on next, and I’m going to let those instal. Eclipse will probably need to restart, so I’m going to pause the
camera while this instals and then press resume when we’ve finished. Okay, so that finished off camera, and Eclipse did need to restart. I’m just reopening my project, checking everything is still there. No reason why it shouldn’t be. And we’re now ready then to
try and build our WAR file. So we’re going to export our project, and well, we’ve got some
more options down here, and under Web, we’ve
got the WAR file option. It’s got the project name here. We just need to find
somewhere to save the file. I think I’m going to
put this on the desktop so that we can have a look at it, and we’re just calling it Restaurant.WAR. The name of our project
is The Restaurant Project. Okay, so I’m going to
click on save and finish. That’s all I need to do
to create the WAR file. And there’s the file that’s been created. Now, there are several ways to look at the contents of a WAR file. I’m just going to go
for the quick and dirty way of doing it, which is to
rename this to a ZIP file. You might remember I said
that, like JAR files, WAR files are just ZIP files. So let me just choose rename, and we’ll call it Restaurant.ZIP, and then my operating system
will let me open this up. So as we can see, then, here is the structure of our website. There’s the META-INF folder and the WEB-INF folder
that I mentioned before. Everything not in these two folders is going to be publicly accessible. So what we’ve got here then
is not in those two folders is our HTML files, there’s
the CSS, there’s an image. All of that sits in the
root of our website. If I’d created a structure of
folders for our HTML files, then we’d see those here as well. So let’s have a look
at the META-INF folder, and in there is the MANIFEST file that, again, we’ve already mentioned. Let’s open this up and have a look at it. Well, that’s just a text file. As we can see, it’s pretty boring. Nothing exciting there at all. Because this is a maven project, there’s actually another
folder in here called maven. I won’t bother looking at that. That’s not going to be in every WAR file, and it’s not something
entirely interesting to us. Okay, well, in the WEB-INF folder, and we go into the classes folder, we’ll see, here is the
structure of the classes in our application, so in
this instance it’s com, virtualpairprogrammers, and
we’ve got different folders, different compiled Java classes in here, and back to the root of
WEB-INF in the lib folder, all of the JAR files that
our application needs. The final thing I’ll point out is that, under WEB-INF, there’s
also a file called web.xml Now, I’m not going to
open that file just yet because we are going to
be building this file throughout the course,
and we’ll understand what’s in here as we progress through. Okay, well, that’s the
contents of a WAR file. How do we deploy it then to a server? Well, the first thing
I’m going to do is to rename this file back to .WAR, and then we need an instance
of the Tomcat software. Now, you can go and download this from the website. I’ve done that offline,
and I’m going to start the software up here by
going into the bin folder and I’m running Windows,
so I just need to click on this startup.bat file. If you were using Linux or the Mac, you’d be running Okay, so my application is started. I’m going to leave that
console visible there. And then, to deploy
our WAR file to Tomcat, I need to copy the WAR file that we built into the web apps folder here. So I’m just going to make sure that this console window is visible. I’m just going to find the WAR file, which is hiding behind here, look. So I’m going to copy the WAR file into that web apps folder, and hopefully, within a minute or two, we’ll see something happen
on the console to indicate that Tomcat has found that
WAR file and is adding it, there we are, something happened, look, and we can see deployment
of web application archive. There’s our Restaurant.WAR, has finished. So with that being the case, we should be able to go
and view the website. So I’m opening a browser and we need to navigate to localhost:8080. That’s the port that our
Tomcat server is running on, and then, by default, when
we’re running in a production version of Tomcat, we need
to have / and the name of our WAR file, which was Restaurant.WAR We don’t need the .WAR bit. If I press enter on here, that should hopefully bring
me back the front page for my restaurant, which it has done. I know it’s not very pretty, but at least we’ve seen that
we can take the Java code, the application that we’ve built, create a WAR file from
it, deploy it to Tomcat. I know I’ve done that on
my desktop machine here, but that instance of
Tomcat could have been running on a server and it
would have been an identical process to deploy this file to the server, and then we’d have our website available on the Internet on a production server. So that’s the process for deployment. Now, in development it
would be very painful to have to go through that full process, so the good news is that
there is an alternative. W can run the Tomcat software embedded within Eclipse as part of our project. We’re going to see how to
do that in the next chapter, which is also where we’ll start learning the basics of web development in Java. So when you’re ready, I’ll
see you in chapter two.

Leave a Reply

Your email address will not be published. Required fields are marked *