Servlets and JSPs Tutorial: Learn Web Applications With Java
4.0 (1,368 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
16,961 students enrolled

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.0 (1,368 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
16,961 students enrolled
Created by John Purcell
Last updated 4/2016
Current price: $23.99 Original price: $34.99 Discount: 31% off
5 hours left at this price!
30-Day Money-Back Guarantee
This course includes
  • 10 hours on-demand video
  • 2 articles
  • 3 downloadable resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
Training 5 or more people?

Get your team access to 4,000+ top Udemy courses anytime, anywhere.

Try Udemy for Business
What you'll learn
  • Understand the core technologies of modern Java web programming
  • Know how to develop and deploy your own websites using Java
Course content
Expand all 64 lectures 09:54:56
+ Getting Started
8 lectures 01:01:13

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

Preview 03: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

Preview 06:58

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

Preview 07:18

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.

Preview 09:53

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

Preview 15:01

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

Preview 07:17, 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, which lets you set up lots of kinds of app server projects -- Java Tomcat included.

Preview 00:12

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.

Preview 10:36
+ Nuts and Bolts
10 lectures 01:26:43

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

Importing Java Classes Into JSPs

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

Getting URL Parameters

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

Scripting HTML

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.

Including Files In Other Files

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

Forwarding and Redirecting

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.

The Declaration Tag

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

JSP Tags Summary

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

Model 1 vs. Model 2 Architecture

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
Using External APIs in Web Applications

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

The Servlet Lifecycle
+ Beans and Forms
9 lectures 01:52:10

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

JSP Beans

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.

Beans and Scope

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

Setting Bean Properties Using Parameters

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


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

HTTP Posts

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

Using Beans to Store Form Data

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

Adding Validation to Beans

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

Self-Validating Forms

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

Validating Forms Using MVC Architecture
+ Some Useful Objects
6 lectures 55:49

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

The Session Object

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!

URL Rewriting: Supporting Sessions Without Cookies

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

The Servlet Context Object

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

Getting Initialisation Parameters from web.xml

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

Scoped Objects Review

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

+ Databases
4 lectures 01:06:03

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

Connecting to a Database

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

Setting Up a JNDI Data Source

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

Querying Databases

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

Inserting Data Into Databases
+ Sending Emails
2 lectures 20:47

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

Sending Emails: Prerequisites

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

Sending Emails with the Mail API
+ Simplify Your Code With JSTL
22 lectures 02:42:08

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

Beginning JSTL: Hello World JSTL

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

Accessing Bean Values in JSTL

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

Getting URL Parameters in JSTL

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
The JSTL "if" Statement

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

The Choose Tag in JSTL

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

The foreach loop in JSTL

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

Referencing Java Objects in JSTL

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

JSTL Implicit Objects

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

Using Maps in EL

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

Outputting Variables: The c:out Tag vs. Using EL Directly

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

forEach: Iterating Through Lists

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

Using External Files: Graphics, CSS, etc

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

Reusable Fragments Using JSTL c:import

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

SQL Queries with JSTL

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

Setting Variables in JSTL

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

Expressions in EL

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


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

Wildcards and Getting a Single Row in JSTL

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

JSTL Functions

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

SQL Updates and Transactions in JSTL

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

Formatting Using JSTL

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

Custom Tags
+ Where Now?
1 lecture 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 :)

Some Final Words ....
+ Extra!
1 lecture 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.

Uploading Files: Allowing the User to Upload Images and Other Stuff
  • Basic Java knowledge
  • Ideally basic HTML and SQL knowledge
  • Basic computer and Internet skills

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.

Who this course is for:
  • 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