Servlets and JSPs Tutorial: Learn Web Applications With Java

Learn how to create dynamic websites using the Java programming language with this java web application tutorial
4.3 (716 ratings) Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
14,272 students enrolled Bestselling in Java
$19
$35
46% off
Take This Course
  • Lectures 64
  • Length 10 hours
  • Skill Level All Levels
  • Languages English, captions
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 9/2012 English Closed captions available

Course Description

In this Java web application tutorial I'll show you how to create dynamic websites using the core technologies of Java web programming. If you want to create your own interactive websites, if you know some Java and you want to take your skills to the next level, or if you want hot skills for the job marketplace, this Java web application tutorial course is for you.

In this Java web application tutorial course you'll learn how to:

  • Leverage the power of Java to create dynamic websites
  • Deploy your applications for free on the Internet
  • Use and understand core server-side Java web technologies

I'll show you how to take your basic Java knowledge and use it to create websites using the same technologies (servlets, JSPs and JSTL) that everyone from self-employed web developers to huge corporations use to create modern interactive web sites.

What are the requirements?

  • Basic Java knowledge
  • Ideally basic HTML and SQL knowledge
  • Basic computer and Internet skills

What am I going to get from this course?

  • Understand the core technologies of modern Java web programming
  • Know how to develop and deploy your own websites using Java

What is the target audience?

  • Anyone who wants to create websites that interact with databases
  • Java programmers who want to learn web programming skills
  • Java beginners who want to learn more Java and enjoy a challenge

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Getting Started
03:58

About the course, and why you should learn web programming with Java.

  • Core basic Java web server technologies
  • Some exciting stuff we'll cover in the course
  • Web design vs. server technologies
  • Travel the world with Java
  • Some prerequisites

06:58

In this tutorial we'll look at what software you need to install to start developing Java-based websites. All the software you need is free; we'll be using the industry-standard Eclipse IDE together with the highly popular Tomcat server. And of course, the JDK and JRE (Java Development Kit and Java Runtime Environment).

  • What software to install for Java web application development.
  • Where to find the software.
  • Apache HTTP server vs. Tomcat.
  • Starting Tomcat using the Windows administration tool
  • Starting Tomcat using Eclipse
  • Dealing with the "ports in use" error message

07:18

In this tutorial we'll create a "hello world" Java servlet. A servlet is a Java class that runs in an "application server" and sends web pages back to a browser when a user somewhere in the world clicks on a URL. Here we'll get started by creating a really simple servlet that creates the simplest possible web page.

  • Creating servlets
  • Creating dynamic web projects in Eclipse
  • The PrintWriter class
  • http gets
  • The init, doGet and destroy servlet methods
  • The request and response objects

09:53

In this tutorial we'll look at a better way out outputting HTML, rather than having to hardcode it in your servlet. JSPs are another of the core building blocks of Java web applications, allowing you to combine HTML and Java seamlessly.

  • Creating a JSP
  • What are JSPs for?
  • A very brief introduction to HTML
  • Embedding Java in JSPs with scriptlet tags
  • Displaying the date on a JSP page
  • Using the "out" object in a JSP page.



15:01

In this tutorial we'll look at the web.xml file; a standard config file that enables us to, among other things, map urls to servlets and JSPs in our applications. We'll need to understand web.xml before we can move on to deploying our applications on the Internet. I'll also give you some tips that may help to ensure you will seldom have to look at web.xml, except when you want to customise your URLs. 

  • The web.xml file
  • Some differences between Eclipse versions
  • "Annotation" style deployment descriptors, and why not to use them
  • Context roots, domain names and servlet mappings
  • Adding a welcome page to your project
  • Mapping JSPs to URLs
  • What to do if your latest code doesn't seem to run in Eclipse

07:17

In this tutorial we'll look at deploying your application to a Tomcat server using a .war file. Deploying Java web applications is surprisingly simple, provided you've got a working web.xml.

  • Deploying your application to a Tomcat server
  • Creating .war files
  • Redeploying web applications


Article

Cloudbees.com, which I originally recommended using to upload your servlet is being or has been decommissioned. There are lots of other options around though -- for example, try https://www.openshift.com, which lets you set up lots of kinds of app server projects -- Java Tomcat included.

10:36

In this tutorial we'll look at how you can deploy your application to the Internet, and without even paying a penny.

  • Google App Engine
  • Cloudbees
  • Clouds and their advantages
  • The importance of web.xml for deployment
  • Fending off the notorious "unsupported major minor version" error
  • Recompiling your application with an appropriate Java version
  • Dealing with the "ports already in use" error message.

Section 2: Nuts and Bolts
09:15

We've seen so far that you can use Java in your JSPs via scriptlet tags; but to really leverage Java (as well as to keep your JSPs reasonably simple) you'll want to import your own and other Java classes into JSPs. We'll see how to do that in this tutorial.

  • Where your Java code ends up in the translated servlet
  • The page directive with an import attribute
  • Importing Java classes into JSPs

11:15

In this tutorial we'll look at the vital topic of how to retrieve parameters from the URL in JSPs and servlets.

  1. What a URL parameter looks like
  2. Getting parameters in servlets
  3. Specifying multiple parameters
  4. Getting parameters in JSPs: the 'out' object
  5. The break tag in HTML


08:05

In this tutorial we'll discover a slightly surprising but very important use of scriptlets; looping or conditionally including entire blocks of HTML. 

  • Generating HTML with scriptlets
  • Scriptlets with conditions and loops
  • The paragraph and strong tags in HTML


18:38

In this tutorial we'll look at techniques for including files within other files, so that you can build your application up out of chunks of HTML contained in separate files. For instance, you can have a file containing header for all your pages which you can then include in all the pages on your website.

  • The include directive
  • The JSP include tag
  • Static vs. dynamic includes -- which to use and when
  • When you absolutely need a static include
  • When you absolutely need a dynamic include

Note: I said in this tutorial that jsp:include causes a separate browser request --- but actually this isn't quite right; jsp:include causes a sort of simulated request, apparently --- not an actual request from the browser.

08:15

In this tutorial we'll look at various ways of getting a servlet or JSP page to display another page; a powerful technique that enables you to build fully-fledged Model-View-Controller applications, among many other things.

  • The jsp:forward tag
  • Using the request dispatcher
  • The response sendRedirect method
  • Forwarding vs. redirecting


04:22

The declaration tag allows you to add methods and data members to the servlet that your JSP gets translated into, which is occasionally useful .... actually its main use is probably just that it makes it clearer where your Java actually ends up when you use Java scriptlets in JSPs.

  • The declaration tag
  • Where your Java ends up in the translated JSP.
  • Adding methods and data members to a JSP.

05:42

In this tutorial we'll take a look at the types of tags that we've seen so far.

  • The directive tag; the page directive
  • Multiline scriptlet comment tags
  • The expression tag
  • The declaration tag
  • The scriptlet tag

14:56

In this tutorial we'll look at the difference between Model 1 and Model 2 architecture; two important ways of structuring your web applications.

  • Model 1 architecture - disadvantages
  • Model 2 architecture: MVC (Model-View-Controller)
  • What is MVC?
  • Using relative URLs
  • Using servlets with forwards to implement controllers



03:31

Some tips about using jar files containing external APIs in your web projects.

  • The Tomcat lib directory
  • The project WEB-INF lib directory
  • Finding the location of your Eclipse project
</p>
02:44

The servlet lifecycle is something you'll definitely get asked about if you go for a job interview or sit an exam. For that reason, we'll review it in this tutorial.

  • The servlet lifecycle
  • The init and destroy methods

Section 3: Beans and Forms
10:32

Beans are a mechanism for sharing data between JSPs and for maintaining state in your application. In this tutorial we look at a simple example of a bean, storing some information about the user.

  • Creating beans
  • The JSP useBean tag
  • Setting bean properties
  • Getting bean properties using expression tags

16:35

The notion of "scope" is very important in web programming; we'll start to look at it in this tutorial, in the context of JSP beans.

  • Session scope and cookies
  • Page scope
  • Request scope
  • Application scope
Note: although I mistakenly said that the session timeout is set by default to ten minutes in Tomcat 6.0, actually Tomcat 6 and 7 set it to 30 minutes as far as I know. Later on we'll see a demonstration of that fact by retrieving the timeout. If you care about the session timeout, it's best to configure it yourself.


05:57

This tutorial looks at how to set bean properties from parameters; either URL parameters or, as we'll see later on, parameters set by the user submitting a form.

  • Setting bean properties using the param attribute
  • Using default bean behaviour to set parameters
  • Using the wildcard to match bean properties to parameters

08:13

Forms are, of course, a vital part of web programming, allowing you to gather information from your users or allowing them to log in to your website. Here we'll start looking at adding forms to your applications using the ideas we've seen so far, plus some HTML.

  • The "form" HTML tag
  • Input controls - buttons, text fields
  • The "get" method
  • Retrieving submitted values


05:39

Posts are a type of HTTP request in which the data you submit doesn't appear in the URL, by-passing the data limit on URLs and making for much nicer URLs, among other things. Data from forms is usually submitted via post, not "get". We'll look at how easy it is to make your forms post data in this tutorial.

  • Post vs. get
  • The doPost servlet method

11:22

Beans are a great way to collect data from forms. We'll see how to do just that in this tutorial.

  • Using beans to store form data
  • Creating a "user" bean
  • Submitting a form to a JSP page

11:55

Beans are a handy place to put server-side validation code. If we add validation code to a bean, we can check if the information the user has entered in a form makes sense or not by filling in the bean and calling its validation method.

  • Adding validation code to a "user" bean
  • An example of a regular expression for validating email addresses

19:43

By bringing together some of the techniques we've seen so far, we can create a form that validates itself, allowing itself to be submitted to your servlet or confirmation page only when the user has entered valid input.

  • Creating self-validating forms using beans
  • The input control value attribute in HTML
  • The jsp getProperty tag
  • Hidden HTML input controls



22:14

In this tutorial we'll take a look at a little web application that uses a strict MVC architecture, incorporating our bean validation code and validating the bean in the controller rather than the JSP.

  • An MVC example
  • Validating forms in your controller servlet
  • Setting request attributes

Section 4: Some Useful Objects
11:53

The session object allows you to store objects and data that persist for the duration of the user's session, making it an indispensable tool for maintaining application state while your user is browsing your site.

  • The session object
  • Retrieving the session timeout period
  • Using the session object in servlets and in JSPs
  • Setting and getting session attributes
  • Retrieving objects from the session


13:23

How can you maintain session if the user disables cookies? In this tutorial we'll look at a form of URL rewriting that adds a session ID to your links, allowing session to be maintained.

  • What happens to sessions if cookies are disabled
  • Preventing errors when the session doesn't work
  • Encoding (rewriting) URLs to support cookie-free sessions
  • Getting the context path programmatically
  • The jsessionid.

Note: some argue that session IDs in URLs are a security risk; you may decide instead either to not use sessions or to allow your site to simply not work if the user disables cookies.

If you turn of cookies in your browser while following this tutorial, don't forget to turn them on again so you can continue following the Udemy course!

07:33

You can use the context object to store data that persists as long as your server is running, and is shared between all users.

  • The servlet context object
  • Setting and getting context attributes
  • Implementing a simple (non-persistent) page hit counter

04:43

If you have some parameters, for example user names and passwords, that you want to be configurable for your web application, one place to put them is web.xml. You can use the servlet context object to retrieve these parameters.

  • Adding parameters to web.xml
  • Using the Eclipse XML editor
  • Fetching parameters using the context object

04:43

So far we've discussed scope in the context of JSPs, and we've seen various objects that exist at different scopes in servlets. Here we'll review the three servlet scoped objects that we've seen.

  • The request object
  • The session object
  • The servlet context object
  • Getting and setting object attributes

13:34

Cookies are both loved and loathed, but either way they're an essential part of the Internet. In this tutorial we'll look at how to get and set cookies in a servlet.

  • The Cookie class
  • Setting cookies via the response
  • Getting cookies via the request
  • The cookie maximum lifespan
  • Setting and getting the lifespan

Section 5: Databases
15:01

If you're serious about web programming, you'll definitely want to connect to a database at some point. Database connectivity can be achieved in servlets using the generic database Java API known as JDBC. In this tutorial we'll look at a simple way you can connect to a database using JDBC, that works in any kind of Java program.

  • JDBC connectors
  • Finding a connector jar for your database
  • Connecting to a database using the DriverManager class
  • Why connecting in the init() method is a terrible idea

20:59

If you're working on an industrial-strength web app, you'll definitely want to set up a JNDI data source, implementing connection pooling and connection timeouts and so preventing application crashes from using up your database connections. You'll probably also need to use JNDI if you're deploying your application to an application server that you don't personally control.

  • Benefits of JNDI data sources
  • Some differences between Eclipse versions
  • Setting up your data source
  • Where to find information on data sources for your application server
  • Getting a database connection from a data source
  • How to stop Eclipse caching your code


20:33

In this tutorial, as an example of doing a database query, we'll create a form that allows a user to log in to a website, only allowing the user in if the entered email address and password match those stored in the database.

  • The PreparedStatement and ResultSet classes
  • Wildcards in JDBC queries
  • How to avoid SQL injection attack

09:30

In this tutorial we'll look at an example of inserting data into a database, creating an application that lets the user create an account in our demo web application.

  • Inserting values into databases with prepared statements
  • Implementing a "create account" screen

Section 6: Sending Emails
04:44

You can send email from your servlet or other Java program, provided you've got access to a mail server (which you can get for free) and the Java Mail API.

  • Some free email servers you can use
  • Where to find the Java Mail API
  • How to install the mail API


16:03

In this tutorial we'll look at an example of sending an email using the mail API, together with an SMTP server.

  • Where to find information about the mail API
  • Where to find information about your mail server
  • Specifying your server and authentication information
  • An alternative way of getting the email session object
  • Constructing and sending a message

Section 7: Simplify Your Code With JSTL
06:11

Up till this point in the course we've covered just about everything you need to know to implement a web application; however, our code is getting messy and a lot of it is not exactly elegant. Fear not; in this tutorial we'll look at another standard technology that will allow us to greatly simply our code, making it more elegant and more maintainable.

  • Introducing JSTL
  • Where to find the JSTL API jar, if you need it
  • The taglib directive
  • The c:out tag

06:41

How to access beans using JSTL, and a first look at EL (Expression Language)

  • What your class properties need to be accessed in JSTL
  • A first look at EL (Expression Language)
  • Accessing bean properties in EL



02:45

You can fetch parameters in JSTL by using an "implicit object", the param object.

  • Another comment style; the difference between HTML and scriptlet-style comments
  • Fetching URL parameters with the implicit param object


04:27

Just like Java, JSTL has an "if" statement, allowing you to dispense with using ifs in Java scriptlets in your JSPs.

  • The JSTL if statement
  • Comparing strings in JSTL
  • Taking different actions based on parameter values
04:46

The "if" tag in JSTL is pretty limited, not allowing any kind of else or else-if clause. For that you need the "choose" tag, which allows you to pick between multiple possibilities.

  • The c:choose tag
  • The "otherwise" clause

05:41

JSTL has an equivalent construct to the Java for loop in the form of the "foreach" loop. Here we'll take a look at foreach and some of its most useful attributes.

  • The forEach loop
  • The step attribute; specifying the step size
  • The varStatus attribute; an easy way to test for the first or last loop iteration

05:57

You can set objects as attributes on the request, session or web context object in your servlet and references them in your JSPs using a very simple EL syntax, which we'll look at here. Basically this enables you to passing Java objects to your JSP pages and references them in JSTL.

  • More on the request, session and web context objects
  • Passing objects from servlet into JSPs and referencing them in EL

04:01

In this tutorial we'll look at some further (in addiction to param) predefined objects that you can use in EL, enabling you to do lots of useful stuff.

  • Where to find documentation about the EL implicit objects
  • The scoped implicit objects
  • The pageContext object

04:11

You can set a Java Map into one of the scoped objects and then access its values from JSTL. We'll look at how to do it here.

  • Accessing Map values in EL

04:42

You can actually output stuff on your JSP page by using an EL expression directly on the page, outside of any JSTL tags. On the other hand, if you need your strings to be escaped, you need c:out. We'll take a closer look here.

  • Outputting links with c:out
  • Outputting links directly with EL
  • The difference between direct EL and c:out

07:19

You can use the JSTL forEach loop to iterate through the items in a Java List or any one of various collection types. We'll see how to do it here, and later on this is going to come in very handy when we look at dealing with the results of SQL queries in JSTL.

  • Using forEach with lists
  • HTML tables -- a brief example

14:07

In this tutorial we'll look at how to take files created by a designer (or yourself) and turn them into a dynamic web project. Along the way we'll look at how to use graphics in a web application, and we'll see a really simple way to access the context root.

  • Importing external files into a dynamic web application
  • Using images
  • A really simple way to access the context root in EL
  • Picture Squirrel: an example web application
  • An example of setting a servlet to the site home page

10:24

We've already seen two good ways to include content in other content; here we'll look at the JSTL way of doing it, and we'll see an example of parcelling off a header and footer into separate file.

  • JSTL c:import
  • Including files in other files with JSTL
  • Setting parameters in JSTL imports

10:53

You can use JSTL to do SQL stuff, querying and even updating database; thus encapsulating a lot of messy SQL in the page where it's actually used and taking a huge burden of complexity off your controller servlets or other auxiliary code.

  • Doing a SQL query in JSTL
  • The "sql" prefix and taglib directive

07:04

It's time to take the image names we retrieved in the  last tutorial and use them to actually display images. We'll also use the JSTL c:set tag to set the image names into a scoped variable which can then be used to display the images. Plus, a bit of a look at, in effect, concatenating strings in JSTL.

  • The HTML image tag
  • The c:set tag: creating scoped variables in JSTL
  • String concatenation in JSTL

05:54

You can use a bunch of arithmetic operators and other stuff in EL; we'll see an example here in the course of putting our images into a grid.

  • Expressions in EL: an example
  • Creating grids from SQL queries
  • More JSTL examples
  • Getting the current index in forEach loops

08:01

The c:url tag both prefixes your context root and also adds on the jsessionid if necessary. We'll look at it in this tutorial.

  • The c:url tag
  • An example of mapping a servlet to a user-friendly url
  • An example of the link tag in HTML

06:45

You can use JDBC wildcards in JSTL; we'll look at it here, along with an example of getting a single row from a set of results.

  • SQL Wildcards in JSTL
  • Indexing arrays in JSTL
  • Avoiding SQL injection attacks using wildcards

10:48

JSTL includes some functions that you can use in EL, mostly connected to string handling. We'll take a look at a couple of examples here.

  • JSTL functions
  • Where to find documentation
  • Getting strings from substrings
  • Upper- and lower-casing strings
  • Capitalising the first letter of a word in JSTL

11:51

So far we've looked at SQL queries, and in this tutorial we'll go on to looking at an example of a SQL update. We'll also take a look at transactions, which enable us to execute a bunch of SQL statement which will either all succeed or else all fail; in the case of failure, the database is left in the same state that it was prior to the transaction.

  • SQL Updates in JSTL
  • Transactions
  • More EL expression examples

04:02

Besides the "c" and "sql" tags in JSTL, there are also formatting tags. In this tutorial we'll format a number using JSTL.

  • The JSTL fmt tags
  • Rounding a number using JSTL

15:38

You can create your own JSTL tags; in fact, you can create your own versions of every kind of tag we've seen. In this tutorial we'll take a look at a demo of creating a simple custom tag.

  • Custom tags
  • .tag files
  • Tag attributes
  • Some other things you can do with tags

Section 8: Source Code
Source Code
Preview
Article
Section 9: Where Now?
07:44

Congratulations on finishing the course!!! Many thanks for subscribing, and I hope you'll check out my courses in the future. In this final video I'll tell you about some other related technologies that might interest you, plus a few words about the job market.

  • Thanks and congratulations!
  • Client vs. server technologies
  • HTML, CSS and Javascript
  • Web frameworks
  • Looking for jobs
PS. This video was a bit EU/USA-centric. If you're from elsewhere, then please don't feel left out :)

Section 10: Extra!
22:15

In this video we'll look at how you can allow the user to upload files using Tomcat 6. The catch is, you either need to have a writeable directory on your server, or write some code to store uploaded files in MySQL. Assuming one of those, we'll take a look at how you can support upload forms with servlets.

NOTE: For Tomcat 7, see the second set of attached code to this lecture. Some things apparently changed in Tomcat 7, and the code I gave in this video no longer works.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

John Purcell, Software Development Trainer

After working as a software developer and contractor for over 14 years for a whole bunch of companies including CSC, Proquest, SPSS and AT&T in the UK and Netherlands, I decided to work full-time as a private software trainer. I now live in the beautiful city of Budapest, Hungary, from where I run the website Cave of Programming.

Ready to start learning?
Take This Course